* @copyright Copyright (c) 2013-2017 PHP Framework Interop Group * @license MIT * @site https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md */ class Psr4AutoloaderClass { /** * An associative array where the key is a namespace prefix and the value * is an array of base directories for classes in that namespace. * * @var array */ protected $prefixes = []; protected $loaded_files = []; /** * Register loader with SPL autoloader stack. * * @return void */ public function register() { spl_autoload_register(array($this, 'load_class')); } public function is_loaded(string $prefix): bool { $prefix = trim($prefix, '\\') . '\\'; return (isset($this->prefixes[$prefix])) ? true : false; } public function get_list(): array { return $this->prefixes; } public function get_files_list(): array { return $this->loaded_files; } /** * Adds a base directory for a namespace prefix. * * @param string $prefix The namespace prefix. * @param string $base_dir A base directory for class files in the * namespace. * @param bool $prepend If true, prepend the base directory to the stack * instead of appending it; this causes it to be searched first rather * than last. * @return void */ public function add_namespace(string $prefix, string $base_dir, bool $prepend = false): void { $prefix = trim($prefix, '\\') . '\\'; $base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR) . '/'; if (isset($this->prefixes[$prefix]) === false) { $this->prefixes[$prefix] = array(); } if ($prepend) { array_unshift($this->prefixes[$prefix], $base_dir); } else { array_push($this->prefixes[$prefix], $base_dir); } } /** * Loads the class file for a given class name. * * @param string $class The fully-qualified class name. * @return mixed The mapped file name on success, or boolean false on * failure. */ public function load_class(string $class): false|string { if (! strrpos($class, '\\')) { $ret = ($this->load_mapped_file($class . '\\', $class)); if ($ret !== false) { return $ret; } } $prefix = $class; while (false !== $pos = strrpos($prefix, '\\')) { // retain the trailing namespace separator in the prefix $prefix = substr($class, 0, $pos + 1); $relative_class = substr($class, $pos + 1); $mapped_file = $this->load_mapped_file($prefix, $relative_class); if ($mapped_file) { return $mapped_file; } // remove the trailing namespace separator for the next iteration // of strrpos() $prefix = rtrim($prefix, '\\'); } // never found a mapped file return false; } /** * Load the mapped file for a namespace prefix and relative class. * * @param string $prefix The namespace prefix. * @param string $relative_class The relative class name. * @return mixed Boolean false if no mapped file can be loaded, or the * name of the mapped file that was loaded. */ protected function load_mapped_file(string $prefix, string $relative_class): false | string { // are there any base directories for this namespace prefix? if (isset($this->prefixes[$prefix]) === false) { return false; } // look through base directories for this namespace prefix foreach ($this->prefixes[$prefix] as $base_dir) { $file = str_replace('\\', '/', $relative_class) . '.php'; if ($this->require_file($base_dir, $file)) { return $file; } } return false; } /** * If a file exists, require it from the file system. * * @param string $file The file to require. * @return bool True if the file exists, false if not. */ private function require_file(string $path, string $file): bool { $safer_file = requires::safer_file_exists($file, $path); if ($safer_file !== false) { if (! isset($this->loaded_files[$safer_file])) { require $safer_file; $this->loaded_files[$safer_file] = true; } return true; } return false; } }