diff --git a/app/Bootstrap/AppManager.php b/app/Bootstrap/AppManager.php new file mode 100644 index 0000000..c665f58 --- /dev/null +++ b/app/Bootstrap/AppManager.php @@ -0,0 +1,71 @@ +container = $container; + $this->config = $config; + $this->callableResolver = $callableResolver; + } + + /** + * Setup and configure the application. + * + * @return \Slim\App + */ + public function __invoke(): App + { + $this->registerProviders(); + + return Bridge::create($this->container); + } + + /** + * Register application providers. + * + * @return void + */ + protected function registerProviders(): void + { + Collection::make(self::PROVIDERS)->merge( + $this->config->get('app.providers', []) + )->each(function (string $provider) { + $this->container->call( + $this->callableResolver->resolve($provider) + ); + }); + } +} diff --git a/app/Controllers/DirectoryController.php b/app/Controllers/DirectoryController.php index 6d0ce0c..5f94ac5 100644 --- a/app/Controllers/DirectoryController.php +++ b/app/Controllers/DirectoryController.php @@ -120,6 +120,6 @@ class DirectoryController */ protected function isRoot(string $path): bool { - return realpath($path) === realpath($this->container->get('app.root')); + return realpath($path) === realpath($this->container->get('base_path')); } } diff --git a/app/Bootstrap/ConfigComposer.php b/app/Providers/ConfigProvider.php similarity index 77% rename from app/Bootstrap/ConfigComposer.php rename to app/Providers/ConfigProvider.php index d1558ca..e92e37f 100644 --- a/app/Bootstrap/ConfigComposer.php +++ b/app/Providers/ConfigProvider.php @@ -1,17 +1,17 @@ merge(self::APP_FILES); })->map(function (string $file) { return glob( - $this->container->get('app.root') . '/' . $file, + $this->container->get('base_path') . '/' . $file, GLOB_BRACE | GLOB_NOSORT ); })->flatten()->map(function (string $file) { diff --git a/app/Providers/ParsedownProvider.php b/app/Providers/ParsedownProvider.php new file mode 100644 index 0000000..483a897 --- /dev/null +++ b/app/Providers/ParsedownProvider.php @@ -0,0 +1,32 @@ +container = $container; + } + + /** + * Initialize and register the Parsedown component. + * + * @return void + */ + public function __invoke(): void + { + $this->container->set(Parsedown::class, new Parsedown); + } +} diff --git a/app/Bootstrap/ViewComposer.php b/app/Providers/TwigProvider.php similarity index 90% rename from app/Bootstrap/ViewComposer.php rename to app/Providers/TwigProvider.php index bdfd07d..d046e35 100644 --- a/app/Bootstrap/ViewComposer.php +++ b/app/Providers/TwigProvider.php @@ -1,7 +1,8 @@ Helpers::env('RENDER_README', true), + + /** + * Additional providers to be loaded during application initialization. + * + * Default value: [] + */ + 'providers' => [ + // ... + ], ]; diff --git a/index.php b/index.php index 9cd22a9..24eafb1 100644 --- a/index.php +++ b/index.php @@ -1,8 +1,7 @@ load(); // Initialize the container $container = new Container(); -$container->set('app.root', __DIR__); +$container->set('base_path', __DIR__); -// Configure the application componentes -$container->call(Bootstrap\ConfigComposer::class); -$container->call(Bootstrap\FinderComposer::class); -$container->call(Bootstrap\ViewComposer::class); - -// Create the application -$app = Bridge::create($container); +// Configure the application +$app = $container->call(AppManager::class); // Register routes $app->get('/file-info/[{path:.*}]', Controllers\FileInfoController::class); diff --git a/tests/TestCase.php b/tests/TestCase.php index c0a9fbc..a6ae71f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -25,7 +25,7 @@ class TestCase extends PHPUnitTestCase public function setUp(): void { $this->container = new Container(); - $this->container->set('app.root', $this->testFilesPath); + $this->container->set('base_path', $this->testFilesPath); $this->config = new Config([ 'app' => [ diff --git a/tests/Unit/Bootstrap/FinderComposerTest.php b/tests/Unit/Bootstrap/FinderComposerTest.php index 6e212f5..2608364 100644 --- a/tests/Unit/Bootstrap/FinderComposerTest.php +++ b/tests/Unit/Bootstrap/FinderComposerTest.php @@ -2,7 +2,7 @@ namespace Tests\Unit\Bootstrap; -use App\Bootstrap\FinderComposer; +use App\Bootstrap\FinderProvider; use RuntimeException; use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\SplFileInfo; @@ -12,10 +12,10 @@ class FinderComposerTest extends TestCase { public function test_it_can_compose_the_finder_component(): void { - (new FinderComposer($this->container, $this->config))(); + (new FinderProvider($this->container, $this->config))(); $finder = $this->container->get(Finder::class); - $finder->in($this->container->get('app.root')); + $finder->in($this->container->get('base_path')); $this->assertInstanceOf(Finder::class, $finder); foreach ($finder as $file) { @@ -29,10 +29,10 @@ class FinderComposerTest extends TestCase return $file1->getSize() <=> $file2->getSize(); }); - (new FinderComposer($this->container, $this->config))(); + (new FinderProvider($this->container, $this->config))(); $finder = $this->container->get(Finder::class); - $finder->in($this->container->get('app.root')); + $finder->in($this->container->get('base_path')); $this->assertEquals([ 'alpha.scss', @@ -47,10 +47,10 @@ class FinderComposerTest extends TestCase { $this->config->set('app.reverse_sort', true); - (new FinderComposer($this->container, $this->config))(); + (new FinderProvider($this->container, $this->config))(); $finder = $this->container->get(Finder::class); - $finder->in($this->container->get('app.root')); + $finder->in($this->container->get('base_path')); $this->assertEquals([ 'echo.yaml', @@ -67,7 +67,7 @@ class FinderComposerTest extends TestCase $this->expectException(RuntimeException::class); - (new FinderComposer($this->container, $this->config))(); + (new FinderProvider($this->container, $this->config))(); } protected function getFilesArray(Finder $finder): array