HummingBirdAnimeClient/app/bootstrap.php

172 lines
5.3 KiB
PHP
Raw Normal View History

2016-10-20 22:09:36 -04:00
<?php declare(strict_types=1);
2016-08-30 10:01:18 -04:00
/**
2017-02-15 16:13:32 -05:00
* Hummingbird Anime List Client
2016-08-30 10:01:18 -04:00
*
2016-12-20 12:58:37 -05:00
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
2016-08-30 10:01:18 -04:00
*
2016-10-20 22:09:36 -04:00
* PHP version 7
2016-08-30 10:01:18 -04:00
*
2017-02-15 16:13:32 -05:00
* @package HummingbirdAnimeClient
2016-08-30 10:01:18 -04:00
* @author Timothy J. Warren <tim@timshomepage.net>
2017-01-11 10:30:53 -05:00
* @copyright 2015 - 2017 Timothy J. Warren
2016-08-30 10:01:18 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2016-12-20 12:58:37 -05:00
* @version 4.0
2016-08-30 10:01:18 -04:00
* @link https://github.com/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient;
2015-06-29 09:46:49 -04:00
use Aura\Html\HelperLocatorFactory;
use Aura\Router\RouterContainer;
2015-10-09 14:34:55 -04:00
use Aura\Session\SessionFactory;
use Aviat\AnimeClient\API\{Kitsu, MAL};
use Aviat\AnimeClient\API\Kitsu\KitsuRequestBuilder;
use Aviat\AnimeClient\API\MAL\MALRequestBuilder;
2016-12-20 12:58:37 -05:00
use Aviat\AnimeClient\Model;
use Aviat\Banker\Pool;
use Aviat\Ion\Config;
2015-09-17 23:11:18 -04:00
use Aviat\Ion\Di\Container;
2016-12-20 12:58:37 -05:00
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use Zend\Diactoros\{Response, ServerRequestFactory};
// -----------------------------------------------------------------------------
// Setup DI container
// -----------------------------------------------------------------------------
2015-10-09 14:34:55 -04:00
return function(array $config_array = []) {
2015-09-17 23:11:18 -04:00
$container = new Container();
2015-11-13 16:31:01 -05:00
// -------------------------------------------------------------------------
// Logging
// -------------------------------------------------------------------------
$app_logger = new Logger('animeclient');
$app_logger->pushHandler(new RotatingFileHandler(__DIR__ . '/logs/app.log', Logger::NOTICE));
$kitsu_request_logger = new Logger('kitsu-request');
2017-02-04 15:18:34 -05:00
$kitsu_request_logger->pushHandler(new RotatingFileHandler(__DIR__ . '/logs/kitsu_request.log', Logger::NOTICE));
$mal_request_logger = new Logger('mal-request');
2017-02-04 15:18:34 -05:00
$mal_request_logger->pushHandler(new RotatingFileHandler(__DIR__ . '/logs/mal_request.log', Logger::NOTICE));
$container->setLogger($app_logger, 'default');
$container->setLogger($kitsu_request_logger, 'kitsu-request');
$container->setLogger($mal_request_logger, 'mal-request');
2015-11-13 16:31:01 -05:00
2015-09-17 23:11:18 -04:00
// -------------------------------------------------------------------------
// Injected Objects
// -------------------------------------------------------------------------
// Create Config Object
2017-01-19 12:49:18 -05:00
$container->set('config', function() use ($config_array) {
return new Config($config_array);
});
2015-09-17 23:11:18 -04:00
// Create Cache Object
$container->set('cache', function($container) {
$logger = $container->getLogger();
$config = $container->get('config')->get('cache');
return new Pool($config, $logger);
});
2015-09-17 23:11:18 -04:00
// Create Aura Router Object
$container->set('aura-router', function() {
return new RouterContainer;
});
2015-09-17 23:11:18 -04:00
// Create Html helper Object
$container->set('html-helper', function($container) {
$html_helper = (new HelperLocatorFactory)->newInstance();
$html_helper->set('menu', function() use ($container) {
$menu_helper = new Helper\Menu();
$menu_helper->setContainer($container);
return $menu_helper;
});
return $html_helper;
});
2015-09-17 23:11:18 -04:00
// Create Request/Response Objects
$container->set('request', function() {
return ServerRequestFactory::fromGlobals(
$_SERVER,
$_GET,
$_POST,
$_COOKIE,
$_FILES
);
});
$container->set('response', function() {
return new Response;
});
2015-09-17 23:11:18 -04:00
// Create session Object
$container->set('session', function() {
return (new SessionFactory())->newInstance($_COOKIE);
});
2015-09-17 23:11:18 -04:00
2015-11-16 19:30:04 -05:00
// Miscellaneous helper methods
$container->set('util', function($container) {
return new Util($container);
});
2015-09-17 23:11:18 -04:00
2015-12-08 16:39:49 -05:00
// Models
$container->set('kitsu-model', function($container) {
$requestBuilder = new KitsuRequestBuilder();
$requestBuilder->setLogger($container->getLogger('kitsu-request'));
$listItem = new Kitsu\ListItem();
$listItem->setContainer($container);
$listItem->setRequestBuilder($requestBuilder);
$model = new Kitsu\Model($listItem);
$model->setContainer($container);
$model->setRequestBuilder($requestBuilder);
$cache = $container->get('cache');
$model->setCache($cache);
return $model;
});
$container->set('mal-model', function($container) {
$requestBuilder = new MALRequestBuilder();
$requestBuilder->setLogger($container->getLogger('mal-request'));
$listItem = new MAL\ListItem();
$listItem->setContainer($container);
$listItem->setRequestBuilder($requestBuilder);
$model = new MAL\Model($listItem);
$model->setContainer($container);
$model->setRequestBuilder($requestBuilder);
return $model;
});
$container->set('api-model', function($container) {
return new Model\API($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);
});
2015-12-08 16:39:49 -05:00
2016-04-08 18:05:52 -04:00
// Miscellaneous Classes
$container->set('auth', function($container) {
return new Kitsu\Auth($container);
});
$container->set('url-generator', function($container) {
return new UrlGenerator($container);
});
2015-12-08 16:39:49 -05:00
2015-09-17 23:11:18 -04:00
// -------------------------------------------------------------------------
2015-10-09 14:34:55 -04:00
// Dispatcher
2015-09-17 23:11:18 -04:00
// -------------------------------------------------------------------------
$container->set('dispatcher', function($container) {
return new Dispatcher($container);
});
2015-09-17 23:11:18 -04:00
return $container;
};
// End of bootstrap.php