From 5044e44161dfa0725a14fadf4baa7263dca1cc98 Mon Sep 17 00:00:00 2001 From: neoan Date: Fri, 26 Aug 2022 22:50:53 -0400 Subject: [PATCH] Address issue #26 --- src/Helper/ComposerParser.php | 28 +++++++++++ src/NeoanApp.php | 7 ++- src/Routing/AttributeRouting.php | 5 +- tests/Mocks/database.db | Bin 28672 -> 28672 bytes tests/NeoanAppTest.php | 5 ++ tests/clover.xml | 84 +++++++++++++++++++------------ 6 files changed, 94 insertions(+), 35 deletions(-) create mode 100644 src/Helper/ComposerParser.php 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 0b7a81686942a75f92229964d1000fae629e710f..a9685760723f761e76b941614608d59c26a978fc 100644 GIT binary patch delta 255 zcmZp8z}WDBae}nqYz77fb|8iUvxz#!lCv4~f)jZ8e=xA|)g;QsF0QT3*qk`IoX2sp5x>r4M?TTXzj=hgvS23zoF*HyyP6~2NbMf&s$PY6&&Ga;Kaji%+PxYuQ z)OXR>%MLR0Pxkc63^yw?4#^13sc=fp&kc4--Q@6invoke($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 @@ - +