1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/mvc package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\Mvc; |
11
|
|
|
|
12
|
|
|
use Interop\Container\ContainerInterface; |
13
|
|
|
use Slick\Configuration\Configuration; |
14
|
|
|
use Slick\Di\Container; |
15
|
|
|
use Slick\Di\ContainerBuilder; |
16
|
|
|
use Slick\Http\PhpEnvironment\MiddlewareRunnerInterface; |
17
|
|
|
use Slick\Http\PhpEnvironment\Request; |
18
|
|
|
use Slick\Http\PhpEnvironment\Response; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Application |
22
|
|
|
* @package Slick\Mvc |
23
|
|
|
*/ |
24
|
|
|
class Application |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ContainerInterface |
29
|
|
|
*/ |
30
|
|
|
private static $container; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $configPath; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Request |
39
|
|
|
*/ |
40
|
|
|
private $request; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var MiddlewareRunnerInterface |
44
|
|
|
*/ |
45
|
|
|
private $runner; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Response |
49
|
|
|
*/ |
50
|
|
|
private $response; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var ContainerInterface |
54
|
|
|
*/ |
55
|
|
|
private static $defaultContainer; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Creates an application with an HTTP request |
59
|
|
|
* |
60
|
|
|
* If no request is provided then a Slick\Http\PhpEnvironment\Request is |
61
|
|
|
* created with values form current php environment settings. |
62
|
|
|
* |
63
|
|
|
* @param Request $request |
64
|
|
|
*/ |
65
|
8 |
|
public function __construct(Request $request = null) |
66
|
|
|
{ |
67
|
8 |
|
$this->request = $request; |
68
|
8 |
|
Configuration::addPath(__DIR__.'/Configuration'); |
69
|
8 |
|
$definitions = include __DIR__.'/Configuration/services.php'; |
70
|
8 |
|
self::$defaultContainer = ( |
71
|
8 |
|
new ContainerBuilder($definitions) |
72
|
1 |
|
) |
73
|
8 |
|
->getContainer(); |
74
|
8 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Gets the application dependency injection container |
78
|
|
|
* |
79
|
|
|
* @return ContainerInterface|Container |
80
|
|
|
*/ |
81
|
6 |
|
public function getContainer() |
82
|
|
|
{ |
83
|
6 |
|
if (is_null(self::$container)) { |
84
|
2 |
|
self::$container = $this->checkContainer(); |
85
|
1 |
|
} |
86
|
6 |
|
return self::$container; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Sets the dependency injection container |
91
|
|
|
* |
92
|
|
|
* @param ContainerInterface $container |
93
|
|
|
*/ |
94
|
6 |
|
public static function setContainer(ContainerInterface $container) |
95
|
|
|
{ |
96
|
6 |
|
self::$container = $container; |
97
|
6 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Returns the application dependency container |
101
|
|
|
* |
102
|
|
|
* @return ContainerInterface|Container |
103
|
|
|
*/ |
104
|
20 |
|
public static function container() |
105
|
|
|
{ |
106
|
20 |
|
if (null == self::$container) { |
107
|
2 |
|
$app = new static; |
108
|
2 |
|
$app->getContainer(); |
109
|
1 |
|
} |
110
|
20 |
|
return self::$container; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set middleware runner |
115
|
|
|
* |
116
|
|
|
* @param MiddlewareRunnerInterface $runner |
117
|
|
|
* |
118
|
|
|
* @return $this|self|Application |
119
|
|
|
*/ |
120
|
2 |
|
public function setRunner(MiddlewareRunnerInterface $runner) |
121
|
|
|
{ |
122
|
2 |
|
$this->runner = $runner; |
123
|
2 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns the processed response |
128
|
|
|
* |
129
|
|
|
* @return Response |
130
|
|
|
*/ |
131
|
2 |
|
public function getResponse() |
132
|
|
|
{ |
133
|
2 |
|
if (is_null($this->response)) { |
134
|
2 |
|
$this->response = $this->getRunner()->run(); |
135
|
1 |
|
} |
136
|
2 |
|
return $this->response; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Gets the HTTP middleware runner for this application |
141
|
|
|
* |
142
|
|
|
* @return MiddlewareRunnerInterface |
143
|
|
|
*/ |
144
|
2 |
|
public function getRunner() |
145
|
|
|
{ |
146
|
2 |
|
if (null === $this->runner) { |
147
|
2 |
|
$this->setRunner( |
148
|
2 |
|
$this->getContainer() |
149
|
2 |
|
->get('middleware.runner') |
150
|
1 |
|
); |
151
|
1 |
|
} |
152
|
2 |
|
return $this->runner; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Set configuration path |
157
|
|
|
* |
158
|
|
|
* @param string $configPath |
159
|
|
|
* |
160
|
|
|
* @return Application|self |
161
|
|
|
*/ |
162
|
|
|
public function setConfigPath($configPath) |
163
|
|
|
{ |
164
|
|
|
$this->configPath = $configPath; |
165
|
|
|
Configuration::addPath($configPath); |
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Gets configuration path |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public function getConfigPath() |
175
|
|
|
{ |
176
|
|
|
if (null == $this->configPath) { |
177
|
|
|
$this->configPath = getcwd().'/Configuration'; |
178
|
|
|
Configuration::addPath($this->configPath); |
179
|
|
|
} |
180
|
|
|
return $this->configPath; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Gets HTTP request object |
185
|
|
|
* |
186
|
|
|
* @return Request |
187
|
|
|
*/ |
188
|
2 |
|
public function getRequest() |
189
|
|
|
{ |
190
|
2 |
|
if (null === $this->request) { |
191
|
2 |
|
$this->request = $this->getContainer() |
192
|
2 |
|
->get('request'); |
193
|
1 |
|
} |
194
|
2 |
|
return $this->request; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Gets container with user overridden settings |
199
|
|
|
* |
200
|
|
|
* @return ContainerInterface|\Slick\Di\Container |
201
|
|
|
*/ |
202
|
2 |
|
protected function checkContainer() |
203
|
|
|
{ |
204
|
2 |
|
$container = self::$defaultContainer; |
205
|
|
|
if ( |
206
|
2 |
|
null != $this->configPath && |
207
|
1 |
|
file_exists($this->configPath.'/services.php') |
208
|
1 |
|
) { |
209
|
|
|
$definitions = include $this->configPath.'/services.php'; |
210
|
|
|
$container = ( |
211
|
|
|
new ContainerBuilder( |
212
|
|
|
$definitions, |
213
|
|
|
true |
214
|
|
|
) |
215
|
|
|
)->getContainer(); |
216
|
|
|
} |
217
|
2 |
|
return $container; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
} |