mirror of
https://github.com/danog/class-finder.git
synced 2025-01-23 06:11:26 +01:00
Refactor to make AppConfig an instance object; move another method into the class.
This commit is contained in:
parent
4ccbaf74bb
commit
83dab4b8a0
@ -9,15 +9,20 @@ namespace HaydenPierce\ClassFinder;
|
||||
*/
|
||||
class AppConfig
|
||||
{
|
||||
public static $appRoot;
|
||||
private $appRoot;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->appRoot = $this->findAppRoot();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function findAppRoot()
|
||||
private function findAppRoot()
|
||||
{
|
||||
if (self::$appRoot) {
|
||||
$appRoot = self::$appRoot;
|
||||
if ($this->appRoot) {
|
||||
$appRoot = $this->appRoot;
|
||||
} else {
|
||||
$workingDirectory = str_replace('\\', '/', __DIR__);
|
||||
$workingDirectory = str_replace('/vendor/haydenpierce/class-finder/src', '', $workingDirectory);
|
||||
@ -34,13 +39,60 @@ class AppConfig
|
||||
} while (is_null($appRoot) && count($directoryPathPieces) > 0);
|
||||
}
|
||||
|
||||
$this->throwIfInvalidAppRoot($appRoot);
|
||||
|
||||
$this->appRoot= $appRoot;
|
||||
return $this->appRoot;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $appRoot
|
||||
* @throws ClassFinderException
|
||||
*/
|
||||
private function throwIfInvalidAppRoot($appRoot)
|
||||
{
|
||||
if (!file_exists($appRoot . '/composer.json')) {
|
||||
throw new ClassFinderException(sprintf("Could not locate composer.json. You can get around this by setting ClassFinder::\$appRoot manually. See '%s' for details.",
|
||||
'https://gitlab.com/hpierce1102/ClassFinder/blob/master/docs/exceptions/missingComposerConfig.md'
|
||||
));
|
||||
} else {
|
||||
self::$appRoot = $appRoot;
|
||||
return self::$appRoot;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws ClassFinderException
|
||||
*/
|
||||
public function getDefinedNamespaces()
|
||||
{
|
||||
$appRoot = $this->getAppRoot();
|
||||
$this->throwIfInvalidAppRoot($appRoot);
|
||||
|
||||
$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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAppRoot()
|
||||
{
|
||||
if ($this->appRoot === null) {
|
||||
$this->appRoot = $this->findAppRoot();
|
||||
}
|
||||
|
||||
return $this->appRoot;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $appRoot
|
||||
*/
|
||||
public function setAppRoot($appRoot)
|
||||
{
|
||||
$this->appRoot = $appRoot;
|
||||
}
|
||||
}
|
@ -28,22 +28,6 @@ class ClassFinder
|
||||
return array_values($classes);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
private static function getDefinedNamespaces()
|
||||
{
|
||||
$appRoot = AppConfig::findAppRoot();
|
||||
|
||||
$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
|
||||
@ -51,9 +35,9 @@ class ClassFinder
|
||||
*/
|
||||
private static function getNamespaceDirectory($namespace)
|
||||
{
|
||||
$appRoot = AppConfig::findAppRoot();
|
||||
$appRoot = self::$config->getAppRoot();
|
||||
|
||||
$composerNamespaces = self::getDefinedNamespaces();
|
||||
$composerNamespaces = self::$config->getDefinedNamespaces();
|
||||
|
||||
$namespaceFragments = explode('\\', $namespace);
|
||||
$undefinedNamespaceFragments = [];
|
||||
@ -87,7 +71,7 @@ class ClassFinder
|
||||
public static function setAppRoot($appRoot)
|
||||
{
|
||||
self::initialize();
|
||||
AppConfig::$appRoot = $appRoot;
|
||||
self::$config->setAppRoot($appRoot);
|
||||
}
|
||||
|
||||
private static function initialize()
|
||||
|
Loading…
x
Reference in New Issue
Block a user