2016-10-20 22:09:36 -04:00
|
|
|
<?php declare(strict_types=1);
|
2016-04-19 13:02:50 -04:00
|
|
|
/**
|
2017-02-15 16:13:32 -05:00
|
|
|
* Hummingbird Anime List Client
|
2016-04-19 13:02:50 -04:00
|
|
|
*
|
2018-08-22 13:48:27 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2016-04-19 13:02:50 -04:00
|
|
|
*
|
2019-12-03 15:17:25 -05:00
|
|
|
* PHP version 7.2
|
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>
|
2019-12-06 09:16:35 -05:00
|
|
|
* @copyright 2015 - 2019 Timothy J. Warren
|
2016-08-30 10:01:18 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2019-12-06 09:16:35 -05:00
|
|
|
* @version 4.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\Command;
|
2016-04-19 13:02:50 -04:00
|
|
|
|
2017-02-08 15:48:20 -05:00
|
|
|
use function Aviat\AnimeClient\loadToml;
|
2018-10-09 18:26:42 -04:00
|
|
|
use function Aviat\AnimeClient\loadTomlFile;
|
2017-02-08 15:48:20 -05:00
|
|
|
|
2018-10-10 16:04:58 -04:00
|
|
|
use Aura\Router\RouterContainer;
|
2016-04-19 13:02:50 -04:00
|
|
|
use Aura\Session\SessionFactory;
|
2018-11-09 10:38:35 -05:00
|
|
|
use Aviat\AnimeClient\{Model, UrlGenerator, Util};
|
|
|
|
use Aviat\AnimeClient\API\{Anilist, CacheTrait, Kitsu};
|
2017-02-14 15:29:13 -05:00
|
|
|
use Aviat\AnimeClient\API\Kitsu\KitsuRequestBuilder;
|
2017-01-19 12:49:18 -05:00
|
|
|
use Aviat\Banker\Pool;
|
2016-10-20 22:32:17 -04:00
|
|
|
use Aviat\Ion\Config;
|
2017-01-27 12:35:28 -05:00
|
|
|
use Aviat\Ion\Di\{Container, ContainerAware};
|
2018-01-31 15:44:48 -05:00
|
|
|
use ConsoleKit\{Command, ConsoleException};
|
2016-10-20 22:32:17 -04:00
|
|
|
use ConsoleKit\Widgets\Box;
|
2017-10-18 19:28:57 -04:00
|
|
|
use Monolog\Handler\RotatingFileHandler;
|
2017-01-19 12:49:18 -05:00
|
|
|
use Monolog\Logger;
|
2018-10-10 16:04:58 -04:00
|
|
|
use Zend\Diactoros\{Response, ServerRequestFactory};
|
2016-04-19 13:02:50 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for console command setup
|
|
|
|
*/
|
2019-12-09 14:34:23 -05:00
|
|
|
abstract class BaseCommand extends Command {
|
2017-01-27 12:35:28 -05:00
|
|
|
use CacheTrait;
|
|
|
|
use ContainerAware;
|
2016-04-19 13:02:50 -04:00
|
|
|
|
2016-04-19 14:51:58 -04:00
|
|
|
/**
|
|
|
|
* Echo text in a box
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-12-09 13:13:31 -05:00
|
|
|
protected function echoBox($message): void
|
2016-04-19 14:51:58 -04:00
|
|
|
{
|
2018-01-31 15:44:48 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
echo "\n";
|
|
|
|
$box = new Box($this->getConsole(), $message);
|
|
|
|
$box->write();
|
|
|
|
echo "\n";
|
|
|
|
}
|
|
|
|
catch (ConsoleException $e)
|
|
|
|
{
|
|
|
|
// oops
|
|
|
|
}
|
2016-04-19 14:51:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup the Di container
|
|
|
|
*
|
|
|
|
* @return Container
|
|
|
|
*/
|
2018-01-31 15:44:48 -05:00
|
|
|
protected function setupContainer(): Container
|
2016-04-19 13:02:50 -04:00
|
|
|
{
|
2017-01-19 12:49:18 -05:00
|
|
|
$APP_DIR = realpath(__DIR__ . '/../../app');
|
2017-02-10 16:12:02 -05:00
|
|
|
$APPCONF_DIR = realpath("{$APP_DIR}/appConf/");
|
2017-01-19 12:49:18 -05:00
|
|
|
$CONF_DIR = realpath("{$APP_DIR}/config/");
|
2018-10-09 18:10:20 -04:00
|
|
|
$baseConfig = require $APPCONF_DIR . '/base_config.php';
|
2016-04-19 13:02:50 -04:00
|
|
|
|
2017-02-08 15:48:20 -05:00
|
|
|
$config = loadToml($CONF_DIR);
|
2016-04-19 13:02:50 -04:00
|
|
|
|
2018-10-09 18:10:20 -04:00
|
|
|
$overrideFile = $CONF_DIR . '/admin-override.toml';
|
|
|
|
$overrideConfig = file_exists($overrideFile)
|
|
|
|
? loadTomlFile($overrideFile)
|
|
|
|
: [];
|
|
|
|
|
|
|
|
$configArray = array_replace_recursive($baseConfig, $config, $overrideConfig);
|
|
|
|
|
2019-12-09 13:13:31 -05:00
|
|
|
$di = static function ($configArray) use ($APP_DIR): Container {
|
2016-04-19 13:02:50 -04:00
|
|
|
$container = new Container();
|
2017-02-08 15:48:20 -05:00
|
|
|
|
2017-01-27 12:35:28 -05:00
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
// Logging
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
|
2017-01-19 12:49:18 -05:00
|
|
|
$app_logger = new Logger('animeclient');
|
2017-10-18 19:28:57 -04:00
|
|
|
$app_logger->pushHandler(new RotatingFileHandler($APP_DIR . '/logs/app-cli.log', Logger::NOTICE));
|
2019-12-09 13:13:31 -05:00
|
|
|
|
2017-02-08 15:48:20 -05:00
|
|
|
$kitsu_request_logger = new Logger('kitsu-request');
|
2017-10-18 19:28:57 -04:00
|
|
|
$kitsu_request_logger->pushHandler(new RotatingFileHandler($APP_DIR . '/logs/kitsu_request-cli.log', Logger::NOTICE));
|
2019-12-09 13:13:31 -05:00
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
$anilistRequestLogger = new Logger('anilist-request');
|
|
|
|
$anilistRequestLogger->pushHandler(new RotatingFileHandler($APP_DIR . '/logs/anilist_request-cli.log', Logger::NOTICE));
|
2019-12-09 13:13:31 -05:00
|
|
|
|
2018-01-16 14:58:07 -05:00
|
|
|
$container->setLogger($app_logger);
|
2018-09-26 22:31:04 -04:00
|
|
|
$container->setLogger($anilistRequestLogger, 'anilist-request');
|
2017-02-08 15:48:20 -05:00
|
|
|
$container->setLogger($kitsu_request_logger, 'kitsu-request');
|
|
|
|
|
2016-04-19 13:02:50 -04:00
|
|
|
// Create Config Object
|
2019-12-09 13:13:31 -05:00
|
|
|
$container->set('config', static function() use ($configArray): Config {
|
2018-10-09 18:10:20 -04:00
|
|
|
return new Config($configArray);
|
2016-08-29 15:36:36 -04:00
|
|
|
});
|
2016-04-19 13:02:50 -04:00
|
|
|
|
|
|
|
// Create Cache Object
|
2019-05-08 13:19:03 -04:00
|
|
|
$container->set('cache', static function($container) {
|
2017-01-19 12:49:18 -05:00
|
|
|
$logger = $container->getLogger();
|
|
|
|
$config = $container->get('config')->get('cache');
|
|
|
|
return new Pool($config, $logger);
|
2016-08-29 15:36:36 -04:00
|
|
|
});
|
2016-04-19 13:02:50 -04:00
|
|
|
|
2018-10-10 16:04:58 -04:00
|
|
|
// Create Aura Router Object
|
2019-05-08 13:19:03 -04:00
|
|
|
$container->set('aura-router', static function() {
|
2018-10-10 16:04:58 -04:00
|
|
|
return new RouterContainer;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create Request/Response Objects
|
2019-05-08 13:19:03 -04:00
|
|
|
$container->set('request', static function() {
|
2018-10-10 16:04:58 -04:00
|
|
|
return ServerRequestFactory::fromGlobals(
|
|
|
|
$_SERVER,
|
|
|
|
$_GET,
|
|
|
|
$_POST,
|
|
|
|
$_COOKIE,
|
|
|
|
$_FILES
|
|
|
|
);
|
|
|
|
});
|
2019-12-09 13:13:31 -05:00
|
|
|
$container->set('response', static function(): Response {
|
2018-10-10 16:04:58 -04:00
|
|
|
return new Response;
|
|
|
|
});
|
|
|
|
|
2016-04-19 13:02:50 -04:00
|
|
|
// Create session Object
|
2019-05-08 13:19:03 -04:00
|
|
|
$container->set('session', static function() {
|
2016-08-29 15:36:36 -04:00
|
|
|
return (new SessionFactory())->newInstance($_COOKIE);
|
|
|
|
});
|
2016-04-19 13:02:50 -04:00
|
|
|
|
|
|
|
// Models
|
2019-12-09 13:13:31 -05:00
|
|
|
$container->set('kitsu-model', static function($container): Kitsu\Model {
|
2017-02-14 15:29:13 -05:00
|
|
|
$requestBuilder = new KitsuRequestBuilder();
|
|
|
|
$requestBuilder->setLogger($container->getLogger('kitsu-request'));
|
|
|
|
|
2017-02-10 16:12:02 -05:00
|
|
|
$listItem = new Kitsu\ListItem();
|
2017-01-27 12:35:28 -05:00
|
|
|
$listItem->setContainer($container);
|
2017-02-14 15:29:13 -05:00
|
|
|
$listItem->setRequestBuilder($requestBuilder);
|
|
|
|
|
2017-02-08 15:48:20 -05:00
|
|
|
$model = new Kitsu\Model($listItem);
|
2017-01-27 12:35:28 -05:00
|
|
|
$model->setContainer($container);
|
2017-02-14 15:29:13 -05:00
|
|
|
$model->setRequestBuilder($requestBuilder);
|
|
|
|
|
2017-01-27 12:35:28 -05:00
|
|
|
$cache = $container->get('cache');
|
|
|
|
$model->setCache($cache);
|
|
|
|
return $model;
|
2016-08-29 15:36:36 -04:00
|
|
|
});
|
2019-12-09 13:13:31 -05:00
|
|
|
$container->set('anilist-model', static function ($container): Anilist\Model {
|
2018-09-26 22:31:04 -04:00
|
|
|
$requestBuilder = new Anilist\AnilistRequestBuilder();
|
|
|
|
$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;
|
|
|
|
});
|
2019-12-09 13:13:31 -05:00
|
|
|
$container->set('settings-model', static function($container): Model\Settings {
|
2018-10-10 16:04:58 -04:00
|
|
|
$model = new Model\Settings($container->get('config'));
|
|
|
|
$model->setContainer($container);
|
|
|
|
return $model;
|
|
|
|
});
|
|
|
|
|
2019-12-09 13:13:31 -05:00
|
|
|
$container->set('auth', static function($container): Kitsu\Auth {
|
2018-10-10 16:04:58 -04:00
|
|
|
return new Kitsu\Auth($container);
|
|
|
|
});
|
|
|
|
|
2019-12-09 13:13:31 -05:00
|
|
|
$container->set('url-generator', static function($container): UrlGenerator {
|
2018-10-10 16:04:58 -04:00
|
|
|
return new UrlGenerator($container);
|
|
|
|
});
|
2017-02-14 15:29:13 -05:00
|
|
|
|
2019-12-09 13:13:31 -05:00
|
|
|
$container->set('util', static function($container): Util {
|
2016-08-29 15:36:36 -04:00
|
|
|
return new Util($container);
|
|
|
|
});
|
2016-04-19 13:02:50 -04:00
|
|
|
|
|
|
|
return $container;
|
|
|
|
};
|
|
|
|
|
2018-10-09 18:10:20 -04:00
|
|
|
return $di($configArray);
|
2016-04-19 13:02:50 -04:00
|
|
|
}
|
|
|
|
}
|