Extracted several class array constants to container definitions

This commit is contained in:
Chris Kankiewicz
2020-05-12 10:47:33 -07:00
parent c0e329cb30
commit a7999070eb
5 changed files with 50 additions and 49 deletions

View File

@@ -1,6 +1,9 @@
<?php
use App\Factories;
use App\Middlewares;
use App\SortMethods;
use App\ViewFunctions;
return [
/** Path definitions */
@@ -13,6 +16,45 @@ return [
'views_path' => DI\string('{app_path}/views'),
'icons_config' => DI\string('{config_path}/icons.php'),
/** Array of application files (to be hidden) */
'app_files' => ['app', 'index.php', '.hidden'],
/** Array of application middlewares */
'middlewares' => [
Middlewares\WhoopsMiddleware::class
],
/** Array of sort options mapped to their respective classes */
'sort_methods' => [
'accessed' => SortMethods\Accessed::class,
'changed' => SortMethods\Changed::class,
'modified' => SortMethods\Modified::class,
'name' => SortMethods\Name::class,
'natural' => SortMethods\Natural::class,
'type' => SortMethods\Type::class,
],
/** Array of available translation languages */
'translations' => [
'de', 'en', 'es', 'fr', 'id', 'it', 'kr', 'nl',
'pl', 'pt-BR', 'ro', 'ru', 'zh-CN', 'zh-TW'
],
/** Array of view functions */
'view_functions' => [
ViewFunctions\Asset::class,
ViewFunctions\Breadcrumbs::class,
ViewFunctions\Config::class,
ViewFunctions\FileUrl::class,
ViewFunctions\Icon::class,
ViewFunctions\Markdown::class,
ViewFunctions\ParentUrl::class,
ViewFunctions\SizeForHumans::class,
ViewFunctions\Translate::class,
ViewFunctions\Url::class,
ViewFunctions\ZipUrl::class,
],
/** Container definitions */
Symfony\Component\Finder\Finder::class => DI\factory(Factories\FinderFactory::class),
Symfony\Contracts\Translation\TranslatorInterface::class => DI\factory(Factories\TranslationFactory::class),

View File

@@ -10,11 +10,6 @@ use Tightenco\Collect\Support\Collection;
class MiddlewareManager
{
/** @const Array of application middlewares */
protected const MIDDLEWARES = [
Middlewares\WhoopsMiddleware::class
];
/** @var App The application */
protected $app;
@@ -40,7 +35,7 @@ class MiddlewareManager
*/
public function __invoke()
{
Collection::make(self::MIDDLEWARES)->each(
Collection::make($this->container->get('middlewares'))->each(
function (string $middleware): void {
$this->app->add($middleware);
}

View File

@@ -2,7 +2,6 @@
namespace App\Factories;
use App\SortMethods;
use Closure;
use DI\Container;
use PHLAK\Utilities\Glob;
@@ -13,19 +12,6 @@ use Tightenco\Collect\Support\Collection;
class FinderFactory
{
/** @const Application paths to be hidden */
protected const APP_FILES = ['app', 'index.php', '.hidden'];
/** @const Array of sort options mapped to their respective methods */
public const SORT_METHODS = [
'accessed' => SortMethods\Accessed::class,
'changed' => SortMethods\Changed::class,
'modified' => SortMethods\Modified::class,
'name' => SortMethods\Name::class,
'natural' => SortMethods\Natural::class,
'type' => SortMethods\Type::class,
];
/** @var Container The application container */
protected $container;
@@ -59,11 +45,11 @@ class FinderFactory
if ($sortOrder instanceof Closure) {
$finder->sort($sortOrder);
} else {
if (! array_key_exists($sortOrder, self::SORT_METHODS)) {
if (! array_key_exists($sortOrder, $this->container->get('sort_methods'))) {
throw new RuntimeException("Invalid sort option '{$sortOrder}'");
}
$this->container->call(self::SORT_METHODS[$sortOrder], [$finder]);
$this->container->call($this->container->get('sort_methods')[$sortOrder], [$finder]);
}
if ($this->container->get('reverse_sort')) {
@@ -86,8 +72,8 @@ class FinderFactory
return $collection->merge(
file($this->container->get('hidden_files_list'), FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)
);
})->when($this->container->get('hide_app_files'), static function (Collection $collection) {
return $collection->merge(self::APP_FILES);
})->when($this->container->get('hide_app_files'), function (Collection $collection) {
return $collection->merge($this->container->get('app_files'));
})->unique();
}

View File

@@ -11,12 +11,6 @@ use Tightenco\Collect\Support\Collection;
class TranslationFactory
{
/** @const Available translation languages */
protected const LANGUAGES = [
'de', 'en', 'es', 'fr', 'id', 'it', 'kr', 'nl',
'pl', 'pt-BR', 'ro', 'ru', 'zh-CN', 'zh-TW'
];
/** @var Container The applicaiton container */
protected $container;
@@ -39,14 +33,14 @@ class TranslationFactory
{
$language = $this->container->get('language');
if (! in_array($language, self::LANGUAGES)) {
if (! in_array($language, $this->container->get('translations'))) {
throw new RuntimeException("Invalid language option '{$language}'");
}
$translator = new Translator($language);
$translator->addLoader('yaml', new YamlFileLoader());
Collection::make(self::LANGUAGES)->each(
Collection::make($this->container->get('translations'))->each(
function (string $language) use ($translator): void {
$resource = sprintf($this->container->get('translations_path') . '/%s.yaml', $language);
$translator->addResource('yaml', $resource, $language);

View File

@@ -2,7 +2,6 @@
namespace App\Factories;
use App\ViewFunctions;
use DI\Container;
use Invoker\CallableResolver;
use Slim\Views\Twig;
@@ -12,21 +11,6 @@ use Twig\TwigFunction;
class TwigFactory
{
/** @const Constant description */
protected const VIEW_FUNCTIONS = [
ViewFunctions\Asset::class,
ViewFunctions\Breadcrumbs::class,
ViewFunctions\Config::class,
ViewFunctions\FileUrl::class,
ViewFunctions\Icon::class,
ViewFunctions\Markdown::class,
ViewFunctions\ParentUrl::class,
ViewFunctions\SizeForHumans::class,
ViewFunctions\Translate::class,
ViewFunctions\Url::class,
ViewFunctions\ZipUrl::class,
];
/** @var Container The application container */
protected $container;
@@ -70,7 +54,7 @@ class TwigFactory
$this->container->get('timezone')
);
foreach (self::VIEW_FUNCTIONS as $function) {
foreach ($this->container->get('view_functions') as $function) {
$function = $this->callableResolver->resolve($function);
$twig->getEnvironment()->addFunction(