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