mirror of
https://github.com/danog/class-finder.git
synced 2025-01-23 06:11:26 +01:00
Refactor some PSR4 specific stuff into a PSR4 class
This commit is contained in:
parent
8b8404cf82
commit
38a1c6a8c2
@ -6,11 +6,18 @@ class ClassFinder
|
||||
/** @var AppConfig */
|
||||
private static $config;
|
||||
|
||||
/** @var PSR4Finder */
|
||||
private static $psr4;
|
||||
|
||||
private static function initialize()
|
||||
{
|
||||
if (!(self::$config instanceof AppConfig)) {
|
||||
self::$config = new AppConfig();
|
||||
}
|
||||
|
||||
if (!(self::$psr4 instanceof PSR4Finder)) {
|
||||
self::$psr4 = new PSR4Finder(self::$config);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -22,7 +29,7 @@ class ClassFinder
|
||||
{
|
||||
self::initialize();
|
||||
|
||||
$files = scandir(self::getNamespaceDirectory($namespace));
|
||||
$files = scandir(self::$psr4->getNamespaceDirectory($namespace));
|
||||
|
||||
$classes = array_map(function($file) use ($namespace){
|
||||
return $namespace . '\\' . str_replace('.php', '', $file);
|
||||
@ -35,46 +42,6 @@ class ClassFinder
|
||||
return array_values($classes);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $namespace
|
||||
* @return bool|string
|
||||
* @throws \Exception
|
||||
*/
|
||||
private static function getNamespaceDirectory($namespace)
|
||||
{
|
||||
$appRoot = self::$config->getAppRoot();
|
||||
|
||||
$composerNamespaces = self::$config->getDefinedNamespaces();
|
||||
|
||||
$namespaceFragments = explode('\\', $namespace);
|
||||
$undefinedNamespaceFragments = [];
|
||||
|
||||
while($namespaceFragments) {
|
||||
$possibleNamespace = implode('\\', $namespaceFragments) . '\\';
|
||||
|
||||
if(array_key_exists($possibleNamespace, $composerNamespaces)){
|
||||
$resolvedDirectory = $appRoot . $composerNamespaces[$possibleNamespace] . implode('/', $undefinedNamespaceFragments);
|
||||
$realDirectory = realpath($resolvedDirectory);
|
||||
if ($realDirectory !== false) {
|
||||
return $realDirectory;
|
||||
} else {
|
||||
throw new ClassFinderException(sprintf("Unknown namespace '%s'. Checked for files in %s, but that directory did not exist. See %s for details.",
|
||||
$namespace,
|
||||
$resolvedDirectory,
|
||||
'https://gitlab.com/hpierce1102/ClassFinder/blob/master/docs/exceptions/unknownSubNamespace.md'
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
array_unshift($undefinedNamespaceFragments, array_pop($namespaceFragments));
|
||||
}
|
||||
|
||||
throw new ClassFinderException(sprintf("Unknown namespace '%s'. You should add the namespace prefix to composer.json. See '%s' for details.",
|
||||
$namespace,
|
||||
'https://gitlab.com/hpierce1102/ClassFinder/blob/master/docs/exceptions/unregisteredRoot.md'
|
||||
));
|
||||
}
|
||||
|
||||
public static function setAppRoot($appRoot)
|
||||
{
|
||||
self::initialize();
|
||||
|
7
src/FinderInterface.php
Normal file
7
src/FinderInterface.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace HaydenPierce\ClassFinder;
|
||||
|
||||
interface FinderInterface
|
||||
{
|
||||
|
||||
}
|
67
src/PSR4Finder.php
Normal file
67
src/PSR4Finder.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
namespace HaydenPierce\ClassFinder;
|
||||
|
||||
class PSR4Finder implements FinderInterface
|
||||
{
|
||||
private $config;
|
||||
|
||||
public function __construct(AppConfig $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
/**
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getDefinedNamespaces()
|
||||
{
|
||||
$appRoot = $this->config->getAppRoot();
|
||||
|
||||
$composerJsonPath = $appRoot. 'composer.json';
|
||||
$composerConfig = json_decode(file_get_contents($composerJsonPath));
|
||||
|
||||
//Apparently PHP doesn't like hyphens, so we use variable variables instead.
|
||||
$psr4 = "psr-4";
|
||||
return (array) $composerConfig->autoload->$psr4;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $namespace
|
||||
* @return bool|string
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getNamespaceDirectory($namespace)
|
||||
{
|
||||
$appRoot = $this->config->getAppRoot();
|
||||
|
||||
$composerNamespaces = $this->config->getDefinedNamespaces();
|
||||
|
||||
$namespaceFragments = explode('\\', $namespace);
|
||||
$undefinedNamespaceFragments = [];
|
||||
|
||||
while($namespaceFragments) {
|
||||
$possibleNamespace = implode('\\', $namespaceFragments) . '\\';
|
||||
|
||||
if(array_key_exists($possibleNamespace, $composerNamespaces)){
|
||||
$resolvedDirectory = $appRoot . $composerNamespaces[$possibleNamespace] . implode('/', $undefinedNamespaceFragments);
|
||||
$realDirectory = realpath($resolvedDirectory);
|
||||
if ($realDirectory !== false) {
|
||||
return $realDirectory;
|
||||
} else {
|
||||
throw new ClassFinderException(sprintf("Unknown namespace '%s'. Checked for files in %s, but that directory did not exist. See %s for details.",
|
||||
$namespace,
|
||||
$resolvedDirectory,
|
||||
'https://gitlab.com/hpierce1102/ClassFinder/blob/master/docs/exceptions/unknownSubNamespace.md'
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
array_unshift($undefinedNamespaceFragments, array_pop($namespaceFragments));
|
||||
}
|
||||
|
||||
throw new ClassFinderException(sprintf("Unknown namespace '%s'. You should add the namespace prefix to composer.json. See '%s' for details.",
|
||||
$namespace,
|
||||
'https://gitlab.com/hpierce1102/ClassFinder/blob/master/docs/exceptions/unregisteredRoot.md'
|
||||
));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user