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