HummingBirdAnimeClient/src/AnimeClient/Command/BaseCommand.php

225 lines
6.5 KiB
PHP
Raw Normal View History

2016-10-20 22:09:36 -04:00
<?php declare(strict_types=1);
/**
2017-02-15 16:13:32 -05:00
* Hummingbird Anime List Client
*
2018-08-22 13:48:27 -04:00
* An API client for Kitsu to manage anime and manga watch lists
*
* 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>
2020-01-08 15:39:49 -05:00
* @copyright 2015 - 2020 Timothy J. Warren
2016-08-30 10:01:18 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2020-08-04 09:30:21 -04:00
* @version 5.1
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\Command;
use function Aviat\AnimeClient\loadToml;
2018-10-09 18:26:42 -04:00
use function Aviat\AnimeClient\loadTomlFile;
use Aura\Router\RouterContainer;
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};
2020-05-08 19:15:21 -04:00
use Aviat\Banker\Teller;
2016-10-20 22:32:17 -04:00
use Aviat\Ion\Config;
use Aviat\Ion\Di\{Container, ContainerInterface, ContainerAware};
use ConsoleKit\{Colors, Command, ConsoleException};
2016-10-20 22:32:17 -04:00
use ConsoleKit\Widgets\Box;
2020-04-10 20:01:46 -04:00
use Laminas\Diactoros\{Response, ServerRequestFactory};
2017-10-18 19:28:57 -04:00
use Monolog\Handler\RotatingFileHandler;
2017-01-19 12:49:18 -05:00
use Monolog\Logger;
/**
* 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;
/**
* Echo text in a box
*
* @param string|array $message
* @param string|int|null $fgColor
* @param string|int|null $bgColor
* @return void
*/
public function echoBox($message, $fgColor = NULL, $bgColor = NULL): void
{
if (is_array($message))
2018-01-31 15:44:48 -05:00
{
$message = implode("\n", $message);
}
2020-08-17 18:08:58 -04:00
// color message
$message = Colors::colorize($message, $fgColor, $bgColor);
2020-08-17 18:08:58 -04:00
// create the box
$box = new Box($this->getConsole(), $message);
2020-08-17 18:08:58 -04:00
$box->write();
2020-08-17 18:08:58 -04:00
echo "\n";
}
public function echo(string $message): void
{
$this->_line($message);
}
public function echoSuccess(string $message): void
{
$this->_line($message, Colors::GREEN | Colors::BOLD, Colors::BLACK);
}
public function echoWarning(string $message): void
{
$this->_line($message, Colors::YELLOW | Colors::BOLD, Colors::BLACK);
}
public function echoWarningBox(string $message): void
{
$this->echoBox($message, Colors::YELLOW | Colors::BOLD, Colors::BLACK);
}
public function echoError(string $message): void
{
$this->_line($message, Colors::RED | Colors::BOLD, Colors::BLACK);
}
public function echoErrorBox(string $message): void
{
$this->echoBox($message, Colors::RED | Colors::BOLD, Colors::BLACK);
}
public function clearLine(): void
{
$this->getConsole()->write("\r\e[2K");
}
/**
* Setup the Di container
*
* @return Containerinterface
*/
public function setupContainer(): ContainerInterface
{
$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/");
$baseConfig = require $APPCONF_DIR . '/base_config.php';
$config = loadToml($CONF_DIR);
$overrideFile = $CONF_DIR . '/admin-override.toml';
$overrideConfig = file_exists($overrideFile)
? loadTomlFile($overrideFile)
: [];
$configArray = array_replace_recursive($baseConfig, $config, $overrideConfig);
2020-05-08 19:15:21 -04:00
return $this->_di($configArray, $APP_DIR);
}
private function _line(string $message, $fgColor = NULL, $bgColor = NULL): void
{
$message = Colors::colorize($message, $fgColor, $bgColor);
$this->getConsole()->writeln($message);
}
2020-05-08 19:15:21 -04:00
private function _di(array $configArray, string $APP_DIR): ContainerInterface
{
$container = new Container();
// -------------------------------------------------------------------------
// Logging
// -------------------------------------------------------------------------
$app_logger = new Logger('animeclient');
$app_logger->pushHandler(new RotatingFileHandler($APP_DIR . '/logs/app-cli.log', Logger::WARNING));
2020-05-08 19:15:21 -04:00
$kitsu_request_logger = new Logger('kitsu-request');
$kitsu_request_logger->pushHandler(new RotatingFileHandler($APP_DIR . '/logs/kitsu_request-cli.log', Logger::WARNING));
2020-05-08 19:15:21 -04:00
$anilistRequestLogger = new Logger('anilist-request');
$anilistRequestLogger->pushHandler(new RotatingFileHandler($APP_DIR . '/logs/anilist_request-cli.log', Logger::WARNING));
2020-05-08 19:15:21 -04:00
$container->setLogger($app_logger);
$container->setLogger($anilistRequestLogger, 'anilist-request');
$container->setLogger($kitsu_request_logger, 'kitsu-request');
// Create Config Object
$container->set('config', fn () => new Config($configArray));
// Create Cache Object
$container->set('cache', static function($container) {
$logger = $container->getLogger();
$config = $container->get('config')->get('cache');
return new Teller($config, $logger);
});
// Create Aura Router Object
$container->set('aura-router', fn () => new RouterContainer);
// Create Request/Response Objects
$container->set('request', fn () => ServerRequestFactory::fromGlobals(
$_SERVER,
$_GET,
$_POST,
$_COOKIE,
$_FILES
));
$container->set('response', fn () => new Response);
// Create session Object
$container->set('session', fn () => (new SessionFactory())->newInstance($_COOKIE));
// Models
$container->set('kitsu-model', static function($container): Kitsu\Model {
2020-08-06 09:39:12 -04:00
$requestBuilder = new Kitsu\RequestBuilder($container);
2020-05-08 19:15:21 -04:00
$requestBuilder->setLogger($container->getLogger('kitsu-request'));
$listItem = new Kitsu\ListItem();
$listItem->setContainer($container);
$listItem->setRequestBuilder($requestBuilder);
2020-05-08 19:15:21 -04:00
$model = new Kitsu\Model($listItem);
$model->setContainer($container);
$model->setRequestBuilder($requestBuilder);
2020-05-08 19:15:21 -04:00
$cache = $container->get('cache');
$model->setCache($cache);
return $model;
});
$container->set('anilist-model', static function ($container): Anilist\Model {
2020-08-06 09:39:12 -04:00
$requestBuilder = new Anilist\RequestBuilder($container);
2020-05-08 19:15:21 -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;
});
$container->set('settings-model', static function($container): Model\Settings {
$model = new Model\Settings($container->get('config'));
$model->setContainer($container);
return $model;
});
$container->set('auth', fn ($container) => new Kitsu\Auth($container));
$container->set('url-generator', fn ($container) => new UrlGenerator($container));
$container->set('util', fn ($container) => new Util($container));
return $container;
}
}