themeisle-companion
Last commit date
core
3 years ago
dashboard
3 years ago
images
8 years ago
languages
3 years ago
obfx_modules
3 years ago
vendor
3 years ago
.eslintrc
4 years ago
CHANGELOG.md
3 years ago
CONTRIBUTING.md
5 years ago
LICENSE.txt
8 years ago
class-autoloader.php
6 years ago
index.php
8 years ago
readme.md
3 years ago
readme.txt
3 years ago
themeisle-companion.php
3 years ago
uninstall.php
8 years ago
class-autoloader.php
220 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The file that defines autoload class |
| 4 | * |
| 5 | * A simple autoloader that loads class files recursively starting in the directory |
| 6 | * where this class resides. Additional options can be provided to control the naming |
| 7 | * convention of the class files. |
| 8 | * |
| 9 | * @link https://themeisle.com |
| 10 | * @copyright Copyright (c) 2017, Bogdan Preda |
| 11 | * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 12 | * |
| 13 | * @since 1.0.0 |
| 14 | * @package Orbit_Fox |
| 15 | */ |
| 16 | |
| 17 | /** |
| 18 | * The Autoloader class. |
| 19 | * |
| 20 | * @since 1.0.0 |
| 21 | * @package Orbit_Fox |
| 22 | * @author Themeisle <friends@themeisle.com> |
| 23 | */ |
| 24 | class Autoloader { |
| 25 | |
| 26 | /** |
| 27 | * File extension as a string. Defaults to ".php". |
| 28 | * |
| 29 | * @since 1.0.0 |
| 30 | * @access protected |
| 31 | * @var string $file_ext The file extension to look for. |
| 32 | */ |
| 33 | protected static $file_ext = '.php'; |
| 34 | |
| 35 | /** |
| 36 | * The top level directory where recursion will begin. Defaults to the current |
| 37 | * directory. |
| 38 | * |
| 39 | * @since 1.0.0 |
| 40 | * @access protected |
| 41 | * @var string $path_top The root directory. |
| 42 | */ |
| 43 | protected static $path_top = __DIR__; |
| 44 | |
| 45 | /** |
| 46 | * The plugin directory where recursion will begin. Defaults to empty ( No module will be loaded ). |
| 47 | * |
| 48 | * @since 1.0.0 |
| 49 | * @access protected |
| 50 | * @var string $plugins_path The installation plugins directory. |
| 51 | */ |
| 52 | protected static $plugins_path = ''; |
| 53 | |
| 54 | /** |
| 55 | * Holds an array of namespaces to filter in autoloading if set. |
| 56 | * |
| 57 | * @since 1.0.0 |
| 58 | * @access protected |
| 59 | * @var array $namespaces The namespace array, used if not empty on autoloading. |
| 60 | */ |
| 61 | protected static $namespaces = array(); |
| 62 | |
| 63 | /** |
| 64 | * An array of files to exclude when looking to autoload. |
| 65 | * |
| 66 | * @since 1.0.0 |
| 67 | * @access protected |
| 68 | * @var array $excluded_files The excluded files list. |
| 69 | */ |
| 70 | protected static $excluded_files = array(); |
| 71 | |
| 72 | /** |
| 73 | * A placeholder to hold the file iterator so that directory traversal is only |
| 74 | * performed once. |
| 75 | * |
| 76 | * @since 1.0.0 |
| 77 | * @access protected |
| 78 | * @var RecursiveIteratorIterator $file_iterator Holds an instance of the iterator class. |
| 79 | */ |
| 80 | protected static $file_iterator = null; |
| 81 | |
| 82 | /** |
| 83 | * Method to check in allowed namespaces. |
| 84 | * |
| 85 | * @since 1.0.0 |
| 86 | * @access protected |
| 87 | * @param string $class_name the class name to check with the namespaces. |
| 88 | * @return bool |
| 89 | */ |
| 90 | protected static function check_namespaces( $class_name ) { |
| 91 | $found = false; |
| 92 | foreach ( static::$namespaces as $namespace ) { |
| 93 | if ( substr( $class_name, 0, strlen( $namespace ) ) === $namespace ) { |
| 94 | $found = true; |
| 95 | } |
| 96 | if ( $namespace === 'OBFX_Module' && substr( $class_name, strlen( $namespace ) * ( -1 ), strlen( $namespace ) ) === $namespace ) { |
| 97 | return static::module_loader( $class_name ); |
| 98 | } |
| 99 | } |
| 100 | return $found; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Autoload function for registration with spl_autoload_register |
| 105 | * |
| 106 | * Looks recursively through project directory and loads class files based on |
| 107 | * filename match. |
| 108 | * |
| 109 | * @since 1.0.0 |
| 110 | * @access public |
| 111 | * @param string $class_name The class name requested. |
| 112 | * @return mixed |
| 113 | */ |
| 114 | public static function loader( $class_name ) { |
| 115 | |
| 116 | if ( ! empty( static::$namespaces ) ) { |
| 117 | $found = static::check_namespaces( $class_name ); |
| 118 | if ( ! $found ) { |
| 119 | return $found; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | $directory = new RecursiveDirectoryIterator( static::$path_top . DIRECTORY_SEPARATOR . 'core', RecursiveDirectoryIterator::SKIP_DOTS ); |
| 124 | |
| 125 | if ( is_null( static::$file_iterator ) ) { |
| 126 | $iterator = new RecursiveIteratorIterator( $directory ); |
| 127 | $regex = new RegexIterator( $iterator, '/^.+\.php$/i', RecursiveRegexIterator::MATCH ); |
| 128 | static::$file_iterator = iterator_to_array( $regex, false ); |
| 129 | } |
| 130 | |
| 131 | $filename = 'class-' . str_replace( '_', '-', strtolower( $class_name ) ) . static::$file_ext; |
| 132 | foreach ( static::$file_iterator as $file ) { |
| 133 | if ( strtolower( $file->getFileName() ) === strtolower( $filename ) && is_readable( $file->getPathName() ) ) { |
| 134 | require $file->getPathName(); |
| 135 | return true; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Method used for loading required module init file. |
| 142 | * |
| 143 | * @since 1.0.0 |
| 144 | * @access public |
| 145 | * @param string $class_name The class name requested. |
| 146 | * @return bool |
| 147 | */ |
| 148 | public static function module_loader( $class_name ) { |
| 149 | $module_name = str_replace( '_', '-', strtolower( str_replace( '_OBFX_Module', '', $class_name ) ) ); |
| 150 | if ( static::$plugins_path !== '' ) { |
| 151 | $directories = glob( static::$plugins_path . '*' . DIRECTORY_SEPARATOR . 'obfx_modules' . DIRECTORY_SEPARATOR . $module_name, GLOB_ONLYDIR ); |
| 152 | foreach ( $directories as $directory ) { |
| 153 | $filename = $directory . DIRECTORY_SEPARATOR . 'init.php'; |
| 154 | if ( is_readable( $filename ) ) { |
| 155 | require $filename; |
| 156 | return true; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Sets the $file_ext property |
| 165 | * |
| 166 | * @since 1.0.0 |
| 167 | * @access public |
| 168 | * @param string $file_ext The file extension used for class files. Default is "php". |
| 169 | */ |
| 170 | public static function set_file_ext( $file_ext ) { |
| 171 | static::$file_ext = $file_ext; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Sets the $plugins_path property |
| 176 | * |
| 177 | * @since 1.0.0 |
| 178 | * @access public |
| 179 | * @param string $path The path representing the top level where recursion should |
| 180 | * begin for plugins. Defaults to empty ( does not look in plugins ). |
| 181 | */ |
| 182 | public static function set_plugins_path( $path ) { |
| 183 | static::$plugins_path = $path; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Sets the $path property |
| 188 | * |
| 189 | * @since 1.0.0 |
| 190 | * @access public |
| 191 | * @param string $path The path representing the top level where recursion should |
| 192 | * begin. Defaults to the current directory. |
| 193 | */ |
| 194 | public static function set_path( $path ) { |
| 195 | static::$path_top = $path; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Adds a new file to the exclusion list. |
| 200 | * |
| 201 | * @since 1.0.0 |
| 202 | * @access public |
| 203 | * @param string $file_name The file name to exclude from autoload. |
| 204 | */ |
| 205 | public static function exclude_file( $file_name ) { |
| 206 | static::$excluded_files[] = $file_name; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Sets the namespaces used in autoloading if any. |
| 211 | * |
| 212 | * @since 1.0.0 |
| 213 | * @access public |
| 214 | * @param array $namespaces The namespaces to use. |
| 215 | */ |
| 216 | public static function define_namespaces( $namespaces = array() ) { |
| 217 | static::$namespaces = $namespaces; |
| 218 | } |
| 219 | } |
| 220 |