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