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