1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Joomla! Statistics Server |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (C) 2013 - 2017 Open Source Matters, Inc. All rights reserved. |
6
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Joomla\StatsServer\Providers; |
10
|
|
|
|
11
|
|
|
use Joomla\Application\AbstractApplication; |
12
|
|
|
use Joomla\Application\AbstractWebApplication; |
13
|
|
|
use Joomla\Application\Controller\ContainerControllerResolver; |
14
|
|
|
use Joomla\Application\Controller\ControllerResolverInterface; |
15
|
|
|
use Joomla\Application\Web\WebClient; |
16
|
|
|
use Joomla\Application\WebApplication; |
17
|
|
|
use Joomla\DI\Container; |
18
|
|
|
use Joomla\DI\ServiceProviderInterface; |
19
|
|
|
use Joomla\Event\DispatcherInterface; |
20
|
|
|
use Joomla\Input\Input; |
21
|
|
|
use Joomla\Router\Router; |
22
|
|
|
use Joomla\StatsServer\Controllers\DisplayStatisticsController; |
23
|
|
|
use Joomla\StatsServer\Controllers\SubmitDataController; |
24
|
|
|
use Joomla\StatsServer\Repositories\StatisticsRepository; |
25
|
|
|
use Joomla\StatsServer\Views\Stats\StatsJsonView; |
26
|
|
|
use Psr\Log\LoggerInterface; |
27
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Web application service provider |
31
|
|
|
*/ |
32
|
|
|
class WebApplicationServiceProvider implements ServiceProviderInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Registers the service provider with a DI container. |
36
|
|
|
* |
37
|
|
|
* @param Container $container The DI container. |
38
|
|
|
* |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
4 |
|
public function register(Container $container): void |
42
|
|
|
{ |
43
|
4 |
|
$container->alias(WebApplication::class, AbstractWebApplication::class) |
44
|
4 |
|
->share(AbstractWebApplication::class, [$this, 'getWebApplicationService']); |
45
|
|
|
|
46
|
|
|
/* |
47
|
|
|
* Application Class Dependencies |
48
|
|
|
*/ |
49
|
|
|
|
50
|
4 |
|
$container->share(Input::class, [$this, 'getInputService']); |
51
|
4 |
|
$container->share(Router::class, [$this, 'getRouterService']); |
52
|
|
|
|
53
|
4 |
|
$container->alias(ContainerControllerResolver::class, ControllerResolverInterface::class) |
54
|
4 |
|
->share(ControllerResolverInterface::class, [$this, 'getControllerResolverService']); |
55
|
|
|
|
56
|
4 |
|
$container->share(WebClient::class, [$this, 'getWebClientService']); |
57
|
|
|
|
58
|
|
|
/* |
59
|
|
|
* MVC Layer |
60
|
|
|
*/ |
61
|
|
|
|
62
|
|
|
// Controllers |
63
|
4 |
|
$container->share(DisplayStatisticsController::class, [$this, 'getDisplayStatisticsControllerService']); |
64
|
4 |
|
$container->share(SubmitDataController::class, [$this, 'getSubmitDataControllerService']); |
65
|
|
|
|
66
|
|
|
// Views |
67
|
4 |
|
$container->share(StatsJsonView::class, [$this, 'getStatsJsonViewService']); |
68
|
4 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the controller resolver service |
72
|
|
|
* |
73
|
|
|
* @param Container $container The DI container. |
74
|
|
|
* |
75
|
|
|
* @return ControllerResolverInterface |
76
|
|
|
*/ |
77
|
1 |
|
public function getControllerResolverService(Container $container): ControllerResolverInterface |
78
|
|
|
{ |
79
|
1 |
|
return new ContainerControllerResolver($container); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the DisplayControllerGet class service |
84
|
|
|
* |
85
|
|
|
* @param Container $container The DI container. |
86
|
|
|
* |
87
|
|
|
* @return DisplayStatisticsController |
88
|
|
|
*/ |
89
|
|
View Code Duplication |
public function getDisplayStatisticsControllerService(Container $container): DisplayStatisticsController |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$controller = new DisplayStatisticsController( |
92
|
|
|
$container->get(StatsJsonView::class) |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$controller->setApplication($container->get(AbstractApplication::class)); |
96
|
|
|
$controller->setInput($container->get(Input::class)); |
97
|
|
|
|
98
|
|
|
return $controller; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the Input class service |
103
|
|
|
* |
104
|
|
|
* @param Container $container The DI container. |
105
|
|
|
* |
106
|
|
|
* @return Input |
107
|
|
|
*/ |
108
|
1 |
|
public function getInputService(Container $container): Input |
|
|
|
|
109
|
|
|
{ |
110
|
1 |
|
return new Input($_REQUEST); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the router service |
115
|
|
|
* |
116
|
|
|
* @param Container $container The DI container. |
117
|
|
|
* |
118
|
|
|
* @return Router |
119
|
|
|
*/ |
120
|
1 |
|
public function getRouterService(Container $container): Router |
|
|
|
|
121
|
|
|
{ |
122
|
1 |
|
$router = new Router; |
123
|
|
|
|
124
|
1 |
|
$router->get( |
125
|
1 |
|
'/', |
126
|
1 |
|
DisplayStatisticsController::class |
127
|
|
|
); |
128
|
|
|
|
129
|
1 |
|
$router->post( |
130
|
1 |
|
'/submit', |
131
|
1 |
|
SubmitDataController::class |
132
|
|
|
); |
133
|
|
|
|
134
|
1 |
|
$router->get( |
135
|
1 |
|
'/:source', |
136
|
1 |
|
DisplayStatisticsController::class, |
137
|
|
|
[ |
138
|
1 |
|
'source' => '(' . implode('|', StatisticsRepository::ALLOWED_SOURCES) . ')', |
139
|
|
|
] |
140
|
|
|
); |
141
|
|
|
|
142
|
1 |
|
return $router; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get the StatsJsonView class service |
147
|
|
|
* |
148
|
|
|
* @param Container $container The DI container. |
149
|
|
|
* |
150
|
|
|
* @return StatsJsonView |
151
|
|
|
*/ |
152
|
|
|
public function getStatsJsonViewService(Container $container): StatsJsonView |
153
|
|
|
{ |
154
|
|
|
return new StatsJsonView( |
155
|
|
|
$container->get(StatisticsRepository::class) |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get the SubmitControllerCreate class service |
161
|
|
|
* |
162
|
|
|
* @param Container $container The DI container. |
163
|
|
|
* |
164
|
|
|
* @return SubmitDataController |
165
|
|
|
*/ |
166
|
|
View Code Duplication |
public function getSubmitDataControllerService(Container $container): SubmitDataController |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
$controller = new SubmitDataController( |
169
|
|
|
$container->get(StatisticsRepository::class), |
170
|
|
|
$container->get('filesystem.versions') |
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
$controller->setApplication($container->get(AbstractApplication::class)); |
174
|
|
|
$controller->setInput($container->get(Input::class)); |
175
|
|
|
|
176
|
|
|
return $controller; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get the web application service |
181
|
|
|
* |
182
|
|
|
* @param Container $container The DI container. |
183
|
|
|
* |
184
|
|
|
* @return WebApplication |
185
|
|
|
*/ |
186
|
1 |
|
public function getWebApplicationService(Container $container): WebApplication |
187
|
|
|
{ |
188
|
1 |
|
$application = new WebApplication( |
189
|
1 |
|
$container->get(ControllerResolverInterface::class), |
190
|
1 |
|
$container->get(Router::class), |
191
|
1 |
|
$container->get(Input::class), |
192
|
1 |
|
$container->get('config'), |
193
|
1 |
|
$container->get(WebClient::class), |
194
|
1 |
|
new JsonResponse([]) |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
// Inject extra services |
198
|
1 |
|
$application->setDispatcher($container->get(DispatcherInterface::class)); |
199
|
1 |
|
$application->setLogger($container->get(LoggerInterface::class)); |
200
|
|
|
|
201
|
1 |
|
return $application; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get the web client service |
206
|
|
|
* |
207
|
|
|
* @param Container $container The DI container. |
208
|
|
|
* |
209
|
|
|
* @return WebClient |
210
|
|
|
*/ |
211
|
1 |
|
public function getWebClientService(Container $container): WebClient |
212
|
|
|
{ |
213
|
|
|
/** @var Input $input */ |
214
|
1 |
|
$input = $container->get(Input::class); |
215
|
1 |
|
$userAgent = $input->server->getString('HTTP_USER_AGENT', ''); |
216
|
1 |
|
$acceptEncoding = $input->server->getString('HTTP_ACCEPT_ENCODING', ''); |
217
|
1 |
|
$acceptLanguage = $input->server->getString('HTTP_ACCEPT_LANGUAGE', ''); |
218
|
|
|
|
219
|
1 |
|
return new WebClient($userAgent, $acceptEncoding, $acceptLanguage); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.