This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Application.php
398 lines (335 loc) · 11.9 KB
/
Application.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
/**
* This file is part of Vegas package
*
* @author Slawomir Zytko <slawek@amsterdam-standard.pl>
* @company Amsterdam Standard Sp. z o.o.
* @homepage http://cmf.vegas
*/
namespace Vegas\Mvc;
use Phalcon\Config;
use Phalcon\Events\Manager;
use Phalcon\Http\ResponseInterface;
use Vegas\Mvc\ModuleManager\EventListener\Boot as ModuleManagerBootEventListener;
use Vegas\Mvc\Router\EventListener\Boot as RouterBootEventListener;
use Vegas\Mvc\Autoloader\EventListener\Boot as AutoloaderBootEventListener;
use Vegas\Mvc\View\EventListener\Boot as ViewBootEventListener;
use Vegas\Mvc\Application\EventListener\Boot as ApplicationBootEventListener;
use Vegas\Mvc\Di\Injector\EventListener\Boot as DiBootEventListener;
use Vegas\Mvc\Router;
/**
* Class Application
* @package Vegas\Mvc
*/
class Application extends \Phalcon\Mvc\Application
{
/**
* @var string
*/
protected $applicationDirectory;
/**
* @var ModuleManager
*/
protected $moduleManager;
/**
* @var Config
*/
protected $config;
/**
* @var bool
*/
protected $isBootstrapped = false;
/**
* Application constructor.
* @param \Phalcon\DiInterface|null $dependencyInjector
* @param Config $config
*/
public function __construct(\Phalcon\DiInterface $dependencyInjector = null, Config $config)
{
parent::__construct($dependencyInjector);
$this->config = $config;
$this->_eventsManager = new Manager();
$dependencyInjector->set('config', $config);
$this->attachBootstrapEvents();
}
/**
* @param $applicationDirectory
*/
public function setApplicationDirectory($applicationDirectory)
{
$this->applicationDirectory = $applicationDirectory;
}
/**
* @return string
*/
public function getApplicationDirectory()
{
return $this->applicationDirectory;
}
/**
* @return mixed
*/
public function getModuleManager()
{
return $this->moduleManager;
}
/**
* @return Config
*/
public function getConfig()
{
return $this->config;
}
/**
* @return $this
*/
protected function attachBootstrapEvents()
{
$this->getEventsManager()->attach('application', new ModuleManagerBootEventListener());
$this->getEventsManager()->attach('application', new AutoloaderBootEventListener());
$this->getEventsManager()->attach('application', new DiBootEventListener());
$this->getEventsManager()->attach('application', new RouterBootEventListener());
$this->getEventsManager()->attach('application', new ViewBootEventListener());
$this->getEventsManager()->attach('application', new ApplicationBootEventListener());
return $this;
}
/**
* Triggers the bootstrap process autoloading modules
* @return bool
* @throws \Exception
*/
public function bootstrap()
{
$di = $this->di;
if (!is_object($di)) {
throw new \Exception("A dependency injection object is required to access internal services");
}
$eventsManager = $this->_eventsManager;
/**
* Call boot event, this allow the developer to perform initialization actions
*/
if ($eventsManager instanceof Manager) {
if ($eventsManager->fire("application:boot", $this) === false) {
return false;
}
}
return true;
}
/**
* @param null $uri
* @return mixed|object
* @throws \Exception
*/
public function handle($uri = null)
{
/**
* Allow one bootstrap per application instance
*/
if (!$this->isBootstrapped) {
$this->isBootstrapped = $this->bootstrap();
}
/**
* Refuse to continue if boot process failed
*/
if (!$this->isBootstrapped) {
return false;
}
$di = $this->di;
$eventsManager = $this->_eventsManager;
$router = $di->getShared("router");
/**
* Handle the URI pattern (if any)
*/
$router->handle($uri);
/**
* If the router doesn't return a valid module we use the default module
*/
$moduleName = $router->getModuleName();
if (!$moduleName) {
$moduleName = $this->_defaultModule;
}
$moduleObject = null;
/**
* Process the module definition
*/
if ($moduleName) {
if ($eventsManager instanceof Manager) {
if ($eventsManager->fire("application:beforeStartModule", $this, $moduleName) === false) {
return false;
}
}
/**
* Gets the module definition
*/
$module = $this->getModule($moduleName);
/**
* A module definition must ne an array or an object
*/
if (!is_array($module) && !$module instanceof \Phalcon\Mvc\ModuleDefinitionInterface) {
throw new \Exception("Invalid module definition");
}
/**
* An array module definition contains a path to a module definition class
*/
if (is_array($module)) {
/**
* Class name used to load the module definition
*/
$className = isset($module['className']) ? $module['className'] : ModuleManager::MODULE_BOOTSTRAP;
$moduleObject = $di->get($className);
/**
* 'registerAutoloaders' and 'registerServices' are automatically called
*/
$moduleObject->registerAutoloaders($di);
$moduleObject->registerServices($di);
} else {
/**
* A module definition object, can be a Closure instance
*/
if ($module instanceof \Closure) {
$moduleObject = call_user_func_array($module, [$di]);
} else {
throw new \Exception("Invalid module definition");
}
}
/**
* Calling afterStartModule event
*/
if ($eventsManager instanceof Manager) {
$eventsManager->fire("application:afterStartModule", $this, $moduleObject);
}
}
/**
* We get the parameters from the router and assign them to the dispatcher
* Assign the values passed from the router
*/
$dispatcher = $di->getShared("dispatcher");
$dispatcher->setDefaultNamespace(sprintf('%s\\Controller', $router->getModuleName()));
$dispatcher->setModuleName($router->getModuleName());
$dispatcher->setNamespaceName($router->getNamespaceName());
$dispatcher->setControllerName($router->getControllerName());
$dispatcher->setActionName($router->getActionName());
$dispatcher->setParams($router->getParams());
if ($dispatcher->getEventsManager() == null) {
$eventsManager = new Manager();
$dispatcher->setEventsManager($eventsManager);
}
if ($di->has('view') && $this->_implicitView) {
$di->get('view')->setEventsManager($dispatcher->getEventsManager());
$di->get('view')->start();
}
/**
* Calling beforeHandleRequest
*/
if ($eventsManager instanceof Manager) {
if ($eventsManager->fire("application:beforeHandleRequest", $this, $dispatcher) === false) {
return false;
}
}
/**
* The dispatcher must return an object
*/
$controller = $dispatcher->dispatch();
/**
* Get the latest value returned by an action
*/
$possibleResponse = $dispatcher->getReturnedValue();
if (gettype($possibleResponse) === 'boolean' && $possibleResponse == false) {
$response = $di->getShared("response");
} else {
if (is_object($possibleResponse)) {
/**
* Check if the returned object is already a response
*/
$returnedResponse = $possibleResponse instanceof ResponseInterface;
} else {
$returnedResponse = false;
}
/**
* Calling afterHandleRequest
*/
if ($eventsManager instanceof Manager) {
$eventsManager->fire("application:afterHandleRequest", $this, $controller);
}
/**
* If the dispatcher returns an object we try to render the view in auto-rendering mode
*/
if (!$returnedResponse) {
if ($di->has('view') && $this->_implicitView === true) {
if (is_object($controller)) {
$renderStatus = true;
/**
* This allows to make a custom view render
*/
if ($eventsManager instanceof Manager) {
$renderStatus = $eventsManager->fire("application:viewRender", $this, $di->get('view'));
}
/**
* Check if the view process has been treated by the developer
*/
if ($renderStatus !== false) {
/**
* Automatic render based on the latest controller executed
*/
if (isset($module)) {
$di->get('view')->render(
sprintf(
'modules/%s/View/%s',
$module['name'],
ltrim(str_replace('\\', '/', $dispatcher->getControllerName()), '/')
),
$dispatcher->getActionName(),
$dispatcher->getParams()
);
} else {
$di->get('view')->render(
$dispatcher->getControllerName(),
$dispatcher->getActionName(),
$dispatcher->getParams()
);
}
}
}
}
}
/**
* Finish the view component (stop output buffering)
*/
if ($di->has('view') && $this->_implicitView === true) {
$di->get('view')->finish();
}
if (!$returnedResponse) {
$response = $di->getShared("response");
if ($this->_implicitView === true) {
/**
* The content returned by the view is passed to the response service
*/
$response->setContent($di->get('view')->getContent());
}
} else {
/**
* We don't need to create a response because there is one already created
*/
$response = $possibleResponse;
}
}
/**
* Calling beforeSendResponse
*/
if ($eventsManager instanceof Manager) {
$eventsManager->fire("application:beforeSendResponse", $this, $response);
}
/**
* Headers and Cookies are automatically send
*/
$response->sendHeaders();
$response->sendCookies();
if (!$response->isSent()) {
$response->send();
}
/**
* Return the response
*/
return $response;
}
}