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\Input; |
11
|
|
|
use Stats\Application; |
12
|
|
|
use Stats\Controllers\DisplayControllerGet; |
13
|
|
|
use Stats\Controllers\SubmitControllerCreate; |
14
|
|
|
use Stats\Controllers\SubmitControllerGet; |
15
|
|
|
use Stats\Models\StatsModel; |
16
|
|
|
use Stats\Router; |
17
|
|
|
use Stats\Views\Stats\StatsJsonView; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Application service provider |
21
|
|
|
* |
22
|
|
|
* @since 1.0 |
23
|
|
|
*/ |
24
|
|
|
class ApplicationServiceProvider implements ServiceProviderInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Registers the service provider with a DI container. |
28
|
|
|
* |
29
|
|
|
* @param Container $container The DI container. |
30
|
|
|
* |
31
|
|
|
* @return void |
32
|
|
|
* |
33
|
|
|
* @since 1.0 |
34
|
|
|
*/ |
35
|
1 |
|
public function register(Container $container) |
|
|
|
|
36
|
|
|
{ |
37
|
1 |
|
$container->alias(Application::class, JoomlaApplication\AbstractApplication::class) |
38
|
1 |
|
->alias(JoomlaApplication\AbstractWebApplication::class, JoomlaApplication\AbstractApplication::class) |
39
|
1 |
|
->share( |
40
|
1 |
|
JoomlaApplication\AbstractApplication::class, |
41
|
|
|
function (Container $container) |
42
|
|
|
{ |
43
|
|
|
$application = new Application($container->get(Input::class), $container->get('config')); |
44
|
|
|
|
45
|
|
|
// Inject extra services |
46
|
|
|
$application->setLogger($container->get('monolog.logger.application')); |
47
|
|
|
$application->setRouter($container->get(Router::class)); |
48
|
|
|
|
49
|
|
|
return $application; |
50
|
1 |
|
}, |
51
|
|
|
true |
52
|
1 |
|
); |
53
|
|
|
|
54
|
1 |
|
$container->share( |
55
|
1 |
|
Input::class, |
56
|
|
|
function () |
57
|
|
|
{ |
58
|
|
|
return new Input($_REQUEST); |
59
|
1 |
|
}, |
60
|
|
|
true |
61
|
1 |
|
); |
62
|
|
|
|
63
|
1 |
|
$container->share( |
64
|
1 |
|
Router::class, |
65
|
|
|
function (Container $container) |
66
|
|
|
{ |
67
|
|
|
$router = (new Router($container->get(Input::class))) |
68
|
|
|
->setContainer($container) |
69
|
|
|
->setControllerPrefix('Stats\\Controllers\\') |
70
|
|
|
->setDefaultController('DisplayController') |
71
|
|
|
->addMap('/submit', 'SubmitController') |
72
|
|
|
->addMap('/:source', 'DisplayController'); |
73
|
|
|
|
74
|
|
|
return $router; |
75
|
1 |
|
}, |
76
|
|
|
true |
77
|
1 |
|
); |
78
|
|
|
|
79
|
1 |
|
$container->share( |
80
|
1 |
|
DisplayControllerGet::class, |
81
|
|
View Code Duplication |
function (Container $container) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$controller = new DisplayControllerGet( |
84
|
|
|
$container->get(StatsJsonView::class), |
85
|
|
|
$container->get(Cache::class) |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class)); |
89
|
|
|
$controller->setInput($container->get(Input::class)); |
90
|
|
|
|
91
|
|
|
return $controller; |
92
|
1 |
|
}, |
93
|
|
|
true |
94
|
1 |
|
); |
95
|
|
|
|
96
|
1 |
|
$container->share( |
97
|
1 |
|
SubmitControllerCreate::class, |
98
|
|
View Code Duplication |
function (Container $container) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$controller = new SubmitControllerCreate( |
101
|
|
|
$container->get(StatsModel::class) |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class)); |
105
|
|
|
$controller->setInput($container->get(Input::class)); |
106
|
|
|
|
107
|
|
|
return $controller; |
108
|
1 |
|
}, |
109
|
|
|
true |
110
|
1 |
|
); |
111
|
|
|
|
112
|
1 |
|
$container->share( |
113
|
1 |
|
SubmitControllerGet::class, |
114
|
|
|
function (Container $container) |
115
|
|
|
{ |
116
|
|
|
$controller = new SubmitControllerGet; |
117
|
|
|
|
118
|
|
|
$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class)); |
119
|
|
|
$controller->setInput($container->get(Input::class)); |
120
|
|
|
|
121
|
|
|
return $controller; |
122
|
1 |
|
}, |
123
|
|
|
true |
124
|
1 |
|
); |
125
|
|
|
|
126
|
1 |
|
$container->share( |
127
|
1 |
|
StatsModel::class, |
128
|
|
|
function (Container $container) |
129
|
|
|
{ |
130
|
|
|
return new StatsModel( |
131
|
|
|
$container->get(DatabaseDriver::class) |
132
|
|
|
); |
133
|
1 |
|
}, |
134
|
|
|
true |
135
|
1 |
|
); |
136
|
|
|
|
137
|
1 |
|
$container->share( |
138
|
1 |
|
StatsJsonView::class, |
139
|
1 |
|
function (Container $container) |
140
|
|
|
{ |
141
|
|
|
return new StatsJsonView( |
142
|
|
|
$container->get(StatsModel::class) |
143
|
|
|
); |
144
|
1 |
|
}, |
145
|
|
|
true |
146
|
1 |
|
); |
147
|
1 |
|
} |
148
|
|
|
} |
149
|
|
|
|
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: