Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrates Autoloader and FileLocator #1562

Merged
merged 27 commits into from
Dec 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8e9ec47
Move autoloader to BaseService and ensure locator use shared instance
natanfelles Nov 29, 2018
27f7ff5
Move autoloader to BaseService and ensure locator use shared instance
natanfelles Nov 29, 2018
2ef1c73
FileLocator get namespaces by Autoloader DI
natanfelles Nov 29, 2018
6e28573
Ensure that FileLocator uses the same (first) instance of Autoloader
natanfelles Nov 29, 2018
12192cd
Update Autoloader
natanfelles Nov 30, 2018
1a6fc12
Test Autoloader
natanfelles Nov 30, 2018
7ef281f
Update FileLocator
natanfelles Nov 30, 2018
d25f49c
Add UnnamespacedClass to truth test
natanfelles Nov 30, 2018
9068e3b
Update FileLocatorTest
natanfelles Nov 30, 2018
7e9b496
Remove unused mocks
natanfelles Nov 30, 2018
2eb123c
No more mocks here!
natanfelles Nov 30, 2018
379d1c6
Initializes the Autoloader after reset all in ValidationTest
natanfelles Nov 30, 2018
958ff8e
Initializes the Autoloader service after a services reset
natanfelles Nov 30, 2018
2d7f931
Test Autoloader package instances
natanfelles Nov 30, 2018
b75b2d5
Test more Services
natanfelles Nov 30, 2018
1e9c5a2
Allow get current locale
natanfelles Nov 30, 2018
4320de7
Makes the FileLocator even more reliable
natanfelles Dec 1, 2018
f31d605
Remove some lines and update test method name
natanfelles Dec 1, 2018
d477ac0
Allow auto-initialize autoloader instance after clear
natanfelles Dec 1, 2018
ba53ae9
Returns false if the Qualified Name is not found
natanfelles Dec 1, 2018
07d7380
Update FileLocator
natanfelles Dec 1, 2018
af2eba6
Return null since it is not a void function
natanfelles Dec 1, 2018
f5d9f62
Use static method rather than function to use services
natanfelles Dec 1, 2018
817a9cd
Add CLI.commandNotFound language string
natanfelles Dec 1, 2018
40ec26b
Update some PHPDocs and return types
natanfelles Dec 1, 2018
64cfc1a
Update testBadCommand error string
natanfelles Dec 1, 2018
90cab15
Surround the check value by slashes to ensure the position is the fol…
natanfelles Dec 1, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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