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 Psr\Log\LoggerInterface; |
13
|
|
|
use Stats\CliApplication; |
14
|
|
|
use Stats\Commands\Cache\ClearCommand; |
15
|
|
|
use Stats\Commands\Database\MigrateCommand; |
16
|
|
|
use Stats\Commands\Database\StatusCommand; |
17
|
|
|
use Stats\Commands\Tags\JoomlaCommand; |
18
|
|
|
use Stats\Commands\HelpCommand; |
19
|
|
|
use Stats\Commands\InstallCommand; |
20
|
|
|
use Stats\Commands\SnapshotCommand; |
21
|
|
|
use Stats\Commands\UpdateCommand; |
22
|
|
|
use Stats\Console; |
23
|
|
|
use Stats\Controllers\DisplayControllerGet; |
24
|
|
|
use Stats\Controllers\SubmitControllerCreate; |
25
|
|
|
use Stats\Controllers\SubmitControllerGet; |
26
|
|
|
use Stats\Database\Migrations; |
27
|
|
|
use Stats\GitHub\GitHub; |
28
|
|
|
use Stats\Models\StatsModel; |
29
|
|
|
use Stats\Router; |
30
|
|
|
use Stats\Views\Stats\StatsJsonView; |
31
|
|
|
use Stats\WebApplication; |
32
|
|
|
use TheIconic\Tracking\GoogleAnalytics\Analytics; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Application service provider |
36
|
|
|
* |
37
|
|
|
* @since 1.0 |
38
|
|
|
*/ |
39
|
|
|
class ApplicationServiceProvider implements ServiceProviderInterface |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* Registers the service provider with a DI container. |
43
|
|
|
* |
44
|
|
|
* @param Container $container The DI container. |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
* |
48
|
|
|
* @since 1.0 |
49
|
|
|
*/ |
50
|
1 |
|
public function register(Container $container) |
|
|
|
|
51
|
|
|
{ |
52
|
1 |
|
$container->alias(CliApplication::class, JoomlaApplication\AbstractCliApplication::class) |
53
|
1 |
|
->share( |
54
|
1 |
|
JoomlaApplication\AbstractCliApplication::class, |
55
|
|
|
function (Container $container) |
56
|
|
|
{ |
57
|
|
|
$application = new CliApplication( |
58
|
|
|
$container->get(Cli::class), |
59
|
|
|
$container->get('config'), |
60
|
|
|
$container->get(JoomlaApplication\Cli\CliOutput::class), |
61
|
|
|
$container->get(Console::class) |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
// Inject extra services |
65
|
|
|
$application->setContainer($container); |
66
|
|
|
$application->setLogger($container->get('monolog.logger.cli')); |
67
|
|
|
|
68
|
|
|
return $application; |
69
|
1 |
|
}, |
70
|
|
|
true |
71
|
1 |
|
); |
72
|
|
|
|
73
|
1 |
|
$container->alias(WebApplication::class, JoomlaApplication\AbstractWebApplication::class) |
74
|
1 |
|
->share( |
75
|
1 |
|
JoomlaApplication\AbstractWebApplication::class, |
76
|
|
|
function (Container $container) |
77
|
|
|
{ |
78
|
|
|
$application = new WebApplication($container->get(Input::class), $container->get('config')); |
79
|
|
|
|
80
|
|
|
// Inject extra services |
81
|
|
|
$application->setAnalytics($container->get(Analytics::class)); |
82
|
|
|
$application->setLogger($container->get('monolog.logger.application')); |
83
|
|
|
$application->setRouter($container->get(Router::class)); |
84
|
|
|
|
85
|
|
|
return $application; |
86
|
1 |
|
}, |
87
|
|
|
true |
88
|
1 |
|
); |
89
|
|
|
|
90
|
1 |
|
$container->share( |
91
|
1 |
|
Input::class, |
92
|
|
|
function () |
93
|
|
|
{ |
94
|
|
|
return new Input($_REQUEST); |
95
|
1 |
|
}, |
96
|
|
|
true |
97
|
1 |
|
); |
98
|
|
|
|
99
|
1 |
|
$container->share( |
100
|
1 |
|
Cli::class, |
101
|
|
|
function () |
102
|
|
|
{ |
103
|
|
|
return new Cli; |
104
|
1 |
|
}, |
105
|
|
|
true |
106
|
1 |
|
); |
107
|
|
|
|
108
|
1 |
|
$container->share( |
109
|
1 |
|
Console::class, |
110
|
|
|
function (Container $container) |
111
|
|
|
{ |
112
|
|
|
$console = new Console; |
113
|
|
|
$console->setContainer($container); |
114
|
|
|
|
115
|
|
|
return $console; |
116
|
|
|
} |
117
|
1 |
|
); |
118
|
|
|
|
119
|
1 |
|
$container->share( |
120
|
1 |
|
JoomlaApplication\Cli\Output\Processor\ColorProcessor::class, |
121
|
|
|
function (Container $container) |
122
|
|
|
{ |
123
|
|
|
$processor = new JoomlaApplication\Cli\Output\Processor\ColorProcessor; |
124
|
|
|
|
125
|
|
|
/** @var Input $input */ |
126
|
|
|
$input = $container->get(Cli::class); |
127
|
|
|
|
128
|
|
|
if ($input->get('nocolors')) |
129
|
|
|
{ |
130
|
|
|
$processor->noColors = true; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// Setup app colors (also required in "nocolors" mode - to strip them). |
134
|
|
|
$processor->addStyle('title', new JoomlaApplication\Cli\ColorStyle('yellow', '', ['bold'])); |
135
|
|
|
|
136
|
|
|
return $processor; |
137
|
|
|
} |
138
|
1 |
|
); |
139
|
|
|
|
140
|
1 |
|
$container->alias(JoomlaApplication\Cli\CliOutput::class, JoomlaApplication\Cli\Output\Stdout::class) |
141
|
1 |
|
->share( |
142
|
1 |
|
JoomlaApplication\Cli\Output\Stdout::class, |
143
|
|
|
function (Container $container) |
144
|
|
|
{ |
145
|
|
|
return new JoomlaApplication\Cli\Output\Stdout($container->get(JoomlaApplication\Cli\Output\Processor\ColorProcessor::class)); |
146
|
|
|
} |
147
|
1 |
|
); |
148
|
|
|
|
149
|
1 |
|
$container->share( |
150
|
1 |
|
Analytics::class, |
151
|
|
|
function () |
152
|
|
|
{ |
153
|
|
|
return new Analytics(true); |
154
|
|
|
} |
155
|
1 |
|
); |
156
|
|
|
|
157
|
1 |
|
$container->share( |
158
|
1 |
|
Router::class, |
159
|
|
|
function (Container $container) |
160
|
|
|
{ |
161
|
|
|
$router = (new Router($container->get(Input::class))) |
162
|
|
|
->setContainer($container) |
163
|
|
|
->setControllerPrefix('Stats\\Controllers\\') |
164
|
|
|
->setDefaultController('DisplayController') |
165
|
|
|
->addMap('/submit', 'SubmitController') |
166
|
|
|
->addMap('/:source', 'DisplayController'); |
167
|
|
|
|
168
|
|
|
return $router; |
169
|
1 |
|
}, |
170
|
|
|
true |
171
|
1 |
|
); |
172
|
|
|
|
173
|
1 |
|
$container->share( |
174
|
1 |
|
HelpCommand::class, |
175
|
|
View Code Duplication |
function (Container $container) |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
$command = new HelpCommand; |
178
|
|
|
|
179
|
|
|
$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class)); |
180
|
|
|
$command->setInput($container->get(Input::class)); |
181
|
|
|
|
182
|
|
|
return $command; |
183
|
1 |
|
}, |
184
|
|
|
true |
185
|
1 |
|
); |
186
|
|
|
|
187
|
1 |
|
$container->share( |
188
|
1 |
|
InstallCommand::class, |
189
|
|
View Code Duplication |
function (Container $container) |
|
|
|
|
190
|
|
|
{ |
191
|
|
|
$command = new InstallCommand($container->get(DatabaseDriver::class)); |
192
|
|
|
|
193
|
|
|
$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class)); |
194
|
|
|
$command->setInput($container->get(Input::class)); |
195
|
|
|
|
196
|
|
|
return $command; |
197
|
1 |
|
}, |
198
|
|
|
true |
199
|
1 |
|
); |
200
|
|
|
|
201
|
1 |
|
$container->share( |
202
|
1 |
|
SnapshotCommand::class, |
203
|
|
View Code Duplication |
function (Container $container) |
|
|
|
|
204
|
|
|
{ |
205
|
|
|
$command = new SnapshotCommand($container->get(StatsJsonView::class)); |
206
|
|
|
|
207
|
|
|
$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class)); |
208
|
|
|
$command->setInput($container->get(Input::class)); |
209
|
|
|
|
210
|
|
|
return $command; |
211
|
1 |
|
}, |
212
|
|
|
true |
213
|
1 |
|
); |
214
|
|
|
|
215
|
1 |
|
$container->share( |
216
|
1 |
|
UpdateCommand::class, |
217
|
|
View Code Duplication |
function (Container $container) |
|
|
|
|
218
|
|
|
{ |
219
|
|
|
$command = new UpdateCommand; |
220
|
|
|
|
221
|
|
|
$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class)); |
222
|
|
|
$command->setInput($container->get(Input::class)); |
223
|
|
|
|
224
|
|
|
return $command; |
225
|
1 |
|
}, |
226
|
|
|
true |
227
|
1 |
|
); |
228
|
|
|
|
229
|
1 |
|
$container->share( |
230
|
1 |
|
ClearCommand::class, |
231
|
|
View Code Duplication |
function (Container $container) |
|
|
|
|
232
|
|
|
{ |
233
|
|
|
$command = new ClearCommand($container->get(Cache::class)); |
234
|
|
|
|
235
|
|
|
$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class)); |
236
|
|
|
$command->setInput($container->get(Input::class)); |
237
|
|
|
|
238
|
|
|
return $command; |
239
|
1 |
|
}, |
240
|
|
|
true |
241
|
1 |
|
); |
242
|
|
|
|
243
|
1 |
|
$container->share( |
244
|
1 |
|
MigrateCommand::class, |
245
|
|
|
function (Container $container) |
246
|
|
|
{ |
247
|
|
|
$command = new MigrateCommand($container->get(Migrations::class)); |
248
|
|
|
|
249
|
|
|
$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class)); |
250
|
|
|
$command->setInput($container->get(Input::class)); |
251
|
|
|
$command->setLogger($container->get(LoggerInterface::class)); |
252
|
|
|
|
253
|
|
|
return $command; |
254
|
1 |
|
}, |
255
|
|
|
true |
256
|
1 |
|
); |
257
|
|
|
|
258
|
1 |
|
$container->share( |
259
|
1 |
|
StatusCommand::class, |
260
|
|
View Code Duplication |
function (Container $container) |
|
|
|
|
261
|
|
|
{ |
262
|
|
|
$command = new StatusCommand($container->get(Migrations::class)); |
263
|
|
|
|
264
|
|
|
$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class)); |
265
|
|
|
$command->setInput($container->get(Input::class)); |
266
|
|
|
|
267
|
|
|
return $command; |
268
|
1 |
|
}, |
269
|
|
|
true |
270
|
1 |
|
); |
271
|
|
|
|
272
|
1 |
|
$container->share( |
273
|
1 |
|
JoomlaCommand::class, |
274
|
|
View Code Duplication |
function (Container $container) |
|
|
|
|
275
|
|
|
{ |
276
|
|
|
$command = new JoomlaCommand($container->get(GitHub::class)); |
277
|
|
|
|
278
|
|
|
$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class)); |
279
|
|
|
$command->setInput($container->get(Input::class)); |
280
|
|
|
|
281
|
|
|
return $command; |
282
|
1 |
|
}, |
283
|
|
|
true |
284
|
1 |
|
); |
285
|
|
|
|
286
|
1 |
|
$container->share( |
287
|
1 |
|
DisplayControllerGet::class, |
288
|
|
View Code Duplication |
function (Container $container) |
|
|
|
|
289
|
|
|
{ |
290
|
|
|
$controller = new DisplayControllerGet( |
291
|
|
|
$container->get(StatsJsonView::class), |
292
|
|
|
$container->get(Cache::class) |
293
|
|
|
); |
294
|
|
|
|
295
|
|
|
$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class)); |
296
|
|
|
$controller->setInput($container->get(Input::class)); |
297
|
|
|
|
298
|
|
|
return $controller; |
299
|
1 |
|
}, |
300
|
|
|
true |
301
|
1 |
|
); |
302
|
|
|
|
303
|
1 |
|
$container->share( |
304
|
1 |
|
SubmitControllerCreate::class, |
305
|
|
View Code Duplication |
function (Container $container) |
|
|
|
|
306
|
|
|
{ |
307
|
|
|
$controller = new SubmitControllerCreate( |
308
|
|
|
$container->get(StatsModel::class) |
309
|
|
|
); |
310
|
|
|
|
311
|
|
|
$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class)); |
312
|
|
|
$controller->setInput($container->get(Input::class)); |
313
|
|
|
|
314
|
|
|
return $controller; |
315
|
1 |
|
}, |
316
|
|
|
true |
317
|
1 |
|
); |
318
|
|
|
|
319
|
1 |
|
$container->share( |
320
|
1 |
|
SubmitControllerGet::class, |
321
|
|
View Code Duplication |
function (Container $container) |
|
|
|
|
322
|
|
|
{ |
323
|
|
|
$controller = new SubmitControllerGet; |
324
|
|
|
|
325
|
|
|
$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class)); |
326
|
|
|
$controller->setInput($container->get(Input::class)); |
327
|
|
|
|
328
|
|
|
return $controller; |
329
|
1 |
|
}, |
330
|
|
|
true |
331
|
1 |
|
); |
332
|
|
|
|
333
|
1 |
|
$container->share( |
334
|
1 |
|
StatsModel::class, |
335
|
|
|
function (Container $container) |
336
|
|
|
{ |
337
|
|
|
return new StatsModel( |
338
|
|
|
$container->get(DatabaseDriver::class) |
339
|
|
|
); |
340
|
1 |
|
}, |
341
|
|
|
true |
342
|
1 |
|
); |
343
|
|
|
|
344
|
1 |
|
$container->share( |
345
|
1 |
|
StatsJsonView::class, |
346
|
1 |
|
function (Container $container) |
347
|
|
|
{ |
348
|
|
|
return new StatsJsonView( |
349
|
|
|
$container->get(StatsModel::class) |
350
|
|
|
); |
351
|
1 |
|
}, |
352
|
|
|
true |
353
|
1 |
|
); |
354
|
1 |
|
} |
355
|
|
|
} |
356
|
|
|
|
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: