Use static closures in bootstrap

This commit is contained in:
Timothy Warren 2019-05-08 08:55:58 -04:00
parent 765fc9de42
commit 2c915188a8
2 changed files with 26 additions and 24 deletions

View File

@ -35,7 +35,7 @@ use Zend\Diactoros\{Response, ServerRequestFactory};
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Setup DI container // Setup DI container
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
return function ($configArray = []) { return static function ($configArray = []) {
$container = new Container(); $container = new Container();
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
@ -57,36 +57,38 @@ return function ($configArray = []) {
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// Create Config Object // Create Config Object
$container->set('config', function() use ($configArray) { $container->set('config', static function() use ($configArray) {
return new Config($configArray); return new Config($configArray);
}); });
// Create Cache Object // Create Cache Object
$container->set('cache', function($container) { $container->set('cache', static function($container) {
$logger = $container->getLogger(); $logger = $container->getLogger();
$config = $container->get('config')->get('cache'); $config = $container->get('config')->get('cache');
return new Pool($config, $logger); return new Pool($config, $logger);
}); });
// Create List Cache
// Create Aura Router Object // Create Aura Router Object
$container->set('aura-router', function() { $container->set('aura-router', static function() {
return new RouterContainer; return new RouterContainer;
}); });
// Create Html helper Object // Create Html helper Object
$container->set('html-helper', function($container) { $container->set('html-helper', static function($container) {
$htmlHelper = (new HelperLocatorFactory)->newInstance(); $htmlHelper = (new HelperLocatorFactory)->newInstance();
$htmlHelper->set('menu', function() use ($container) { $htmlHelper->set('menu', static function() use ($container) {
$menuHelper = new Helper\Menu(); $menuHelper = new Helper\Menu();
$menuHelper->setContainer($container); $menuHelper->setContainer($container);
return $menuHelper; return $menuHelper;
}); });
$htmlHelper->set('field', function() use ($container) { $htmlHelper->set('field', static function() use ($container) {
$formHelper = new Helper\Form(); $formHelper = new Helper\Form();
$formHelper->setContainer($container); $formHelper->setContainer($container);
return $formHelper; return $formHelper;
}); });
$htmlHelper->set('picture', function() use ($container) { $htmlHelper->set('picture', static function() use ($container) {
$pictureHelper = new Helper\Picture(); $pictureHelper = new Helper\Picture();
$pictureHelper->setContainer($container); $pictureHelper->setContainer($container);
return $pictureHelper; return $pictureHelper;
@ -96,7 +98,7 @@ return function ($configArray = []) {
}); });
// Create Request/Response Objects // Create Request/Response Objects
$container->set('request', function() { $container->set('request', static function() {
return ServerRequestFactory::fromGlobals( return ServerRequestFactory::fromGlobals(
$_SERVER, $_SERVER,
$_GET, $_GET,
@ -105,22 +107,22 @@ return function ($configArray = []) {
$_FILES $_FILES
); );
}); });
$container->set('response', function() { $container->set('response', static function() {
return new Response; return new Response;
}); });
// Create session Object // Create session Object
$container->set('session', function() { $container->set('session', static function() {
return (new SessionFactory())->newInstance($_COOKIE); return (new SessionFactory())->newInstance($_COOKIE);
}); });
// Miscellaneous helper methods // Miscellaneous helper methods
$container->set('util', function($container) { $container->set('util', static function($container) {
return new Util($container); return new Util($container);
}); });
// Models // Models
$container->set('kitsu-model', function($container) { $container->set('kitsu-model', static function($container) {
$requestBuilder = new KitsuRequestBuilder(); $requestBuilder = new KitsuRequestBuilder();
$requestBuilder->setLogger($container->getLogger('kitsu-request')); $requestBuilder->setLogger($container->getLogger('kitsu-request'));
@ -136,7 +138,7 @@ return function ($configArray = []) {
$model->setCache($cache); $model->setCache($cache);
return $model; return $model;
}); });
$container->set('anilist-model', function($container) { $container->set('anilist-model', static function($container) {
$requestBuilder = new Anilist\AnilistRequestBuilder(); $requestBuilder = new Anilist\AnilistRequestBuilder();
$requestBuilder->setLogger($container->getLogger('anilist-request')); $requestBuilder->setLogger($container->getLogger('anilist-request'));
@ -151,39 +153,39 @@ return function ($configArray = []) {
return $model; return $model;
}); });
$container->set('api-model', function($container) { $container->set('api-model', static function($container) {
return new Model\API($container); return new Model\API($container);
}); });
$container->set('anime-model', function($container) { $container->set('anime-model', static function($container) {
return new Model\Anime($container); return new Model\Anime($container);
}); });
$container->set('manga-model', function($container) { $container->set('manga-model', static function($container) {
return new Model\Manga($container); return new Model\Manga($container);
}); });
$container->set('anime-collection-model', function($container) { $container->set('anime-collection-model', static function($container) {
return new Model\AnimeCollection($container); return new Model\AnimeCollection($container);
}); });
$container->set('manga-collection-model', function($container) { $container->set('manga-collection-model', static function($container) {
return new Model\MangaCollection($container); return new Model\MangaCollection($container);
}); });
$container->set('settings-model', function($container) { $container->set('settings-model', static function($container) {
$model = new Model\Settings($container->get('config')); $model = new Model\Settings($container->get('config'));
$model->setContainer($container); $model->setContainer($container);
return $model; return $model;
}); });
// Miscellaneous Classes // Miscellaneous Classes
$container->set('auth', function($container) { $container->set('auth', static function($container) {
return new Kitsu\Auth($container); return new Kitsu\Auth($container);
}); });
$container->set('url-generator', function($container) { $container->set('url-generator', static function($container) {
return new UrlGenerator($container); return new UrlGenerator($container);
}); });
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// Dispatcher // Dispatcher
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
$container->set('dispatcher', function($container) { $container->set('dispatcher', static function($container) {
return new Dispatcher($container); return new Dispatcher($container);
}); });

View File

@ -32,7 +32,7 @@ use Aviat\AnimeClient\API\Kitsu;
<?php foreach ($casting as $sid => $series): ?> <?php foreach ($casting as $sid => $series): ?>
<article class="media"> <article class="media">
<?php <?php
$mediaType = (in_array($type, ['anime', 'manga'])) ? $type : 'anime'; $mediaType = in_array($type, ['anime', 'manga'], TRUE) ? $type : 'anime';
$link = $url->generate("{$mediaType}.details", ['id' => $series['slug']]); $link = $url->generate("{$mediaType}.details", ['id' => $series['slug']]);
$titles = Kitsu::filterTitles($series); $titles = Kitsu::filterTitles($series);
?> ?>