* @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 = []; /** * 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; } /** * 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) { /** * Semi-Fix for non-namespaced classes */ if (! strrpos($class, '\\')) { return ($this->load_mapped_file($class . '\\', $class)); } $prefix = $class; while (false !== $pos = strrpos($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; } $prefix = rtrim($prefix, '\\'); } 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) { if (isset($this->prefixes[$prefix]) === false) { return false; } foreach ($this->prefixes[$prefix] as $base_dir) { $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php'; if ($this->require_file($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. */ protected function require_file(string $file): bool { if (file_exists($file)) { require $file; return true; } return false; } }