|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Hummingbird Anime Client |
|
4
|
|
|
* |
|
5
|
|
|
* An API client for Hummingbird to manage anime and manga watch lists |
|
6
|
|
|
* |
|
7
|
|
|
* @package HummingbirdAnimeClient |
|
8
|
|
|
* @author Timothy J. Warren |
|
9
|
|
|
* @copyright Copyright (c) 2015 - 2016 |
|
10
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient |
|
11
|
|
|
* @license MIT |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Aviat\AnimeClient\Command; |
|
15
|
|
|
|
|
16
|
|
|
use Aura\Session\SessionFactory; |
|
17
|
|
|
use ConsoleKit\Command; |
|
18
|
|
|
use ConsoleKit\Widgets\Box; |
|
19
|
|
|
|
|
20
|
|
|
use Aviat\Ion\Di\Container; |
|
21
|
|
|
use Aviat\Ion\Cache\CacheManager; |
|
22
|
|
|
use Aviat\AnimeClient\Config; |
|
23
|
|
|
use Aviat\AnimeClient\AnimeClient; |
|
24
|
|
|
use Aviat\AnimeClient\Auth\HummingbirdAuth; |
|
25
|
|
|
use Aviat\AnimeClient\Model; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Base class for console command setup |
|
29
|
|
|
*/ |
|
30
|
|
|
class BaseCommand extends Command { |
|
31
|
|
|
use \Aviat\Ion\Di\ContainerAware; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Echo text in a box |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $message |
|
37
|
|
|
* @return void |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function echoBox($message) |
|
40
|
|
|
{ |
|
41
|
|
|
echo "\n"; |
|
42
|
|
|
$box = new Box($this->getConsole(), $message); |
|
43
|
|
|
$box->write(); |
|
44
|
|
|
echo "\n"; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Setup the Di container |
|
49
|
|
|
* |
|
50
|
|
|
* @return Container |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function setupContainer() |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
$CONF_DIR = __DIR__ . '/../../../../app/config/'; |
|
55
|
|
|
require_once $CONF_DIR . '/base_config.php'; // $base_config |
|
56
|
|
|
|
|
57
|
|
|
$config = AnimeClient::load_toml($CONF_DIR); |
|
58
|
|
|
$config_array = array_merge($base_config, $config); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
$di = function ($config_array) { |
|
61
|
|
|
$container = new Container(); |
|
62
|
|
|
|
|
63
|
|
|
// Create Config Object |
|
64
|
|
|
$config = new Config($config_array); |
|
65
|
|
|
$container->set('config', $config); |
|
66
|
|
|
|
|
67
|
|
|
// Create Cache Object |
|
68
|
|
|
$container->set('cache', new CacheManager($container)); |
|
69
|
|
|
|
|
70
|
|
|
// Create session Object |
|
71
|
|
|
$session = (new SessionFactory())->newInstance($_COOKIE); |
|
72
|
|
|
$container->set('session', $session); |
|
73
|
|
|
|
|
74
|
|
|
// Models |
|
75
|
|
|
$container->set('api-model', new Model\API($container)); |
|
76
|
|
|
$container->set('anime-model', new Model\Anime($container)); |
|
77
|
|
|
$container->set('manga-model', new Model\Manga($container)); |
|
78
|
|
|
|
|
79
|
|
|
$container->set('auth', new HummingbirdAuth($container)); |
|
80
|
|
|
|
|
81
|
|
|
return $container; |
|
82
|
|
|
}; |
|
83
|
|
|
|
|
84
|
|
|
return $di($config_array); |
|
85
|
|
|
} |
|
86
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: