|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Klasifai; |
| 4 | + |
| 5 | +/** |
| 6 | + * PSR-4 Autoloader: |
| 7 | + * https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md#class-example |
| 8 | + */ |
| 9 | +class Psr4AutoloaderClass |
| 10 | +{ |
| 11 | + /** |
| 12 | + * An associative array where the key is a namespace prefix and the value |
| 13 | + * is an array of base directories for classes in that namespace. |
| 14 | + * |
| 15 | + * @var array |
| 16 | + */ |
| 17 | + protected $prefixes = array(); |
| 18 | + |
| 19 | + /** |
| 20 | + * Register loader with SPL autoloader stack. |
| 21 | + * |
| 22 | + * @return void |
| 23 | + */ |
| 24 | + public function register() |
| 25 | + { |
| 26 | + spl_autoload_register(array($this, 'loadClass')); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Adds a base directory for a namespace prefix. |
| 31 | + * |
| 32 | + * @param string $prefix The namespace prefix. |
| 33 | + * @param string $base_dir A base directory for class files in the |
| 34 | + * namespace. |
| 35 | + * @param bool $prepend If true, prepend the base directory to the stack |
| 36 | + * instead of appending it; this causes it to be searched first rather |
| 37 | + * than last. |
| 38 | + * @return void |
| 39 | + */ |
| 40 | + public function addNamespace($prefix, $base_dir, $prepend = false) |
| 41 | + { |
| 42 | + // normalize namespace prefix |
| 43 | + $prefix = trim($prefix, '\\') . '\\'; |
| 44 | + |
| 45 | + // normalize the base directory with a trailing separator |
| 46 | + $base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR) . '/'; |
| 47 | + |
| 48 | + // initialize the namespace prefix array |
| 49 | + if (isset($this->prefixes[$prefix]) === false) { |
| 50 | + $this->prefixes[$prefix] = array(); |
| 51 | + } |
| 52 | + |
| 53 | + // retain the base directory for the namespace prefix |
| 54 | + if ($prepend) { |
| 55 | + array_unshift($this->prefixes[$prefix], $base_dir); |
| 56 | + } else { |
| 57 | + array_push($this->prefixes[$prefix], $base_dir); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Loads the class file for a given class name. |
| 63 | + * |
| 64 | + * @param string $class The fully-qualified class name. |
| 65 | + * @return mixed The mapped file name on success, or boolean false on |
| 66 | + * failure. |
| 67 | + */ |
| 68 | + public function loadClass($class) |
| 69 | + { |
| 70 | + // the current namespace prefix |
| 71 | + $prefix = $class; |
| 72 | + |
| 73 | + // work backwards through the namespace names of the fully-qualified |
| 74 | + // class name to find a mapped file name |
| 75 | + while (false !== $pos = strrpos($prefix, '\\')) { |
| 76 | + |
| 77 | + // retain the trailing namespace separator in the prefix |
| 78 | + $prefix = substr($class, 0, $pos + 1); |
| 79 | + |
| 80 | + // the rest is the relative class name |
| 81 | + $relative_class = substr($class, $pos + 1); |
| 82 | + |
| 83 | + // try to load a mapped file for the prefix and relative class |
| 84 | + $mapped_file = $this->loadMappedFile($prefix, $relative_class); |
| 85 | + if ($mapped_file) { |
| 86 | + return $mapped_file; |
| 87 | + } |
| 88 | + |
| 89 | + // remove the trailing namespace separator for the next iteration |
| 90 | + // of strrpos() |
| 91 | + $prefix = rtrim($prefix, '\\'); |
| 92 | + } |
| 93 | + |
| 94 | + // never found a mapped file |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Load the mapped file for a namespace prefix and relative class. |
| 100 | + * |
| 101 | + * @param string $prefix The namespace prefix. |
| 102 | + * @param string $relative_class The relative class name. |
| 103 | + * @return mixed Boolean false if no mapped file can be loaded, or the |
| 104 | + * name of the mapped file that was loaded. |
| 105 | + */ |
| 106 | + protected function loadMappedFile($prefix, $relative_class) |
| 107 | + { |
| 108 | + // are there any base directories for this namespace prefix? |
| 109 | + if (isset($this->prefixes[$prefix]) === false) { |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + // look through base directories for this namespace prefix |
| 114 | + foreach ($this->prefixes[$prefix] as $base_dir) { |
| 115 | + |
| 116 | + // replace the namespace prefix with the base directory, |
| 117 | + // replace namespace separators with directory separators |
| 118 | + // in the relative class name, append with .php |
| 119 | + $file = $base_dir |
| 120 | + . str_replace('\\', '/', $relative_class) |
| 121 | + . '.php'; |
| 122 | + |
| 123 | + // if the mapped file exists, require it |
| 124 | + if ($this->requireFile($file)) { |
| 125 | + // yes, we're done |
| 126 | + return $file; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // never found it |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * If a file exists, require it from the file system. |
| 136 | + * |
| 137 | + * @param string $file The file to require. |
| 138 | + * @return bool True if the file exists, false if not. |
| 139 | + */ |
| 140 | + protected function requireFile($file) |
| 141 | + { |
| 142 | + if (file_exists($file)) { |
| 143 | + require $file; |
| 144 | + return true; |
| 145 | + } |
| 146 | + return false; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +// instantiate the loader |
| 151 | +$klasifai_loader = new \Klasifai\Psr4AutoloaderClass(); |
| 152 | + |
| 153 | +// register the autoloader |
| 154 | +$klasifai_loader->register(); |
| 155 | + |
| 156 | +// register the base directories for the namespace prefix |
| 157 | +$klasifai_loader->addNamespace('Klasifai', __DIR__ . '/includes/Klasifai' ); |
| 158 | + |
| 159 | +require_once( __DIR__ . '/includes/Klasifai/Helpers.php' ); |
0 commit comments