HummingBirdAnimeClient/src/Command/BaseCommand.php

115 lines
2.8 KiB
PHP
Raw Normal View History

2016-10-20 22:09:36 -04:00
<?php declare(strict_types=1);
/**
2016-12-20 12:58:37 -05:00
* Anime List Client
*
2016-12-20 12:58:37 -05:00
* An API client for Kitsu and MyAnimeList 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
*
2016-12-20 12:58:37 -05:00
* @package AnimeListClient
2016-08-30 10:01:18 -04:00
* @author Timothy J. Warren <tim@timshomepage.net>
2017-01-11 10:30:53 -05:00
* @copyright 2015 - 2017 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://github.com/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\Command;
use Aura\Session\SessionFactory;
2017-01-19 12:49:18 -05:00
use Aviat\AnimeClient\{
AnimeClient,
Model,
Util
};
use Aviat\AnimeClient\Auth\HummingbirdAuth;
2017-01-19 12:49:18 -05:00
use Aviat\Banker\Pool;
2016-10-20 22:32:17 -04:00
use Aviat\Ion\Config;
use Aviat\Ion\Di\Container;
use ConsoleKit\Command;
use ConsoleKit\Widgets\Box;
2017-01-19 12:49:18 -05:00
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
/**
* Base class for console command setup
*/
class BaseCommand extends Command {
use \Aviat\Ion\Di\ContainerAware;
/**
* Echo text in a box
*
* @param string $message
* @return void
*/
protected function echoBox($message)
{
echo "\n";
$box = new Box($this->getConsole(), $message);
$box->write();
echo "\n";
}
/**
* Setup the Di container
*
* @return Container
*/
protected function setupContainer()
{
2017-01-19 12:49:18 -05:00
$APP_DIR = realpath(__DIR__ . '/../../app');
$CONF_DIR = realpath("{$APP_DIR}/config/");
require_once $CONF_DIR . '/base_config.php'; // $base_config
2016-12-20 12:58:37 -05:00
$config = AnimeClient::loadToml($CONF_DIR);
$config_array = array_merge($base_config, $config);
2017-01-19 12:49:18 -05:00
$di = function ($config_array) use ($APP_DIR) {
$container = new Container();
2017-01-19 12:49:18 -05:00
$app_logger = new Logger('animeclient');
$app_logger->pushHandler(new RotatingFileHandler("{$APP_DIR}/logs/app.log", Logger::NOTICE));
$container->setLogger($app_logger, 'default');
// 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
$container->set('api-model', function($container) {
return new Model\API($container);
});
$container->set('anime-model', function($container) {
return new Model\Anime($container);
});
$container->set('manga-model', function($container) {
return new Model\Manga($container);
});
$container->set('auth', function($container) {
return new HummingbirdAuth($container);
});
$container->set('util', function($container) {
return new Util($container);
});
return $container;
};
return $di($config_array);
}
}