HummingBirdAnimeClient/src/Command/BaseCommand.php

135 lines
3.6 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
*
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>
2018-01-15 14:43:15 -05:00
* @copyright 2015 - 2018 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
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\Command;
use function Aviat\AnimeClient\loadToml;
use Aura\Session\SessionFactory;
2018-01-16 14:58:07 -05:00
use Aviat\AnimeClient\Util;
use Aviat\AnimeClient\API\CacheTrait;
2018-08-22 13:43:04 -04:00
use Aviat\AnimeClient\API\Kitsu;
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;
/**
* Base class for console command setup
*/
class BaseCommand extends Command {
2017-01-27 12:35:28 -05:00
use CacheTrait;
use ContainerAware;
/**
* Echo text in a box
*
* @param string $message
* @return void
*/
protected function echoBox($message)
{
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
}
}
/**
* Setup the Di container
*
* @return Container
*/
2018-01-31 15:44:48 -05:00
protected function setupContainer(): Container
{
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-01-31 15:44:48 -05:00
$base_config = require $APPCONF_DIR . '/base_config.php';
$config = loadToml($CONF_DIR);
$config_array = array_merge($base_config, $config);
2017-10-18 19:28:57 -04:00
$di = function ($config_array) use ($APP_DIR) {
$container = new Container();
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));
$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));
2018-01-16 14:58:07 -05:00
$container->setLogger($app_logger);
$container->setLogger($kitsu_request_logger, 'kitsu-request');
// Create Config Object
2017-01-19 12:49:18 -05:00
$container->set('config', function() use ($config_array) {
return new Config($config_array);
});
// Create Cache Object
$container->set('cache', function($container) {
2017-01-19 12:49:18 -05:00
$logger = $container->getLogger();
$config = $container->get('config')->get('cache');
return new Pool($config, $logger);
});
// Create session Object
$container->set('session', function() {
return (new SessionFactory())->newInstance($_COOKIE);
});
// Models
2017-01-27 12:35:28 -05:00
$container->set('kitsu-model', function($container) {
$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);
$listItem->setRequestBuilder($requestBuilder);
$model = new Kitsu\Model($listItem);
2017-01-27 12:35:28 -05:00
$model->setContainer($container);
$model->setRequestBuilder($requestBuilder);
2017-01-27 12:35:28 -05:00
$cache = $container->get('cache');
$model->setCache($cache);
return $model;
});
$container->set('util', function($container) {
return new Util($container);
});
return $container;
};
return $di($config_array);
}
}