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