Convert Dependency injection bootstrap file to use factory functions, rather than direct instances

This commit is contained in:
Timothy Warren 2016-08-29 14:51:32 -04:00
parent 3335093e87
commit f1a6f99fc4
2 changed files with 61 additions and 33 deletions

View File

@ -38,58 +38,85 @@ return function(array $config_array = []) {
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// Create Config Object // Create Config Object
$config = new Config($config_array); $container->set('config', function() {
$container->set('config', $config); return new Config();
});
$container->setInstance('config', new Config($config_array));
// Create Cache Object // Create Cache Object
$container->set('cache', new CacheManager($config)); $container->set('cache', function($container) {
return new CacheManager($container->get('config'));
});
// Create Aura Router Object // Create Aura Router Object
$container->set('aura-router', new RouterContainer); $container->set('aura-router', function() {
return new RouterContainer;
});
// Create Html helper Object // Create Html helper Object
$container->set('html-helper', function($container) {
$html_helper = (new HelperLocatorFactory)->newInstance(); $html_helper = (new HelperLocatorFactory)->newInstance();
$html_helper->set('menu', function() use ($container) { $html_helper->set('menu', function() use ($container) {
$menu_helper = new Helper\Menu(); $menu_helper = new Helper\Menu();
$menu_helper->setContainer($container); $menu_helper->setContainer($container);
return $menu_helper; return $menu_helper;
}); });
$container->set('html-helper', $html_helper);
return $html_helper;
});
// Create Request/Response Objects // Create Request/Response Objects
$request = ServerRequestFactory::fromGlobals( $container->set('request', function() {
return ServerRequestFactory::fromGlobals(
$_SERVER, $_SERVER,
$_GET, $_GET,
$_POST, $_POST,
$_COOKIE, $_COOKIE,
$_FILES $_FILES
); );
$container->set('request', $request); });
$container->set('response', new Response()); $container->set('response', function() {
return new Response;
});
// Create session Object // Create session Object
$session = (new SessionFactory())->newInstance($_COOKIE); $container->set('session', function() {
$container->set('session', $session); return (new SessionFactory())->newInstance($_COOKIE);
});
// Miscellaneous helper methods // Miscellaneous helper methods
$util = new Util($container); $container->set('util', function($container) {
$container->set('anime-client', $util); return new Util($container);
$container->set('util', $util); });
// Models // Models
$container->set('api-model', new Model\API($container)); $container->set('api-model', function($container) {
$container->set('anime-model', new Model\Anime($container)); return new Model\API($container);
$container->set('manga-model', new Model\Manga($container)); });
$container->set('anime-collection-model', new Model\AnimeCollection($container)); $container->set('anime-model', function($container) {
return new Model\Anime($container);
});
$container->set('manga-model', function($container) {
return new Model\Manga($container);
});
$container->set('anime-collection-model', function($container) {
return new Model\AnimeCollection($container);
});
// Miscellaneous Classes // Miscellaneous Classes
$container->set('auth', new HummingbirdAuth($container)); $container->set('auth', function($container) {
$container->set('url-generator', new UrlGenerator($container)); return new HummingbirdAuth($container);
});
$container->set('url-generator', function($container) {
return new UrlGenerator($container);
});
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// Dispatcher // Dispatcher
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
$container->set('dispatcher', new Dispatcher($container)); $container->set('dispatcher', function($container) {
return new Dispatcher($container);
});
return $container; return $container;
}; };

View File

@ -26,7 +26,8 @@
"phpunit/phpunit": "^5.4", "phpunit/phpunit": "^5.4",
"robmorgan/phinx": "^0.6.4", "robmorgan/phinx": "^0.6.4",
"humbug/humbug": "~1.0@dev", "humbug/humbug": "~1.0@dev",
"consolidation/robo": "~1.0@dev", "consolidation/robo": "~1.0@RC",
"henrikbjorn/lurker": "^1.1.0" "henrikbjorn/lurker": "^1.1.0",
"symfony/var-dumper": "^3.1"
} }
} }