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
|
|
|
*
|
2021-02-04 11:57:01 -05:00
|
|
|
* PHP version 8
|
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\Command;
|
2016-04-19 13:02:50 -04:00
|
|
|
|
2018-10-10 16:04:58 -04:00
|
|
|
use Aura\Router\RouterContainer;
|
2022-03-03 17:26:09 -05:00
|
|
|
|
2016-04-19 13:02:50 -04:00
|
|
|
use Aura\Session\SessionFactory;
|
2018-11-09 10:38:35 -05:00
|
|
|
use Aviat\AnimeClient\API\{Anilist, CacheTrait, Kitsu};
|
2022-03-03 17:26:09 -05:00
|
|
|
|
|
|
|
use Aviat\AnimeClient\{Model, UrlGenerator, Util};
|
2020-05-08 19:15:21 -04:00
|
|
|
use Aviat\Banker\Teller;
|
2016-10-20 22:32:17 -04:00
|
|
|
use Aviat\Ion\Config;
|
2022-03-03 17:26:09 -05:00
|
|
|
use Aviat\Ion\Di\{Container, ContainerAware, ContainerInterface};
|
2016-10-20 22:32:17 -04:00
|
|
|
use ConsoleKit\Widgets\Box;
|
2022-03-03 17:26:09 -05:00
|
|
|
use ConsoleKit\{Colors, Command, ConsoleException};
|
2020-04-10 20:01:46 -04:00
|
|
|
use Laminas\Diactoros\{Response, ServerRequestFactory};
|
2022-03-03 17:26:09 -05:00
|
|
|
use Monolog\Formatter\JsonFormatter;
|
2017-10-18 19:28:57 -04:00
|
|
|
use Monolog\Handler\RotatingFileHandler;
|
2017-01-19 12:49:18 -05:00
|
|
|
use Monolog\Logger;
|
2022-03-03 17:26:09 -05:00
|
|
|
use function Aviat\AnimeClient\{loadConfig, loadTomlFile};
|
|
|
|
use function Aviat\Ion\_dir;
|
|
|
|
use const Aviat\AnimeClient\SRC_DIR;
|
2016-04-19 13:02:50 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for console command setup
|
|
|
|
*/
|
2022-03-03 17:26:09 -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
|
|
|
|
*/
|
2022-03-03 17:26:09 -05:00
|
|
|
public function echoBox(string|array $message, string|int|NULL $fgColor = NULL, string|int|NULL $bgColor = NULL): void
|
2016-04-19 14:51:58 -04:00
|
|
|
{
|
2020-05-04 17:13:03 -04:00
|
|
|
if (is_array($message))
|
2018-01-31 15:44:48 -05:00
|
|
|
{
|
2020-05-04 17:13:03 -04:00
|
|
|
$message = implode("\n", $message);
|
|
|
|
}
|
2020-05-01 17:08:20 -04:00
|
|
|
|
2021-02-12 11:14:45 -05:00
|
|
|
if ($fgColor !== NULL)
|
|
|
|
{
|
2022-03-03 17:26:09 -05:00
|
|
|
$fgColor = (int) $fgColor;
|
2021-02-12 11:14:45 -05:00
|
|
|
}
|
2022-03-03 13:25:10 -05:00
|
|
|
|
2021-02-12 11:14:45 -05:00
|
|
|
if ($bgColor !== NULL)
|
|
|
|
{
|
2022-03-03 17:26:09 -05:00
|
|
|
$bgColor = (int) $bgColor;
|
2021-02-12 11:14:45 -05:00
|
|
|
}
|
|
|
|
|
2021-02-18 12:48:59 -05:00
|
|
|
// Colorize the CLI output
|
|
|
|
// the documented type for the function is wrong
|
|
|
|
// @phpstan-ignore-next-line
|
2020-08-17 18:08:58 -04:00
|
|
|
$message = Colors::colorize($message, $fgColor, $bgColor);
|
2020-05-01 17:08:20 -04:00
|
|
|
|
2020-08-17 18:08:58 -04:00
|
|
|
// create the box
|
|
|
|
$box = new Box($this->getConsole(), $message);
|
2020-05-01 17:08:20 -04:00
|
|
|
|
2020-08-17 18:08:58 -04:00
|
|
|
$box->write();
|
2020-05-01 17:08:20 -04:00
|
|
|
|
2020-08-17 18:08:58 -04:00
|
|
|
echo "\n";
|
2016-04-19 14:51:58 -04:00
|
|
|
}
|
|
|
|
|
2020-05-01 17:08:20 -04:00
|
|
|
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);
|
|
|
|
}
|
2020-05-04 17:13:03 -04:00
|
|
|
|
|
|
|
public function clearLine(): void
|
|
|
|
{
|
|
|
|
$this->getConsole()->write("\r\e[2K");
|
|
|
|
}
|
2020-05-01 17:08:20 -04:00
|
|
|
|
2016-04-19 14:51:58 -04:00
|
|
|
/**
|
|
|
|
* Setup the Di container
|
|
|
|
*/
|
2020-05-01 17:08:20 -04:00
|
|
|
public function setupContainer(): ContainerInterface
|
2016-04-19 13:02:50 -04:00
|
|
|
{
|
2022-03-03 13:25:10 -05:00
|
|
|
$APP_DIR = _dir(dirname(SRC_DIR, 2), 'app');
|
2021-02-04 12:27:52 -05:00
|
|
|
$APPCONF_DIR = _dir($APP_DIR, 'appConf');
|
|
|
|
$CONF_DIR = _dir($APP_DIR, 'config');
|
2022-03-03 17:26:09 -05:00
|
|
|
$baseConfig = require _dir($APPCONF_DIR, 'base_config.php');
|
2016-04-19 13:02:50 -04:00
|
|
|
|
2020-12-11 15:37:55 -05:00
|
|
|
$config = loadConfig($CONF_DIR);
|
2016-04-19 13:02:50 -04:00
|
|
|
|
2021-02-03 09:46:36 -05:00
|
|
|
$overrideFile = _dir($CONF_DIR, 'admin-override.toml');
|
2018-10-09 18:10:20 -04:00
|
|
|
$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);
|
2016-04-19 13:02:50 -04:00
|
|
|
}
|
2020-05-01 17:08:20 -04:00
|
|
|
|
2022-03-03 17:26:09 -05:00
|
|
|
private function _line(string $message, int|string|NULL $fgColor = NULL, int|string|NULL $bgColor = NULL): void
|
2020-05-01 17:08:20 -04:00
|
|
|
{
|
2021-02-12 11:14:45 -05:00
|
|
|
if ($fgColor !== NULL)
|
|
|
|
{
|
2022-03-03 17:26:09 -05:00
|
|
|
$fgColor = (int) $fgColor;
|
2021-02-12 11:14:45 -05:00
|
|
|
}
|
2022-03-03 13:25:10 -05:00
|
|
|
|
2021-02-12 11:14:45 -05:00
|
|
|
if ($bgColor !== NULL)
|
|
|
|
{
|
2022-03-03 17:26:09 -05:00
|
|
|
$bgColor = (int) $bgColor;
|
2021-02-12 11:14:45 -05:00
|
|
|
}
|
2021-02-12 13:09:57 -05:00
|
|
|
|
2021-02-22 15:39:03 -05:00
|
|
|
// Colorize the CLI output
|
|
|
|
// the documented type for the function is wrong
|
|
|
|
// @phpstan-ignore-next-line
|
2020-05-01 17:08:20 -04:00
|
|
|
$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
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
|
2020-08-18 16:59:08 -04:00
|
|
|
$appLogger = new Logger('animeclient');
|
|
|
|
$appLogger->pushHandler(new RotatingFileHandler($APP_DIR . '/logs/app-cli.log', 2, Logger::WARNING));
|
2022-03-03 13:25:10 -05:00
|
|
|
|
2020-08-18 16:59:08 -04:00
|
|
|
$container->setLogger($appLogger);
|
2020-05-08 19:15:21 -04:00
|
|
|
|
2020-08-21 19:26:54 -04:00
|
|
|
foreach (['kitsu-request', 'anilist-request', 'anilist-request-cli', 'kitsu-request-cli'] as $channel)
|
2020-08-18 16:59:08 -04:00
|
|
|
{
|
|
|
|
$logger = new Logger($channel);
|
2022-03-03 17:26:09 -05:00
|
|
|
$handler = new RotatingFileHandler("{$APP_DIR}/logs/{$channel}.log", 2, Logger::WARNING);
|
2020-08-18 16:59:08 -04:00
|
|
|
$handler->setFormatter(new JsonFormatter());
|
|
|
|
$logger->pushHandler($handler);
|
2020-05-08 19:15:21 -04:00
|
|
|
|
2020-08-18 16:59:08 -04:00
|
|
|
$container->setLogger($logger, $channel);
|
|
|
|
}
|
2020-05-08 19:15:21 -04:00
|
|
|
|
|
|
|
// Create Config Object
|
2022-03-03 17:26:09 -05:00
|
|
|
$container->set('config', static fn () => new Config($configArray));
|
2020-05-08 19:15:21 -04:00
|
|
|
|
|
|
|
// Create Cache Object
|
2022-03-03 17:26:09 -05:00
|
|
|
$container->set('cache', static function ($container): Teller {
|
2020-05-08 19:15:21 -04:00
|
|
|
$logger = $container->getLogger();
|
|
|
|
$config = $container->get('config')->get('cache');
|
2022-03-03 17:26:09 -05:00
|
|
|
|
2020-05-08 19:15:21 -04:00
|
|
|
return new Teller($config, $logger);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create Aura Router Object
|
2022-03-03 17:26:09 -05:00
|
|
|
$container->set('aura-router', static fn () => new RouterContainer());
|
2020-05-08 19:15:21 -04:00
|
|
|
|
|
|
|
// Create Request/Response Objects
|
2022-03-03 17:26:09 -05:00
|
|
|
$container->set('request', static fn () => ServerRequestFactory::fromGlobals(
|
2020-12-11 14:26:54 -05:00
|
|
|
$GLOBALS['_SERVER'],
|
2020-05-08 19:15:21 -04:00
|
|
|
$_GET,
|
|
|
|
$_POST,
|
|
|
|
$_COOKIE,
|
|
|
|
$_FILES
|
|
|
|
));
|
2022-03-03 17:26:09 -05:00
|
|
|
$container->set('response', static fn () => new Response());
|
2020-05-08 19:15:21 -04:00
|
|
|
|
|
|
|
// Create session Object
|
2022-03-03 17:26:09 -05:00
|
|
|
$container->set('session', static fn () => (new SessionFactory())->newInstance($_COOKIE));
|
2020-05-08 19:15:21 -04:00
|
|
|
|
|
|
|
// Models
|
2022-03-03 17:26:09 -05:00
|
|
|
$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);
|
2020-07-31 19:03:27 -04:00
|
|
|
$listItem->setRequestBuilder($requestBuilder);
|
2020-05-08 19:15:21 -04:00
|
|
|
|
|
|
|
$model = new Kitsu\Model($listItem);
|
|
|
|
$model->setContainer($container);
|
2020-07-31 19:03:27 -04:00
|
|
|
$model->setRequestBuilder($requestBuilder);
|
2020-05-08 19:15:21 -04:00
|
|
|
|
|
|
|
$cache = $container->get('cache');
|
|
|
|
$model->setCache($cache);
|
2022-03-03 17:26:09 -05:00
|
|
|
|
2020-05-08 19:15:21 -04:00
|
|
|
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;
|
|
|
|
});
|
2022-03-03 17:26:09 -05:00
|
|
|
$container->set('settings-model', static function ($container): Model\Settings {
|
|
|
|
$model = new Model\Settings($container->get('config'));
|
2020-05-08 19:15:21 -04:00
|
|
|
$model->setContainer($container);
|
2022-03-03 17:26:09 -05:00
|
|
|
|
2020-05-08 19:15:21 -04:00
|
|
|
return $model;
|
|
|
|
});
|
|
|
|
|
2022-03-03 17:26:09 -05:00
|
|
|
$container->set('auth', static fn ($container) => new Kitsu\Auth($container));
|
2020-05-08 19:15:21 -04:00
|
|
|
|
2022-03-03 17:26:09 -05:00
|
|
|
$container->set('url-generator', static fn ($container) => new UrlGenerator($container));
|
2020-05-08 19:15:21 -04:00
|
|
|
|
2022-03-03 17:26:09 -05:00
|
|
|
$container->set('util', static fn ($container) => new Util($container));
|
2020-05-08 19:15:21 -04:00
|
|
|
|
|
|
|
return $container;
|
|
|
|
}
|
2022-03-03 17:26:09 -05:00
|
|
|
}
|