Refactored application structure

This commit is contained in:
Chris Kankiewicz
2020-01-06 14:07:57 -07:00
parent 4ec245cfdf
commit 07a8db681b
22 changed files with 153 additions and 45 deletions

View File

@@ -0,0 +1,71 @@
<?php
namespace App\Bootstrap;
use App\Providers;
use DI\Bridge\Slim\Bridge;
use DI\Container;
use Invoker\CallableResolver;
use PHLAK\Config\Config;
use Slim\App;
use Tightenco\Collect\Support\Collection;
class AppManager
{
/** @const Array of application providers */
protected const PROVIDERS = [
Providers\ConfigProvider::class,
Providers\ParsedownProvider::class,
Providers\FinderProvider::class,
Providers\TwigProvider::class,
];
/** @var Container The applicaiton container */
protected $container;
/** @var Config The application config */
protected $config;
/** @var CallableResolver The callable resolver */
protected $callableResolver;
/**
* Create a new Provider object.
*
* @param \DI\Container $container
*/
public function __construct(Container $container, Config $config, CallableResolver $callableResolver)
{
$this->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)
);
});
}
}

View File

@@ -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'));
}
}

View File

@@ -1,17 +1,17 @@
<?php
namespace App\Bootstrap;
namespace App\Providers;
use DI\Container;
use PHLAK\Config\Config;
class ConfigComposer
class ConfigProvider
{
/** @var Container The applicaiton container */
protected $container;
/**
* Create a new ConfigComposer object.
* Create a new ConfigProvider object.
*
* @param \DI\Container $container
*/
@@ -21,7 +21,7 @@ class ConfigComposer
}
/**
* Set up the Config component.
* Initialize and register the Config component.
*
* @return void
*/

View File

@@ -1,7 +1,8 @@
<?php
namespace App\Bootstrap;
namespace App\Providers;
use App\SortMethods;
use Closure;
use DI\Container;
use PHLAK\Config\Config;
@@ -10,7 +11,7 @@ use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Tightenco\Collect\Support\Collection;
class FinderComposer
class FinderProvider
{
/** @const Application paths to be hidden */
protected const APP_FILES = ['app', 'node_modules', 'vendor', 'index.php'];
@@ -32,7 +33,7 @@ class FinderComposer
protected $container;
/**
* Create a new FinderComposer object.
* Create a new ConfigProvider object.
*
* @param \DI\Container $container
* @param \PHLAK\Config\Config $config
@@ -44,7 +45,7 @@ class FinderComposer
}
/**
* Setup the Finder component.
* Initialize and register the Finder component.
*
* @return void
*/
@@ -94,7 +95,7 @@ class FinderComposer
return $collection->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) {

View File

@@ -0,0 +1,32 @@
<?php
namespace App\Providers;
use DI\Container;
use Parsedown;
class ParsedownProvider
{
/** @var Container The applicaiton container */
protected $container;
/**
* Create a new ParsedownProvider object.
*
* @param \DI\Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* Initialize and register the Parsedown component.
*
* @return void
*/
public function __invoke(): void
{
$this->container->set(Parsedown::class, new Parsedown);
}
}

View File

@@ -1,7 +1,8 @@
<?php
namespace App\Bootstrap;
namespace App\Providers;
use App\ViewFunctions;
use DI\Container;
use PHLAK\Config\Config;
use Slim\Views\Twig;
@@ -9,7 +10,7 @@ use Twig\Extension\CoreExtension;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFunction;
class ViewComposer
class TwigProvider
{
/** @const Constant description */
protected const VIEW_FUNCTIONS = [
@@ -26,7 +27,7 @@ class ViewComposer
protected $config;
/**
* Create a new ViewComposer object.
* Create a new ViewProvider object.
*
* @param \DI\Container $container
* @param \PHLAK\Config\Config $config
@@ -38,7 +39,7 @@ class ViewComposer
}
/**
* Set up the Twig component.
* Initialize and register the Twig component.
*
* @return void
*/

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Bootstrap\SortMethods;
namespace App\SortMethods;
use Symfony\Component\Finder\Finder;

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Bootstrap\SortMethods;
namespace App\SortMethods;
use Symfony\Component\Finder\Finder;

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Bootstrap\SortMethods;
namespace App\SortMethods;
use Symfony\Component\Finder\Finder;

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Bootstrap\SortMethods;
namespace App\SortMethods;
use Symfony\Component\Finder\Finder;

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Bootstrap\SortMethods;
namespace App\SortMethods;
use Symfony\Component\Finder\Finder;

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Bootstrap\SortMethods;
namespace App\SortMethods;
use Symfony\Component\Finder\Finder;

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Bootstrap\SortMethods;
namespace App\SortMethods;
use Symfony\Component\Finder\Finder;

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Bootstrap\ViewFunctions;
namespace App\ViewFunctions;
class Asset extends ViewFunction
{

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Bootstrap\ViewFunctions;
namespace App\ViewFunctions;
class Config extends ViewFunction
{

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Bootstrap\ViewFunctions;
namespace App\ViewFunctions;
use Symfony\Component\Finder\SplFileInfo;

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Bootstrap\ViewFunctions;
namespace App\ViewFunctions;
use Symfony\Component\Finder\SplFileInfo;

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Bootstrap\ViewFunctions;
namespace App\ViewFunctions;
use PHLAK\Config\Config;

View File

@@ -56,4 +56,13 @@ return [
* Default value: true
*/
'render_readme' => Helpers::env('RENDER_README', true),
/**
* Additional providers to be loaded during application initialization.
*
* Default value: []
*/
'providers' => [
// ...
],
];

View File

@@ -1,8 +1,7 @@
<?php
use App\Bootstrap;
use App\Bootstrap\AppManager;
use App\Controllers;
use DI\Bridge\Slim\Bridge;
use DI\Container;
use Dotenv\Dotenv;
@@ -16,15 +15,10 @@ Dotenv::createImmutable(__DIR__)->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);

View File

@@ -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' => [

View File

@@ -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