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
|
|
|
*
|
2016-12-20 12:58:37 -05:00
|
|
|
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
|
2016-04-19 13:02:50 -04:00
|
|
|
*
|
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>
|
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
|
2016-04-19 13:02:50 -04:00
|
|
|
* @link https://github.com/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;
|
|
|
|
|
2016-04-19 13:02:50 -04:00
|
|
|
use Aura\Session\SessionFactory;
|
2017-01-19 12:49:18 -05:00
|
|
|
use Aviat\AnimeClient\{
|
|
|
|
AnimeClient,
|
|
|
|
Model,
|
|
|
|
Util
|
|
|
|
};
|
2017-02-14 15:29:13 -05:00
|
|
|
use Aviat\AnimeClient\API\CacheTrait;
|
|
|
|
use Aviat\AnimeClient\API\{Kitsu, MAL};
|
|
|
|
use Aviat\AnimeClient\API\Kitsu\KitsuRequestBuilder;
|
|
|
|
use Aviat\AnimeClient\API\MAL\MALRequestBuilder;
|
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};
|
2016-10-20 22:32:17 -04:00
|
|
|
use ConsoleKit\Command;
|
|
|
|
use ConsoleKit\Widgets\Box;
|
2017-01-27 12:35:28 -05:00
|
|
|
use Monolog\Handler\NullHandler;
|
2017-01-19 12:49:18 -05:00
|
|
|
use Monolog\Logger;
|
2016-04-19 13:02:50 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for console command setup
|
|
|
|
*/
|
2016-04-19 14:51:58 -04:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
protected function echoBox($message)
|
|
|
|
{
|
|
|
|
echo "\n";
|
|
|
|
$box = new Box($this->getConsole(), $message);
|
|
|
|
$box->write();
|
|
|
|
echo "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup the Di container
|
|
|
|
*
|
|
|
|
* @return Container
|
|
|
|
*/
|
2016-04-19 13:02:50 -04:00
|
|
|
protected function setupContainer()
|
|
|
|
{
|
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/");
|
2017-02-10 16:12:02 -05:00
|
|
|
require_once $APPCONF_DIR . '/base_config.php'; // $base_config
|
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
|
|
|
$config_array = array_merge($base_config, $config);
|
|
|
|
|
2017-01-19 12:49:18 -05:00
|
|
|
$di = function ($config_array) use ($APP_DIR) {
|
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-01-27 12:35:28 -05:00
|
|
|
$app_logger->pushHandler(new NullHandler);
|
2017-02-08 15:48:20 -05:00
|
|
|
$kitsu_request_logger = new Logger('kitsu-request');
|
|
|
|
$kitsu_request_logger->pushHandler(new NullHandler);
|
|
|
|
$mal_request_logger = new Logger('mal-request');
|
|
|
|
$mal_request_logger->pushHandler(new NullHandler);
|
2017-01-19 12:49:18 -05:00
|
|
|
$container->setLogger($app_logger, 'default');
|
2017-02-08 15:48:20 -05:00
|
|
|
$container->setLogger($kitsu_request_logger, 'kitsu-request');
|
|
|
|
$container->setLogger($mal_request_logger, 'mal-request');
|
|
|
|
|
2016-04-19 13:02:50 -04:00
|
|
|
// Create Config Object
|
2017-01-19 12:49:18 -05:00
|
|
|
$container->set('config', function() use ($config_array) {
|
|
|
|
return new Config($config_array);
|
2016-08-29 15:36:36 -04:00
|
|
|
});
|
2016-04-19 13:02:50 -04:00
|
|
|
|
|
|
|
// Create Cache Object
|
2016-08-29 15:36:36 -04:00
|
|
|
$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);
|
2016-08-29 15:36:36 -04:00
|
|
|
});
|
2016-04-19 13:02:50 -04:00
|
|
|
|
|
|
|
// Create session Object
|
2016-08-29 15:36:36 -04:00
|
|
|
$container->set('session', function() {
|
|
|
|
return (new SessionFactory())->newInstance($_COOKIE);
|
|
|
|
});
|
2016-04-19 13:02:50 -04:00
|
|
|
|
|
|
|
// Models
|
2017-01-27 12:35:28 -05:00
|
|
|
$container->set('kitsu-model', function($container) {
|
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
|
|
|
});
|
2017-01-27 12:35:28 -05:00
|
|
|
$container->set('mal-model', function($container) {
|
2017-02-14 15:29:13 -05:00
|
|
|
$requestBuilder = new MALRequestBuilder();
|
|
|
|
$requestBuilder->setLogger($container->getLogger('mal-request'));
|
|
|
|
|
2017-02-08 15:48:20 -05:00
|
|
|
$listItem = new MAL\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 MAL\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
|
|
|
return $model;
|
2016-08-29 15:36:36 -04:00
|
|
|
});
|
2017-02-14 15:29:13 -05:00
|
|
|
|
2016-08-29 15:36:36 -04:00
|
|
|
$container->set('util', function($container) {
|
|
|
|
return new Util($container);
|
|
|
|
});
|
2016-04-19 13:02:50 -04:00
|
|
|
|
|
|
|
return $container;
|
|
|
|
};
|
|
|
|
|
|
|
|
return $di($config_array);
|
|
|
|
}
|
|
|
|
}
|