Skip to content

Commit

Permalink
Merge pull request #1562 from natanfelles/autoloader
Browse files Browse the repository at this point in the history
Integrates Autoloader and FileLocator
  • Loading branch information
jim-parry authored Dec 8, 2018
2 parents 4027f38 + 90cab15 commit 29ca3bc
Show file tree
Hide file tree
Showing 26 changed files with 502 additions and 363 deletions.
83 changes: 54 additions & 29 deletions system/Autoloader/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
*/
class Autoloader
{

/**
* Stores namespaces as key, and path as values.
*
Expand All @@ -98,6 +97,8 @@ class Autoloader
* the valid parts that we'll need.
*
* @param \Config\Autoload $config
*
* @return $this
*/
public function initialize(\Config\Autoload $config)
{
Expand All @@ -110,7 +111,7 @@ public function initialize(\Config\Autoload $config)

if (isset($config->psr4))
{
$this->prefixes = $config->psr4;
$this->addNamespace($config->psr4);
}

if (isset($config->classmap))
Expand All @@ -119,14 +120,14 @@ public function initialize(\Config\Autoload $config)
}

unset($config);

return $this;
}

//--------------------------------------------------------------------

/**
* Register the loader with the SPL autoloader stack.
*
* @codeCoverageIgnore
*/
public function register()
{
Expand All @@ -144,48 +145,79 @@ public function register()
$config = is_array($this->classmap) ? $this->classmap : [];

spl_autoload_register(function ($class) use ($config) {
if (! array_key_exists($class, $config))
if (empty($config[$class]))
{
return false;
}

include_once $config[$class];
}, true, // Throw exception
true // Prepend
true // Prepend
);
}

//--------------------------------------------------------------------

/**
* Registers a namespace with the autoloader.
* Registers namespaces with the autoloader.
*
* @param string $namespace
* @param string $path
* @param array|string $namespace
* @param string $path
*
* @return Autoloader
*/
public function addNamespace(string $namespace, string $path)
public function addNamespace($namespace, string $path = null)
{
if (isset($this->prefixes[$namespace]))
if (is_array($namespace))
{
if (is_string($this->prefixes[$namespace]))
foreach ($namespace as $prefix => $path)
{
$this->prefixes[$namespace] = [$this->prefixes[$namespace]];
}
$prefix = trim($prefix, '\\');

if (is_array($path))
{
foreach ($path as $dir)
{
$this->prefixes[$prefix][] = rtrim($dir, '/') . '/';
}

continue;
}

$this->prefixes[$namespace] = array_merge($this->prefixes[$namespace], [$path]);
$this->prefixes[$prefix][] = rtrim($path, '/') . '/';
}
}
else
{
$this->prefixes[$namespace] = [$path];
$this->prefixes[trim($namespace, '\\')][] = rtrim($path, '/') . '/';
}

return $this;
}

//--------------------------------------------------------------------

/**
* Get namespaces with prefixes as keys and paths as values.
*
* If a prefix param is set, returns only paths to the given prefix.
*
* @var string|null $prefix
*
* @return array
*/
public function getNamespace(string $prefix = null)
{
if ($prefix === null)
{
return $this->prefixes;
}

return $this->prefixes[trim($prefix, '\\')] ?? [];
}

//--------------------------------------------------------------------

/**
* Removes a single namespace from the psr4 settings.
*
Expand All @@ -195,7 +227,7 @@ public function addNamespace(string $namespace, string $path)
*/
public function removeNamespace(string $namespace)
{
unset($this->prefixes[$namespace]);
unset($this->prefixes[trim($namespace, '\\')]);

return $this;
}
Expand All @@ -207,7 +239,7 @@ public function removeNamespace(string $namespace)
*
* @param string $class The fully qualified class name.
*
* @return mixed The mapped file on success, or boolean false
* @return string|false The mapped file on success, or boolean false
* on failure.
*/
public function loadClass(string $class)
Expand All @@ -234,7 +266,7 @@ public function loadClass(string $class)
*
* @param string $class The fully-qualified class name
*
* @return mixed The mapped file name on success, or boolean false on fail
* @return string|false The mapped file name on success, or boolean false on fail
*/
protected function loadInNamespace(string $class)
{
Expand All @@ -245,18 +277,14 @@ protected function loadInNamespace(string $class)

foreach ($this->prefixes as $namespace => $directories)
{
if (is_string($directories))
{
$directories = [$directories];
}

foreach ($directories as $directory)
{
$directory = rtrim($directory, '/');

if (strpos($class, $namespace) === 0)
{
$filePath = $directory . str_replace('\\', '/', substr($class, strlen($namespace))) . '.php';
$filePath = $directory . str_replace('\\', '/',
substr($class, strlen($namespace))) . '.php';
$filename = $this->requireFile($filePath);

if ($filename)
Expand Down Expand Up @@ -316,11 +344,9 @@ protected function loadLegacy(string $class)
* A central way to require a file is loaded. Split out primarily
* for testing purposes.
*
* @codeCoverageIgnore
*
* @param string $file
*
* @return boolean
* @return string|false The filename on success, false if the file is not loaded
*/
protected function requireFile(string $file)
{
Expand Down Expand Up @@ -365,6 +391,5 @@ public function sanitizeFilename(string $filename): string

return $filename;
}

//--------------------------------------------------------------------
}
Loading

0 comments on commit 29ca3bc

Please sign in to comment.