You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
4.6 KiB
163 lines
4.6 KiB
<?php
|
|
/**
|
|
* Neato Auto loader PSR4
|
|
*
|
|
* PHP version 8.3
|
|
*
|
|
* @category Util
|
|
* @package Neato
|
|
* @author http://php-fig.org/ <info@php-fig.org>
|
|
* @license https://mit-license.org/ MIT License
|
|
* @link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
|
|
*/
|
|
|
|
/**
|
|
* Auto loader, uses namespace to guess where class file live.
|
|
*
|
|
* @category Util
|
|
* @package Neato
|
|
* @author http://php-fig.org/ <info@php-fig.org>
|
|
* @license https://mit-license.org/ MIT License
|
|
* @link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
|
|
*/
|
|
class Neato_Auto_Loader
|
|
{
|
|
|
|
/**
|
|
* 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, 'loadClass'));
|
|
}
|
|
|
|
/**
|
|
* Is class loaded, yet?
|
|
*
|
|
* @param string $prefix holds global data of namespaces
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isLoaded(string $prefix): bool
|
|
{
|
|
$prefix = trim($prefix, '\\') . '\\';
|
|
return (isset($this->prefixes[$prefix])) ? true : false;
|
|
}
|
|
|
|
/**
|
|
* Get List of loaded prefixes containing namespace/class.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getList(): 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 addNamespace(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 loadClass(string $class)
|
|
{
|
|
/**
|
|
* Semi-Fix for non-namespaced classes
|
|
*/
|
|
if (! strrpos($class, '\\')) {
|
|
return ($this->loadMappedFile($class . '\\', $class));
|
|
}
|
|
$prefix = $class;
|
|
while (false !== $pos = strrpos($prefix, '\\')) {
|
|
$prefix = substr($class, 0, $pos + 1);
|
|
$relative_class = substr($class, $pos + 1);
|
|
$mapped_file = $this->loadMappedFile($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 loadMappedFile(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->requireFile($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 requireFile(string $file): bool
|
|
{
|
|
if (file_exists($file)) {
|
|
include $file;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|