|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Jitamin. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (C) Jitamin Team |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Jitamin\Foundation; |
|
13
|
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Exceptions\AccessForbiddenException; |
|
15
|
|
|
use Jitamin\Foundation\Exceptions\PageNotFoundException; |
|
16
|
|
|
use Jitamin\Http\Controllers\AppController; |
|
17
|
|
|
use Jitamin\Http\Middleware\ApplicationAuthorizationMiddleware; |
|
18
|
|
|
use Jitamin\Http\Middleware\AuthenticationMiddleware; |
|
19
|
|
|
use Jitamin\Http\Middleware\BootstrapMiddleware; |
|
20
|
|
|
use Jitamin\Http\Middleware\PostAuthenticationMiddleware; |
|
21
|
|
|
use Jitamin\Http\Middleware\ProjectAuthorizationMiddleware; |
|
22
|
|
|
use RuntimeException; |
|
23
|
|
|
use Symfony\Component\EventDispatcher\Event; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class Application. |
|
27
|
|
|
*/ |
|
28
|
|
|
class Application extends Base |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* Execute middleware and controller. |
|
32
|
|
|
*/ |
|
33
|
|
|
public function execute() |
|
34
|
|
|
{ |
|
35
|
|
|
if ($this->runningInConsole()) { |
|
36
|
|
|
$this->container['dispatcher']->dispatch('app.bootstrap', new Event()); |
|
37
|
|
|
$this->container['cli']->run(); |
|
38
|
|
|
|
|
39
|
|
|
return; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$this->container['router']->dispatch(); |
|
43
|
|
|
|
|
44
|
|
|
if ($this->container['router']->getController() === 'Api') { |
|
45
|
|
|
echo $this->container['api']->execute(); |
|
46
|
|
|
|
|
47
|
|
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
try { |
|
51
|
|
|
$this->executeMiddleware(); |
|
52
|
|
|
|
|
53
|
|
|
if (!$this->response->isResponseAlreadySent()) { |
|
|
|
|
|
|
54
|
|
|
$this->executeController(); |
|
55
|
|
|
} |
|
56
|
|
|
} catch (PageNotFoundException $e) { |
|
57
|
|
|
$controllerObject = new AppController($this->container); |
|
58
|
|
|
$controllerObject->notFound($e->hasLayout()); |
|
59
|
|
|
} catch (AccessForbiddenException $e) { |
|
60
|
|
|
$controllerObject = new AppController($this->container); |
|
61
|
|
|
$controllerObject->accessForbidden($e->hasLayout(), $e->getMessage()); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Determine if we are running in the console. |
|
67
|
|
|
* |
|
68
|
|
|
* @return bool |
|
69
|
|
|
*/ |
|
70
|
|
|
public function runningInConsole() |
|
71
|
|
|
{ |
|
72
|
|
|
return php_sapi_name() == 'cli'; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Execute all middleware. |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function executeMiddleware() |
|
79
|
|
|
{ |
|
80
|
|
|
if (DEBUG) { |
|
81
|
|
|
$this->logger->debug(__METHOD__); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$bootstrapMiddleware = new BootstrapMiddleware($this->container); |
|
85
|
|
|
$authenticationMiddleware = new AuthenticationMiddleware($this->container); |
|
86
|
|
|
$postAuthenticationMiddleware = new PostAuthenticationMiddleware($this->container); |
|
87
|
|
|
$appAuthorizationMiddleware = new ApplicationAuthorizationMiddleware($this->container); |
|
88
|
|
|
$projectAuthorizationMiddleware = new ProjectAuthorizationMiddleware($this->container); |
|
89
|
|
|
|
|
90
|
|
|
$bootstrapMiddleware->setNextMiddleware($authenticationMiddleware); |
|
|
|
|
|
|
91
|
|
|
$authenticationMiddleware->setNextMiddleware($postAuthenticationMiddleware); |
|
|
|
|
|
|
92
|
|
|
$postAuthenticationMiddleware->setNextMiddleware($appAuthorizationMiddleware); |
|
|
|
|
|
|
93
|
|
|
$appAuthorizationMiddleware->setNextMiddleware($projectAuthorizationMiddleware); |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
$bootstrapMiddleware->execute(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Execute the controller. |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function executeController() |
|
102
|
|
|
{ |
|
103
|
|
|
$className = $this->getControllerClassName(); |
|
104
|
|
|
|
|
105
|
|
|
if (DEBUG) { |
|
106
|
|
|
$this->logger->debug(__METHOD__.' => '.$className.'::'.$this->router->getAction()); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$controllerObject = new $className($this->container); |
|
110
|
|
|
$controllerObject->{$this->router->getAction()}(); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get controller class name. |
|
115
|
|
|
* |
|
116
|
|
|
* @throws RuntimeException |
|
117
|
|
|
* |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function getControllerClassName() |
|
121
|
|
|
{ |
|
122
|
|
|
if ($this->router->getPlugin() !== '') { |
|
|
|
|
|
|
123
|
|
|
$className = '\Jitamin\Plugin\\'.$this->router->getPlugin().'\Controller\\'.$this->router->getController(); |
|
|
|
|
|
|
124
|
|
|
} else { |
|
125
|
|
|
$className = '\Jitamin\Http\Controllers\\'.str_replace('/', '\\', $this->router->getController()); |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
if (!class_exists($className)) { |
|
129
|
|
|
throw new RuntimeException('Controller not found: '.$className); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if (!method_exists($className, $this->router->getAction())) { |
|
|
|
|
|
|
133
|
|
|
throw new RuntimeException('Action not implemented: '.$this->router->getAction()); |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $className; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.