1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of Cli for CodeIgniter |
4
|
|
|
* |
5
|
|
|
* @author Kenji Suzuki <https://github.com/kenjis> |
6
|
|
|
* @license MIT License |
7
|
|
|
* @copyright 2015 Kenji Suzuki |
8
|
|
|
* @link https://github.com/kenjis/codeigniter-cli |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Kenjis\CodeIgniter_Cli\_Config; |
12
|
|
|
|
13
|
|
|
use Aura\Di\Config; |
14
|
|
|
use Aura\Di\Container; |
15
|
|
|
use Kenjis\CodeIgniter_Cli\UserConfig; |
16
|
|
|
|
17
|
|
|
class Common extends Config |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array list of built-in commands |
21
|
|
|
*/ |
22
|
|
|
private $commands = [ |
23
|
|
|
'Seed', 'Migrate', 'Generate', 'Run', |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
private $user_command_paths = []; |
27
|
|
|
|
28
|
|
|
public function define(Container $di) |
29
|
|
|
{ |
30
|
|
|
$di->set('aura/project-kernel:logger', $di->newInstance('Monolog\Logger')); |
31
|
|
|
|
32
|
|
|
/* @var $ci \CI_Controller */ |
33
|
|
|
$ci =& get_instance(); |
34
|
|
|
|
35
|
|
|
// register built-in command classes |
36
|
|
|
foreach ($this->commands as $command) { |
37
|
|
|
$class = 'Kenjis\CodeIgniter_Cli\Command\\' . $command; |
38
|
|
|
$di->params[$class] = [ |
39
|
|
|
'context' => $di->lazyGet('aura/cli-kernel:context'), |
40
|
|
|
'stdio' => $di->lazyGet('aura/cli-kernel:stdio'), |
41
|
|
|
'ci' => $ci, |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$seeder_path = APPPATH . 'database/seeds/'; |
46
|
|
|
$di->setter['Kenjis\CodeIgniter_Cli\Command\Seed']['setSeederPath'] |
47
|
|
|
= $seeder_path; |
48
|
|
|
|
49
|
|
|
$this->user_command_paths = [ APPPATH . 'commands/' ]; |
50
|
|
|
// register user command classes |
51
|
|
|
UserConfig::registerCommandClasses($di, $ci, $this->user_command_paths); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function modify(Container $di) |
55
|
|
|
{ |
56
|
|
|
$this->modifyLogger($di); |
57
|
|
|
$this->modifyCliDispatcherAndHelp($di); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function modifyLogger(Container $di) |
61
|
|
|
{ |
62
|
|
|
$project = $di->get('project'); |
63
|
|
|
$mode = $project->getMode(); |
64
|
|
|
$file = $project->getPath("tmp/log/{$mode}.log"); |
65
|
|
|
|
66
|
|
|
$logger = $di->get('aura/project-kernel:logger'); |
67
|
|
|
$logger->pushHandler($di->newInstance( |
68
|
|
|
'Monolog\Handler\StreamHandler', |
69
|
|
|
array( |
70
|
|
|
'stream' => $file, |
71
|
|
|
) |
72
|
|
|
)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function modifyCliDispatcherAndHelp(Container $di) |
76
|
|
|
{ |
77
|
|
|
// $context = $di->get('aura/cli-kernel:context'); |
78
|
|
|
// $stdio = $di->get('aura/cli-kernel:stdio'); |
79
|
|
|
// $logger = $di->get('aura/project-kernel:logger'); |
80
|
|
|
$dispatcher = $di->get('aura/cli-kernel:dispatcher'); |
81
|
|
|
$help_service = $di->get('aura/cli-kernel:help_service'); |
82
|
|
|
|
83
|
|
|
// register built-in commands |
84
|
|
|
foreach ($this->commands as $command) { |
85
|
|
|
$class = 'Kenjis\CodeIgniter_Cli\Command\\' . $command; |
86
|
|
|
$command_name = strtolower($command); |
87
|
|
|
$dispatcher->setObject( |
88
|
|
|
$command_name, |
89
|
|
|
$di->lazyNew($class) |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$help_class = 'Kenjis\CodeIgniter_Cli\Command\\' . $command . 'Help'; |
93
|
|
|
$help_service->set( |
94
|
|
|
$command_name, |
95
|
|
|
$di->lazyNew($help_class) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// register user commands |
100
|
|
|
UserConfig::registerCommands( |
101
|
|
|
$di, $dispatcher, $help_service, $this->user_command_paths |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|