1
|
|
|
<?php |
2
|
|
|
namespace Samurai; |
3
|
|
|
|
4
|
|
|
use Balloon\Factory\BalloonFactory; |
5
|
|
|
use PHPGit\Git; |
6
|
|
|
use Pimple\Container; |
7
|
|
|
use Puppy\Config\Config; |
8
|
|
|
use Samurai\Alias\AliasCommand; |
9
|
|
|
use Samurai\Module\Factory\ModuleManagerFactory; |
10
|
|
|
use Samurai\Module\ModuleCommand; |
11
|
|
|
use Samurai\Alias\AliasManagerFactory; |
12
|
|
|
use Samurai\Module\ModuleProcedure; |
13
|
|
|
use Samurai\Module\ModulesSorter; |
14
|
|
|
use Samurai\Project\Composer\Composer; |
15
|
|
|
use Samurai\Project\NewCommand; |
16
|
|
|
use Samurai\Project\Project; |
17
|
|
|
use Symfony\Component\Console\Application; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
20
|
|
|
use TRex\Cli\Executor; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Samurai |
24
|
|
|
* main console |
25
|
|
|
* |
26
|
|
|
* @package Samurai |
27
|
|
|
* @author Raphaël Lefebvre <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class Samurai |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var Application |
33
|
|
|
*/ |
34
|
|
|
private $application; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Container |
38
|
|
|
*/ |
39
|
|
|
private $services; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Application $application |
43
|
|
|
* @param Container $services |
44
|
|
|
*/ |
45
|
15 |
|
public function __construct(Application $application, Container $services = null) |
46
|
|
|
{ |
47
|
15 |
|
$application->setName('Samurai console'); |
48
|
15 |
|
$application->setVersion('0.0.0'); |
49
|
|
|
|
50
|
15 |
|
$this->setApplication($application); |
51
|
15 |
|
$this->setServices($services ? : $this->buildServices()); |
52
|
|
|
|
53
|
15 |
|
$this->initCommands(); |
54
|
15 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param InputInterface $input |
58
|
|
|
* @param OutputInterface $output |
59
|
|
|
* @return int |
60
|
|
|
* @throws \Exception |
61
|
|
|
*/ |
62
|
1 |
|
public function run(InputInterface $input = null, OutputInterface $output = null) |
63
|
|
|
{ |
64
|
1 |
|
return $this->getApplication($input, $output)->run(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Getter of $application |
69
|
|
|
* |
70
|
|
|
* @return Application |
71
|
|
|
*/ |
72
|
15 |
|
public function getApplication() |
73
|
|
|
{ |
74
|
15 |
|
return $this->application; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Getter of $services |
79
|
|
|
* |
80
|
|
|
* @return Container |
81
|
|
|
*/ |
82
|
15 |
|
public function getServices() |
83
|
|
|
{ |
84
|
15 |
|
return $this->services; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Setter of $services |
89
|
|
|
* |
90
|
|
|
* @param Container $services |
91
|
|
|
*/ |
92
|
15 |
|
public function setServices(Container $services) |
93
|
|
|
{ |
94
|
15 |
|
$this->services = $services; |
95
|
15 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* |
99
|
|
|
*/ |
100
|
15 |
|
private function initCommands() |
101
|
|
|
{ |
102
|
15 |
|
$this->getApplication()->add(new NewCommand($this->getServices())); |
103
|
15 |
|
$this->getApplication()->add(new AliasCommand($this->getServices())); |
104
|
15 |
|
$this->getApplication()->add(new ModuleCommand($this->getServices())); |
105
|
15 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Setter of $application |
109
|
|
|
* |
110
|
|
|
* @param Application $application |
111
|
|
|
*/ |
112
|
15 |
|
private function setApplication(Application $application) |
113
|
|
|
{ |
114
|
15 |
|
$this->application = $application; |
115
|
15 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return Container |
119
|
|
|
*/ |
120
|
7 |
|
private function buildServices() |
121
|
|
|
{ |
122
|
7 |
|
$application = $this->getApplication(); |
123
|
|
|
|
124
|
7 |
|
$services = new Container(); |
125
|
|
|
|
126
|
|
|
$services['project'] = function () { |
127
|
1 |
|
return new Project(); |
128
|
|
|
}; |
129
|
|
|
|
130
|
|
|
$services['executor'] = function () { |
131
|
1 |
|
return new Executor(); |
132
|
|
|
}; |
133
|
|
|
|
134
|
|
|
$services['composer'] = function (Container $services) { |
135
|
1 |
|
return new Composer($services['executor'], new BalloonFactory()); |
136
|
|
|
}; |
137
|
|
|
|
138
|
|
|
$services['helper_set'] = function () use ($application) { |
139
|
1 |
|
return $application->getHelperSet(); |
140
|
|
|
}; |
141
|
|
|
|
142
|
|
|
$services['config'] = function () { |
143
|
7 |
|
return new Config(''); |
144
|
|
|
}; |
145
|
|
|
|
146
|
|
|
$services['alias_manager'] = function (Container $services) { |
147
|
5 |
|
$factory = new AliasManagerFactory(); |
148
|
5 |
|
return $factory->createFromConfig($services['config']); |
149
|
|
|
}; |
150
|
|
|
|
151
|
|
|
$services['module_manager'] = function (Container $services) { |
152
|
2 |
|
$factory = new ModuleManagerFactory(); |
153
|
2 |
|
return $factory->create($services['config']['module.path']); |
154
|
|
|
}; |
155
|
|
|
|
156
|
|
|
$services['module_procedure'] = function (Container $services) { |
157
|
1 |
|
return new ModuleProcedure( |
158
|
1 |
|
$services['module_manager'], |
159
|
1 |
|
$services['composer'], |
160
|
1 |
|
$services['balloon_factory'], |
161
|
1 |
|
new ModulesSorter() |
162
|
1 |
|
); |
163
|
|
|
}; |
164
|
|
|
|
165
|
|
|
$services['balloon_factory'] = function () { |
166
|
1 |
|
return new BalloonFactory(); |
167
|
|
|
}; |
168
|
|
|
|
169
|
1 |
|
$services['git'] = function () { |
170
|
1 |
|
return new Git(); |
171
|
|
|
}; |
172
|
|
|
|
173
|
7 |
|
return $services; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|