|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Linna Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Sebastian Rapetti <[email protected]> |
|
7
|
|
|
* @copyright (c) 2017, Sebastian Rapetti |
|
8
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
9
|
|
|
*/ |
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Linna\Mvc; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* FrontController. |
|
16
|
|
|
*/ |
|
17
|
|
|
class FrontController |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var object Contain view object for render |
|
21
|
|
|
*/ |
|
22
|
|
|
private $view; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var object Contain model object |
|
26
|
|
|
*/ |
|
27
|
|
|
private $model; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var object Contain controller object |
|
31
|
|
|
*/ |
|
32
|
|
|
private $controller; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string Contain Controller and View action name |
|
36
|
|
|
*/ |
|
37
|
|
|
private $routeAction; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array Paremeter passed to Controller |
|
41
|
|
|
*/ |
|
42
|
|
|
private $routeParam; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Constructor. |
|
46
|
|
|
* |
|
47
|
|
|
* @param Model $model |
|
48
|
|
|
* @param View $view |
|
49
|
|
|
* @param Controller $controller |
|
50
|
|
|
* @param string $action |
|
51
|
|
|
* @param array $param |
|
52
|
|
|
*/ |
|
53
|
15 |
|
public function __construct(Model $model, View $view, Controller $controller, string $action, array $param) |
|
54
|
|
|
{ |
|
55
|
15 |
|
$this->routeAction = $action; |
|
56
|
15 |
|
$this->routeParam = $param; |
|
57
|
|
|
|
|
58
|
15 |
|
$this->model = $model; |
|
59
|
15 |
|
$this->view = $view; |
|
60
|
15 |
|
$this->controller = $controller; |
|
61
|
15 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Run mvc pattern. |
|
65
|
|
|
*/ |
|
66
|
14 |
|
public function run() |
|
67
|
|
|
{ |
|
68
|
|
|
//attach Oserver to Subjetc |
|
69
|
14 |
|
$this->model->attach($this->view); |
|
70
|
|
|
|
|
71
|
|
|
//run action before controller |
|
72
|
14 |
|
$this->beforeAfterController('before'); |
|
73
|
14 |
|
$this->beforeAfterControllerAction('before'); |
|
74
|
|
|
|
|
75
|
|
|
//run controller |
|
76
|
14 |
|
$this->runController(); |
|
77
|
|
|
|
|
78
|
|
|
//run action after controller |
|
79
|
14 |
|
$this->beforeAfterControllerAction('after'); |
|
80
|
14 |
|
$this->beforeAfterController('after'); |
|
81
|
|
|
|
|
82
|
|
|
//notify model changes to view |
|
83
|
14 |
|
$this->model->notify(); |
|
84
|
|
|
|
|
85
|
|
|
//run view |
|
86
|
14 |
|
$this->runView(); |
|
87
|
14 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Run action before or after controller action execution. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $when |
|
93
|
|
|
*/ |
|
94
|
14 |
|
private function beforeAfterControllerAction(string $when) |
|
95
|
|
|
{ |
|
96
|
14 |
|
$actionMethod = $when.ucfirst($this->routeAction); |
|
97
|
|
|
|
|
98
|
14 |
|
if (method_exists($this->controller, $actionMethod) && $actionMethod !== $when) { |
|
99
|
5 |
|
call_user_func([$this->controller, $actionMethod]); |
|
100
|
|
|
} |
|
101
|
14 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Run action before or after controller execution. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $when |
|
107
|
|
|
*/ |
|
108
|
14 |
|
private function beforeAfterController(string $when) |
|
109
|
|
|
{ |
|
110
|
14 |
|
if (method_exists($this->controller, $when)) { |
|
111
|
5 |
|
call_user_func([$this->controller, $when]); |
|
112
|
|
|
} |
|
113
|
14 |
|
} |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Run controller. |
|
118
|
|
|
*/ |
|
119
|
14 |
|
private function runController() |
|
120
|
|
|
{ |
|
121
|
|
|
//get route information |
|
122
|
14 |
|
$routeAction = $this->routeAction; |
|
123
|
14 |
|
$routeParam = $this->routeParam; |
|
124
|
|
|
|
|
125
|
|
|
//action - call controller passing params |
|
126
|
14 |
|
if ($routeParam) { |
|
|
|
|
|
|
127
|
10 |
|
call_user_func_array([$this->controller, $routeAction], $routeParam); |
|
128
|
10 |
|
return; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
//action - call controller |
|
132
|
4 |
|
if ($routeAction) { |
|
133
|
4 |
|
call_user_func([$this->controller, $routeAction]); |
|
134
|
|
|
} |
|
135
|
4 |
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Run view. |
|
139
|
|
|
*/ |
|
140
|
14 |
|
private function runView() |
|
141
|
|
|
{ |
|
142
|
14 |
|
if ($this->routeAction) { |
|
143
|
14 |
|
call_user_func([$this->view, $this->routeAction]); |
|
144
|
|
|
} |
|
145
|
14 |
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Return view data. |
|
149
|
|
|
*/ |
|
150
|
14 |
|
public function response() : string |
|
151
|
|
|
{ |
|
152
|
14 |
|
return $this->view->render(); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.