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