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
|
|
|
*
|
2018-08-22 13:48:27 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2016-08-30 10:01:18 -04:00
|
|
|
*
|
2020-12-10 17:06:50 -05:00
|
|
|
* PHP version 7.4+
|
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>
|
2021-01-13 01:52:03 -05:00
|
|
|
* @copyright 2015 - 2021 Timothy J. Warren
|
2016-08-30 10:01:18 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-12-10 17:06:50 -05:00
|
|
|
* @version 5.2
|
2017-03-07 20:53:58 -05:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
2017-01-11 10:34:24 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient;
|
2015-06-29 09:46:49 -04:00
|
|
|
|
2015-10-05 16:54:25 -04:00
|
|
|
use Aura\Html\HelperLocatorFactory;
|
2016-02-16 16:28:44 -05:00
|
|
|
use Aura\Router\RouterContainer;
|
2015-10-09 14:34:55 -04:00
|
|
|
use Aura\Session\SessionFactory;
|
2020-08-06 09:39:12 -04:00
|
|
|
use Aviat\AnimeClient\API\{Anilist, Kitsu};
|
2020-08-21 12:30:01 -04:00
|
|
|
use Aviat\AnimeClient\Component;
|
2016-12-20 12:58:37 -05:00
|
|
|
use Aviat\AnimeClient\Model;
|
2020-05-08 19:15:21 -04:00
|
|
|
use Aviat\Banker\Teller;
|
2016-07-28 10:44:13 -04:00
|
|
|
use Aviat\Ion\Config;
|
2015-09-17 23:11:18 -04:00
|
|
|
use Aviat\Ion\Di\Container;
|
2020-04-10 20:01:46 -04:00
|
|
|
use Aviat\Ion\Di\ContainerInterface;
|
2020-07-31 16:22:32 -04:00
|
|
|
use Laminas\Diactoros\ServerRequestFactory;
|
2020-08-18 16:59:08 -04:00
|
|
|
use Monolog\Formatter\JsonFormatter;
|
2016-12-20 12:58:37 -05:00
|
|
|
use Monolog\Handler\RotatingFileHandler;
|
|
|
|
use Monolog\Logger;
|
2020-08-18 16:59:08 -04:00
|
|
|
use Psr\SimpleCache\CacheInterface;
|
2015-09-14 10:54:50 -04:00
|
|
|
|
2021-02-03 09:45:18 -05:00
|
|
|
use function Aviat\Ion\_dir;
|
|
|
|
|
2020-08-21 13:07:00 -04:00
|
|
|
if ( ! defined('APP_DIR'))
|
|
|
|
{
|
|
|
|
define('APP_DIR', __DIR__);
|
2021-02-03 09:45:18 -05:00
|
|
|
define('ROOT_DIR', dirname(APP_DIR));
|
|
|
|
define('TEMPLATE_DIR', _dir(APP_DIR, 'templates'));
|
2020-08-21 13:07:00 -04:00
|
|
|
}
|
2020-08-21 12:30:01 -04:00
|
|
|
|
2015-09-14 10:54:50 -04:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Setup DI container
|
|
|
|
// -----------------------------------------------------------------------------
|
2020-04-10 20:01:46 -04:00
|
|
|
return static function (array $configArray = []): Container {
|
2015-09-17 23:11:18 -04:00
|
|
|
$container = new Container();
|
|
|
|
|
2015-11-13 16:31:01 -05:00
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
// Logging
|
|
|
|
// -------------------------------------------------------------------------
|
2021-02-03 09:45:18 -05:00
|
|
|
$LOG_DIR = _dir(APP_DIR, 'logs');
|
2015-11-13 16:31:01 -05:00
|
|
|
|
2017-03-03 11:33:32 -05:00
|
|
|
$appLogger = new Logger('animeclient');
|
2021-02-03 09:45:18 -05:00
|
|
|
$appLogger->pushHandler(new RotatingFileHandler(_dir($LOG_DIR, 'app.log'), 2, Logger::WARNING));
|
2020-08-18 16:59:08 -04:00
|
|
|
$container->setLogger($appLogger);
|
2019-12-09 13:13:31 -05:00
|
|
|
|
2020-08-18 16:59:08 -04:00
|
|
|
foreach (['anilist-request', 'kitsu-request', 'kitsu-graphql'] as $channel)
|
|
|
|
{
|
|
|
|
$logger = new Logger($channel);
|
2021-02-03 09:45:18 -05:00
|
|
|
$handler = new RotatingFileHandler(_dir($LOG_DIR, "{$channel}.log"), 2, Logger::WARNING);
|
2020-08-18 16:59:08 -04:00
|
|
|
$handler->setFormatter(new JsonFormatter());
|
|
|
|
$logger->pushHandler($handler);
|
2019-12-09 13:13:31 -05:00
|
|
|
|
2020-08-18 16:59:08 -04:00
|
|
|
$container->setLogger($logger, $channel);
|
|
|
|
}
|
2015-11-13 16:31:01 -05:00
|
|
|
|
2015-09-17 23:11:18 -04:00
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
// Injected Objects
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Create Config Object
|
2020-12-11 14:26:54 -05:00
|
|
|
$container->set('config', static fn () => new Config($configArray));
|
2015-09-17 23:11:18 -04:00
|
|
|
|
2016-04-12 13:41:03 -04:00
|
|
|
// Create Cache Object
|
2020-05-08 19:15:21 -04:00
|
|
|
$container->set('cache', static function(ContainerInterface $container): CacheInterface {
|
2016-12-22 21:36:23 -05:00
|
|
|
$logger = $container->getLogger();
|
|
|
|
$config = $container->get('config')->get('cache');
|
2020-05-08 19:15:21 -04:00
|
|
|
return new Teller($config, $logger);
|
2016-08-29 14:51:32 -04:00
|
|
|
});
|
2016-04-12 13:41:03 -04:00
|
|
|
|
2015-09-17 23:11:18 -04:00
|
|
|
// Create Aura Router Object
|
2020-12-11 14:26:54 -05:00
|
|
|
$container->set('aura-router', static fn() => new RouterContainer);
|
2015-09-17 23:11:18 -04:00
|
|
|
|
2020-08-21 12:30:01 -04:00
|
|
|
// Create Html helpers
|
2020-04-10 20:01:46 -04:00
|
|
|
$container->set('html-helper', static function(ContainerInterface $container) {
|
2017-03-03 11:33:32 -05:00
|
|
|
$htmlHelper = (new HelperLocatorFactory)->newInstance();
|
2020-08-21 12:30:01 -04:00
|
|
|
$helpers = [
|
|
|
|
'menu' => Helper\Menu::class,
|
|
|
|
'field' => Helper\Form::class,
|
|
|
|
'picture' => Helper\Picture::class,
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($helpers as $name => $class)
|
|
|
|
{
|
|
|
|
$htmlHelper->set($name, static function() use ($class, $container) {
|
|
|
|
$helper = new $class;
|
|
|
|
$helper->setContainer($container);
|
|
|
|
return $helper;
|
|
|
|
});
|
|
|
|
}
|
2016-08-29 14:51:32 -04:00
|
|
|
|
2017-03-03 11:33:32 -05:00
|
|
|
return $htmlHelper;
|
2015-10-05 16:54:25 -04:00
|
|
|
});
|
|
|
|
|
2020-08-21 12:30:01 -04:00
|
|
|
// Create Component helpers
|
2020-08-26 17:26:42 -04:00
|
|
|
$container->set('component-helper', static function (ContainerInterface $container) {
|
2020-08-21 12:30:01 -04:00
|
|
|
$helper = (new HelperLocatorFactory)->newInstance();
|
|
|
|
$components = [
|
2020-08-26 17:26:42 -04:00
|
|
|
'animeCover' => Component\AnimeCover::class,
|
|
|
|
'mangaCover' => Component\MangaCover::class,
|
2020-08-21 12:30:01 -04:00
|
|
|
'character' => Component\Character::class,
|
|
|
|
'media' => Component\Media::class,
|
|
|
|
'tabs' => Component\Tabs::class,
|
|
|
|
'verticalTabs' => Component\VerticalTabs::class,
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($components as $name => $componentClass)
|
|
|
|
{
|
2020-08-26 17:26:42 -04:00
|
|
|
$helper->set($name, static function () use ($container, $componentClass) {
|
|
|
|
$helper = new $componentClass;
|
|
|
|
$helper->setContainer($container);
|
|
|
|
return $helper;
|
|
|
|
});
|
2020-08-21 12:30:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $helper;
|
|
|
|
});
|
|
|
|
|
2020-07-31 16:22:32 -04:00
|
|
|
// Create Request Object
|
2020-12-11 14:26:54 -05:00
|
|
|
$container->set('request', static fn () => ServerRequestFactory::fromGlobals(
|
|
|
|
$GLOBALS['_SERVER'],
|
2020-04-10 20:01:46 -04:00
|
|
|
$_GET,
|
|
|
|
$_POST,
|
|
|
|
$_COOKIE,
|
|
|
|
$_FILES
|
|
|
|
));
|
2015-09-17 23:11:18 -04:00
|
|
|
|
|
|
|
// Create session Object
|
2020-12-11 14:26:54 -05:00
|
|
|
$container->set('session', static fn () => (new SessionFactory())->newInstance($_COOKIE));
|
2015-09-17 23:11:18 -04:00
|
|
|
|
2015-11-16 19:30:04 -05:00
|
|
|
// Miscellaneous helper methods
|
2020-12-11 14:26:54 -05:00
|
|
|
$container->set('util', static fn ($container) => new Util($container));
|
2015-09-17 23:11:18 -04:00
|
|
|
|
2015-12-08 16:39:49 -05:00
|
|
|
// Models
|
2020-04-10 20:01:46 -04:00
|
|
|
$container->set('kitsu-model', static function(ContainerInterface $container): Kitsu\Model {
|
2020-08-06 09:39:12 -04:00
|
|
|
$requestBuilder = new Kitsu\RequestBuilder($container);
|
2017-02-07 13:11:42 -05:00
|
|
|
$requestBuilder->setLogger($container->getLogger('kitsu-request'));
|
2017-02-08 15:48:20 -05:00
|
|
|
|
|
|
|
$listItem = new Kitsu\ListItem();
|
2017-01-06 21:39:01 -05:00
|
|
|
$listItem->setContainer($container);
|
2020-07-31 19:03:27 -04:00
|
|
|
$listItem->setRequestBuilder($requestBuilder);
|
2017-02-08 15:48:20 -05:00
|
|
|
|
|
|
|
$model = new Kitsu\Model($listItem);
|
2016-12-22 21:36:23 -05:00
|
|
|
$model->setContainer($container);
|
2020-07-31 19:03:27 -04:00
|
|
|
$model->setRequestBuilder($requestBuilder);
|
2017-02-08 15:48:20 -05:00
|
|
|
|
2017-01-13 16:53:56 -05:00
|
|
|
$cache = $container->get('cache');
|
|
|
|
$model->setCache($cache);
|
|
|
|
return $model;
|
|
|
|
});
|
2020-04-10 20:01:46 -04:00
|
|
|
$container->set('anilist-model', static function(ContainerInterface $container): Anilist\Model {
|
2020-08-06 09:39:12 -04:00
|
|
|
$requestBuilder = new Anilist\RequestBuilder($container);
|
2018-08-15 08:51:37 -04:00
|
|
|
$requestBuilder->setLogger($container->getLogger('anilist-request'));
|
|
|
|
|
|
|
|
$listItem = new Anilist\ListItem();
|
|
|
|
$listItem->setContainer($container);
|
|
|
|
$listItem->setRequestBuilder($requestBuilder);
|
|
|
|
|
|
|
|
$model = new Anilist\Model($listItem);
|
|
|
|
$model->setContainer($container);
|
|
|
|
$model->setRequestBuilder($requestBuilder);
|
|
|
|
|
|
|
|
return $model;
|
|
|
|
});
|
2020-12-11 14:26:54 -05:00
|
|
|
$container->set('anime-model', static fn ($container) => new Model\Anime($container));
|
|
|
|
$container->set('manga-model', static fn ($container) => new Model\Manga($container));
|
|
|
|
$container->set('anime-collection-model', static fn ($container) => new Model\AnimeCollection($container));
|
|
|
|
$container->set('manga-collection-model', static fn ($container) => new Model\MangaCollection($container));
|
2019-05-08 08:55:58 -04:00
|
|
|
$container->set('settings-model', static function($container) {
|
2020-04-10 20:01:46 -04:00
|
|
|
$model = new Model\Settings($container->get('config'));
|
2018-10-05 21:32:15 -04:00
|
|
|
$model->setContainer($container);
|
|
|
|
return $model;
|
|
|
|
});
|
2015-12-08 16:39:49 -05:00
|
|
|
|
2016-04-08 18:05:52 -04:00
|
|
|
// Miscellaneous Classes
|
2020-12-11 14:26:54 -05:00
|
|
|
$container->set('auth', static fn ($container) => new Kitsu\Auth($container));
|
|
|
|
$container->set('url-generator', static fn ($container) => 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
|
|
|
// -------------------------------------------------------------------------
|
2020-12-11 14:26:54 -05:00
|
|
|
$container->set('dispatcher', static fn ($container) => new Dispatcher($container));
|
2015-09-17 23:11:18 -04:00
|
|
|
|
|
|
|
return $container;
|
|
|
|
};
|
|
|
|
|
2015-10-05 16:54:25 -04:00
|
|
|
// End of bootstrap.php
|