diff --git a/src/Helper/ComposerParser.php b/src/Helper/ComposerParser.php new file mode 100644 index 0000000..b41b4c6 --- /dev/null +++ b/src/Helper/ComposerParser.php @@ -0,0 +1,28 @@ +app = $app; + $composerFile = file_get_contents($app->cliPath . '/composer.json'); + $this->composerData = json_decode($composerFile, true); + } + public function getAutoloadNamespaces(): array + { + $all = []; + if(isset($this->composerData['autoload']['psr-4'])){ + $all = [...$this->composerData['autoload']['psr-4']]; + } + if(isset($this->composerData['autoload-dev']['psr-4'])){ + $all = [...$all, ...$this->composerData['autoload-dev']['psr-4']]; + } + return $all; + } +} \ No newline at end of file diff --git a/src/NeoanApp.php b/src/NeoanApp.php index 6b2e0a4..80e7a50 100644 --- a/src/NeoanApp.php +++ b/src/NeoanApp.php @@ -12,6 +12,7 @@ class NeoanApp public string $publicPath; public string $webPath; public string $cliPath; + private static NeoanApp $instance; public function __construct(string $appPath, string $publicPath, string $cliPath = __DIR__) { @@ -26,7 +27,7 @@ public function __construct(string $appPath, string $publicPath, string $cliPath define('base', $protocol . $_SERVER['HTTP_HOST'] . $this->webPath); } } - + self::$instance = $this; } /** @@ -42,4 +43,8 @@ public function invoke($instance): void { $instance($this); } + public static function getInstance(): self + { + return self::$instance; + } } \ No newline at end of file diff --git a/src/Routing/AttributeRouting.php b/src/Routing/AttributeRouting.php index 8e000f3..64e51cd 100644 --- a/src/Routing/AttributeRouting.php +++ b/src/Routing/AttributeRouting.php @@ -2,6 +2,7 @@ namespace Neoan\Routing; +use Neoan\Helper\ComposerParser; use Neoan\NeoanApp; use ReflectionClass; use ReflectionException; @@ -18,8 +19,8 @@ public function __construct(private readonly string $searchableNamespace) public function __invoke(NeoanApp $neoanApp): void { - $composerFile = json_decode(file_get_contents($neoanApp->cliPath . '/composer.json'), true); - $autoloader = array_merge($composerFile['autoload']['psr-4'], $composerFile['autoload-dev']['psr-4']); + $composer = new ComposerParser($neoanApp); + $autoloader = $composer->getAutoloadNamespaces(); $nameSpaceParts = explode('\\', $this->searchableNamespace); $searchable = ''; foreach($nameSpaceParts as $part) { diff --git a/tests/Mocks/database.db b/tests/Mocks/database.db index 0b7a816..a968576 100644 Binary files a/tests/Mocks/database.db and b/tests/Mocks/database.db differ diff --git a/tests/NeoanAppTest.php b/tests/NeoanAppTest.php index 592abc8..0fdabd9 100644 --- a/tests/NeoanAppTest.php +++ b/tests/NeoanAppTest.php @@ -17,6 +17,11 @@ function testInitiation() $testApp->invoke($listenable); $this->assertTrue(isset($testApp->testVariable)); } + function testGetInstance() + { + $testApp = new NeoanApp(__DIR__, __DIR__); + $this->assertInstanceOf(NeoanApp::class, NeoanApp::getInstance()); + } /*function testRun() { $testApp = new NeoanApp(__DIR__, __DIR__); diff --git a/tests/clover.xml b/tests/clover.xml index c8c6a31..7be7a12 100644 --- a/tests/clover.xml +++ b/tests/clover.xml @@ -1,6 +1,6 @@ - - + + @@ -290,6 +290,23 @@ + + + + + + + + + + + + + + + + + @@ -354,12 +371,12 @@ - - + + - - - + + + @@ -778,21 +795,24 @@ - + - - - - - - - - - - - - - + + + + + + + + + + + + + + + + @@ -982,9 +1002,8 @@ - - - + + @@ -994,24 +1013,25 @@ - - + + - + - + - - + + - + + @@ -1213,6 +1233,6 @@ - +