wps-hide-login
Last commit date
classes
4 years ago
languages
4 years ago
vendor
4 years ago
autoload.php
4 years ago
composer.json
4 years ago
composer.lock
4 years ago
readme.txt
4 years ago
uninstall.php
4 years ago
wps-hide-login.php
4 years ago
autoload.php
205 lines
| 1 | <?php |
| 2 | namespace WPS\WPS_Hide_Login; |
| 3 | |
| 4 | /** |
| 5 | * An example of a general-purpose implementation that includes the optional |
| 6 | * functionality of allowing multiple base directories for a single namespace |
| 7 | * prefix. |
| 8 | * |
| 9 | * Given a foo-bar package of classes in the file system at the following |
| 10 | * paths ... |
| 11 | * |
| 12 | * /path/to/packages/foo-bar/ |
| 13 | * src/ |
| 14 | * Baz.php # Foo\Bar\Baz |
| 15 | * Qux/ |
| 16 | * Quux.php # Foo\Bar\Qux\Quux |
| 17 | * tests/ |
| 18 | * BazTest.php # Foo\Bar\BazTest |
| 19 | * Qux/ |
| 20 | * QuuxTest.php # Foo\Bar\Qux\QuuxTest |
| 21 | * |
| 22 | * ... add the path to the class files for the \Foo\Bar\ namespace prefix |
| 23 | * as follows: |
| 24 | * |
| 25 | * <?php |
| 26 | * // instantiate the loader |
| 27 | * $loader = new \Example\Psr4AutoloaderClass; |
| 28 | * |
| 29 | * // register the autoloader |
| 30 | * $loader->register(); |
| 31 | * |
| 32 | * // register the base directories for the namespace prefix |
| 33 | * $loader->addNamespace('Foo\Bar', '/path/to/packages/foo-bar/src'); |
| 34 | * $loader->addNamespace('Foo\Bar', '/path/to/packages/foo-bar/tests'); |
| 35 | * |
| 36 | * The following line would cause the autoloader to attempt to load the |
| 37 | * \Foo\Bar\Qux\Quux class from /path/to/packages/foo-bar/src/Qux/Quux.php: |
| 38 | * |
| 39 | * <?php |
| 40 | * new \Foo\Bar\Qux\Quux; |
| 41 | * |
| 42 | * The following line would cause the autoloader to attempt to load the |
| 43 | * \Foo\Bar\Qux\QuuxTest class from /path/to/packages/foo-bar/tests/Qux/QuuxTest.php: |
| 44 | * |
| 45 | * <?php |
| 46 | * new \Foo\Bar\Qux\QuuxTest; |
| 47 | */ |
| 48 | class Autoloader { |
| 49 | /** |
| 50 | * An associative array where the key is a namespace prefix and the value |
| 51 | * is an array of base directories for classes in that namespace. |
| 52 | * |
| 53 | * @var array |
| 54 | */ |
| 55 | protected $prefixes = array(); |
| 56 | |
| 57 | /** |
| 58 | * Register loader with SPL autoloader stack. |
| 59 | * |
| 60 | * @return void |
| 61 | */ |
| 62 | public function register() { |
| 63 | spl_autoload_register( array( $this, 'loadClass' ) ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Adds a base directory for a namespace prefix. |
| 68 | * |
| 69 | * @param string $prefix The namespace prefix. |
| 70 | * @param string $base_dir A base directory for class files in the |
| 71 | * namespace. |
| 72 | * @param bool $prepend If true, prepend the base directory to the stack |
| 73 | * instead of appending it; this causes it to be searched first rather |
| 74 | * than last. |
| 75 | * |
| 76 | * @return void |
| 77 | */ |
| 78 | public function addNamespace( $prefix, $base_dir, $prepend = false ) { |
| 79 | // normalize namespace prefix |
| 80 | $prefix = trim( $prefix, '\\' ) . '\\'; |
| 81 | |
| 82 | // normalize the base directory with a trailing separator |
| 83 | $base_dir = rtrim( $base_dir, DIRECTORY_SEPARATOR ) . '/'; |
| 84 | |
| 85 | // initialize the namespace prefix array |
| 86 | if ( isset( $this->prefixes[ $prefix ] ) === false ) { |
| 87 | $this->prefixes[ $prefix ] = array(); |
| 88 | } |
| 89 | |
| 90 | // retain the base directory for the namespace prefix |
| 91 | if ( $prepend ) { |
| 92 | array_unshift( $this->prefixes[ $prefix ], $base_dir ); |
| 93 | } else { |
| 94 | array_push( $this->prefixes[ $prefix ], $base_dir ); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Loads the class file for a given class name. |
| 100 | * |
| 101 | * @param string $class The fully-qualified class name. |
| 102 | * |
| 103 | * @return mixed The mapped file name on success, or boolean false on |
| 104 | * failure. |
| 105 | */ |
| 106 | public function loadClass( $class ) { |
| 107 | // the current namespace prefix |
| 108 | $prefix = $class; |
| 109 | |
| 110 | // work backwards through the namespace names of the fully-qualified |
| 111 | // class name to find a mapped file name |
| 112 | while( false !== $pos = strrpos( $prefix, '\\' ) ) { |
| 113 | |
| 114 | // retain the trailing namespace separator in the prefix |
| 115 | $prefix = substr( $class, 0, $pos + 1 ); |
| 116 | |
| 117 | // the rest is the relative class name |
| 118 | $relative_class = substr( $class, $pos + 1 ); |
| 119 | |
| 120 | // try to load a mapped file for the prefix and relative class |
| 121 | $mapped_file = $this->loadMappedFile( $prefix, $relative_class ); |
| 122 | if ( $mapped_file ) { |
| 123 | return $mapped_file; |
| 124 | } |
| 125 | |
| 126 | // remove the trailing namespace separator for the next iteration |
| 127 | // of strrpos() |
| 128 | $prefix = rtrim( $prefix, '\\' ); |
| 129 | } |
| 130 | |
| 131 | // never found a mapped file |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Load the mapped file for a namespace prefix and relative class. |
| 137 | * |
| 138 | * @param string $prefix The namespace prefix. |
| 139 | * @param string $relative_class The relative class name. |
| 140 | * |
| 141 | * @return mixed Boolean false if no mapped file can be loaded, or the |
| 142 | * name of the mapped file that was loaded. |
| 143 | */ |
| 144 | protected function loadMappedFile( $prefix, $relative_class ) { |
| 145 | // are there any base directories for this namespace prefix? |
| 146 | if ( isset( $this->prefixes[ $prefix ] ) === false ) { |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | // look through base directories for this namespace prefix |
| 151 | foreach ( $this->prefixes[ $prefix ] as $base_dir ) { |
| 152 | |
| 153 | // replace the namespace prefix with the base directory, |
| 154 | // replace namespace separators with directory separators |
| 155 | // in the relative class name, append with .php |
| 156 | $file = $base_dir . strtolower( str_replace( array( '\\', '_' ), array( |
| 157 | '/', |
| 158 | '-' |
| 159 | ), $relative_class ) ) . '.php'; |
| 160 | |
| 161 | // if the mapped file exists, require it |
| 162 | if ( $this->requireFile( $file ) ) { |
| 163 | // yes, we're done |
| 164 | return $file; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // never found it |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * If a file exists, require it from the file system. |
| 174 | * |
| 175 | * @param string $file The file to require. |
| 176 | * |
| 177 | * @return bool True if the file exists, false if not. |
| 178 | */ |
| 179 | protected function requireFile( $file ) { |
| 180 | if ( file_exists( $file ) ) { |
| 181 | require $file; |
| 182 | |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | return false; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // instantiate the loader |
| 191 | $loader = new \WPS\WPS_Hide_Login\Autoloader; |
| 192 | |
| 193 | // register the autoloader |
| 194 | $loader->register(); |
| 195 | |
| 196 | // register the base directories for the namespace prefix |
| 197 | $loader->addNamespace( 'WPS\WPS_Hide_Login', WPS_HIDE_LOGIN_DIR . 'classes' ); |
| 198 | |
| 199 | /** |
| 200 | * Vendor autoload |
| 201 | * @since 2.1.0 |
| 202 | */ |
| 203 | if ( file_exists( WPS_HIDE_LOGIN_DIR . 'vendor/autoload.php' ) ) { |
| 204 | require WPS_HIDE_LOGIN_DIR . 'vendor/autoload.php'; |
| 205 | } |