1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stats\Providers; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\Cache; |
6
|
|
|
use Joomla\Application as JoomlaApplication; |
7
|
|
|
use Joomla\Database\DatabaseDriver; |
8
|
|
|
use Joomla\DI\Container; |
9
|
|
|
use Joomla\DI\ServiceProviderInterface; |
10
|
|
|
use Joomla\Input\Cli; |
11
|
|
|
use Joomla\Input\Input; |
12
|
|
|
use Stats\CliApplication; |
13
|
|
|
use Stats\Commands\HelpCommand; |
14
|
|
|
use Stats\Commands\JoomlaTagsCommand; |
15
|
|
|
use Stats\Commands\SnapshotCommand; |
16
|
|
|
use Stats\Console; |
17
|
|
|
use Stats\Controllers\DisplayControllerGet; |
18
|
|
|
use Stats\Controllers\SubmitControllerCreate; |
19
|
|
|
use Stats\Controllers\SubmitControllerGet; |
20
|
|
|
use Stats\GitHub\GitHub; |
21
|
|
|
use Stats\Models\StatsModel; |
22
|
|
|
use Stats\Router; |
23
|
|
|
use Stats\Views\Stats\StatsJsonView; |
24
|
|
|
use Stats\WebApplication; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Application service provider |
28
|
|
|
* |
29
|
|
|
* @since 1.0 |
30
|
|
|
*/ |
31
|
|
|
class ApplicationServiceProvider implements ServiceProviderInterface |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Registers the service provider with a DI container. |
35
|
|
|
* |
36
|
|
|
* @param Container $container The DI container. |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
* |
40
|
|
|
* @since 1.0 |
41
|
|
|
*/ |
42
|
1 |
|
public function register(Container $container) |
|
|
|
|
43
|
|
|
{ |
44
|
1 |
|
$container->alias(CliApplication::class, JoomlaApplication\AbstractCliApplication::class) |
45
|
1 |
|
->share( |
46
|
1 |
|
JoomlaApplication\AbstractCliApplication::class, |
47
|
|
|
function (Container $container) |
48
|
|
|
{ |
49
|
|
|
$application = new CliApplication( |
50
|
|
|
$container->get(Cli::class), |
51
|
|
|
$container->get('config'), |
52
|
|
|
$container->get(JoomlaApplication\Cli\CliOutput::class), |
53
|
|
|
$container->get(Console::class) |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
// Inject extra services |
57
|
|
|
$application->setContainer($container); |
58
|
|
|
$application->setLogger($container->get('monolog.logger.cli')); |
59
|
|
|
|
60
|
|
|
return $application; |
61
|
1 |
|
}, |
62
|
|
|
true |
63
|
1 |
|
); |
64
|
|
|
|
65
|
1 |
|
$container->alias(WebApplication::class, JoomlaApplication\AbstractWebApplication::class) |
66
|
1 |
|
->share( |
67
|
1 |
|
JoomlaApplication\AbstractWebApplication::class, |
68
|
|
|
function (Container $container) |
69
|
|
|
{ |
70
|
|
|
$application = new WebApplication($container->get(Input::class), $container->get('config')); |
71
|
|
|
|
72
|
|
|
// Inject extra services |
73
|
|
|
$application->setLogger($container->get('monolog.logger.application')); |
74
|
|
|
$application->setRouter($container->get(Router::class)); |
75
|
|
|
|
76
|
|
|
return $application; |
77
|
1 |
|
}, |
78
|
|
|
true |
79
|
1 |
|
); |
80
|
|
|
|
81
|
1 |
|
$container->share( |
82
|
1 |
|
Input::class, |
83
|
|
|
function () |
84
|
|
|
{ |
85
|
|
|
return new Input($_REQUEST); |
86
|
1 |
|
}, |
87
|
|
|
true |
88
|
1 |
|
); |
89
|
|
|
|
90
|
1 |
|
$container->share( |
91
|
1 |
|
Cli::class, |
92
|
|
|
function () |
93
|
|
|
{ |
94
|
|
|
return new Cli; |
95
|
1 |
|
}, |
96
|
|
|
true |
97
|
1 |
|
); |
98
|
|
|
|
99
|
1 |
|
$container->share( |
100
|
1 |
|
Console::class, |
101
|
|
|
function (Container $container) |
102
|
|
|
{ |
103
|
|
|
$console = new Console; |
104
|
|
|
$console->setContainer($container); |
105
|
|
|
|
106
|
|
|
return $console; |
107
|
|
|
} |
108
|
1 |
|
); |
109
|
|
|
|
110
|
1 |
|
$container->share( |
111
|
1 |
|
JoomlaApplication\Cli\Output\Processor\ColorProcessor::class, |
112
|
|
|
function (Container $container) |
113
|
|
|
{ |
114
|
|
|
$processor = new JoomlaApplication\Cli\Output\Processor\ColorProcessor; |
115
|
|
|
|
116
|
|
|
/** @var Input $input */ |
117
|
|
|
$input = $container->get(Cli::class); |
118
|
|
|
|
119
|
|
|
if ($input->get('nocolors')) |
120
|
|
|
{ |
121
|
|
|
$processor->noColors = true; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Setup app colors (also required in "nocolors" mode - to strip them). |
125
|
|
|
$processor->addStyle('title', new JoomlaApplication\Cli\ColorStyle('yellow', '', ['bold'])); |
126
|
|
|
|
127
|
|
|
return $processor; |
128
|
|
|
} |
129
|
1 |
|
); |
130
|
|
|
|
131
|
1 |
|
$container->alias(JoomlaApplication\Cli\CliOutput::class, JoomlaApplication\Cli\Output\Stdout::class) |
132
|
1 |
|
->share( |
133
|
1 |
|
JoomlaApplication\Cli\Output\Stdout::class, |
134
|
|
|
function (Container $container) |
135
|
|
|
{ |
136
|
|
|
return new JoomlaApplication\Cli\Output\Stdout($container->get(JoomlaApplication\Cli\Output\Processor\ColorProcessor::class)); |
137
|
|
|
} |
138
|
1 |
|
); |
139
|
|
|
|
140
|
1 |
|
$container->share( |
141
|
1 |
|
Router::class, |
142
|
|
|
function (Container $container) |
143
|
|
|
{ |
144
|
|
|
$router = (new Router($container->get(Input::class))) |
145
|
|
|
->setContainer($container) |
146
|
|
|
->setControllerPrefix('Stats\\Controllers\\') |
147
|
|
|
->setDefaultController('DisplayController') |
148
|
|
|
->addMap('/submit', 'SubmitController') |
149
|
|
|
->addMap('/:source', 'DisplayController'); |
150
|
|
|
|
151
|
|
|
return $router; |
152
|
1 |
|
}, |
153
|
|
|
true |
154
|
1 |
|
); |
155
|
|
|
|
156
|
1 |
|
$container->share( |
157
|
1 |
|
HelpCommand::class, |
158
|
|
View Code Duplication |
function (Container $container) |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
$command = new HelpCommand; |
161
|
|
|
|
162
|
|
|
$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class)); |
163
|
|
|
$command->setInput($container->get(Input::class)); |
164
|
|
|
|
165
|
|
|
return $command; |
166
|
1 |
|
}, |
167
|
|
|
true |
168
|
1 |
|
); |
169
|
|
|
|
170
|
1 |
|
$container->share( |
171
|
1 |
|
JoomlaTagsCommand::class, |
172
|
|
View Code Duplication |
function (Container $container) |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
$command = new JoomlaTagsCommand($container->get(GitHub::class)); |
175
|
|
|
|
176
|
|
|
$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class)); |
177
|
|
|
$command->setInput($container->get(Input::class)); |
178
|
|
|
|
179
|
|
|
return $command; |
180
|
1 |
|
}, |
181
|
|
|
true |
182
|
1 |
|
); |
183
|
|
|
|
184
|
1 |
|
$container->share( |
185
|
1 |
|
SnapshotCommand::class, |
186
|
|
View Code Duplication |
function (Container $container) |
|
|
|
|
187
|
|
|
{ |
188
|
|
|
$command = new SnapshotCommand($container->get(StatsJsonView::class)); |
189
|
|
|
|
190
|
|
|
$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class)); |
191
|
|
|
$command->setInput($container->get(Input::class)); |
192
|
|
|
|
193
|
|
|
return $command; |
194
|
1 |
|
}, |
195
|
|
|
true |
196
|
1 |
|
); |
197
|
|
|
|
198
|
1 |
|
$container->share( |
199
|
1 |
|
DisplayControllerGet::class, |
200
|
|
View Code Duplication |
function (Container $container) |
|
|
|
|
201
|
|
|
{ |
202
|
|
|
$controller = new DisplayControllerGet( |
203
|
|
|
$container->get(StatsJsonView::class), |
204
|
|
|
$container->get(Cache::class) |
205
|
|
|
); |
206
|
|
|
|
207
|
|
|
$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class)); |
208
|
|
|
$controller->setInput($container->get(Input::class)); |
209
|
|
|
|
210
|
|
|
return $controller; |
211
|
1 |
|
}, |
212
|
|
|
true |
213
|
1 |
|
); |
214
|
|
|
|
215
|
1 |
|
$container->share( |
216
|
1 |
|
SubmitControllerCreate::class, |
217
|
|
View Code Duplication |
function (Container $container) |
|
|
|
|
218
|
|
|
{ |
219
|
|
|
$controller = new SubmitControllerCreate( |
220
|
|
|
$container->get(StatsModel::class) |
221
|
|
|
); |
222
|
|
|
|
223
|
|
|
$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class)); |
224
|
|
|
$controller->setInput($container->get(Input::class)); |
225
|
|
|
|
226
|
|
|
return $controller; |
227
|
1 |
|
}, |
228
|
|
|
true |
229
|
1 |
|
); |
230
|
|
|
|
231
|
1 |
|
$container->share( |
232
|
1 |
|
SubmitControllerGet::class, |
233
|
|
View Code Duplication |
function (Container $container) |
|
|
|
|
234
|
|
|
{ |
235
|
|
|
$controller = new SubmitControllerGet; |
236
|
|
|
|
237
|
|
|
$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class)); |
238
|
|
|
$controller->setInput($container->get(Input::class)); |
239
|
|
|
|
240
|
|
|
return $controller; |
241
|
1 |
|
}, |
242
|
|
|
true |
243
|
1 |
|
); |
244
|
|
|
|
245
|
1 |
|
$container->share( |
246
|
1 |
|
StatsModel::class, |
247
|
|
|
function (Container $container) |
248
|
|
|
{ |
249
|
|
|
return new StatsModel( |
250
|
|
|
$container->get(DatabaseDriver::class) |
251
|
|
|
); |
252
|
1 |
|
}, |
253
|
|
|
true |
254
|
1 |
|
); |
255
|
|
|
|
256
|
1 |
|
$container->share( |
257
|
1 |
|
StatsJsonView::class, |
258
|
1 |
|
function (Container $container) |
259
|
|
|
{ |
260
|
|
|
return new StatsJsonView( |
261
|
|
|
$container->get(StatsModel::class) |
262
|
|
|
); |
263
|
1 |
|
}, |
264
|
|
|
true |
265
|
1 |
|
); |
266
|
1 |
|
} |
267
|
|
|
} |
268
|
|
|
|
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: