|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use \FMUP\Request; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Classe d'initialisation du framework |
|
7
|
|
|
* @deprecated use \FMUP\Framework instead |
|
8
|
|
|
*/ |
|
9
|
|
|
class Framework |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
use \FMUP\Sapi\OptionalTrait; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var Request |
|
15
|
|
|
*/ |
|
16
|
|
|
private $request; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Request\Factory |
|
19
|
|
|
*/ |
|
20
|
|
|
private $requestFactory; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @return Request |
|
24
|
|
|
*/ |
|
25
|
|
|
public function getRequest() |
|
26
|
|
|
{ |
|
27
|
|
|
if (!$this->request) { |
|
28
|
|
|
$this->request = $this->getRequestFactory()->get(); |
|
29
|
|
|
} |
|
30
|
|
|
return $this->request; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Returns defined request factory |
|
35
|
|
|
* @return Request\Factory |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getRequestFactory() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->requestFactory = $this->requestFactory ?: new Request\Factory(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Define request factory |
|
44
|
|
|
* @param Request\Factory|null $factory |
|
45
|
|
|
* @return $this |
|
46
|
|
|
*/ |
|
47
|
|
|
public function setRequestFactory(Request\Factory $factory = null) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->requestFactory = $factory; |
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param Request $request |
|
55
|
|
|
* @return $this |
|
56
|
|
|
*/ |
|
57
|
|
|
public function setRequest(Request $request) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->request = $request; |
|
60
|
|
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function initialize() |
|
64
|
|
|
{ |
|
65
|
|
|
if (!defined('APPLICATION')) { |
|
66
|
|
|
throw new \FMUP\Exception("La variable APPLICATION doit être définie."); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$this->registerErrorHandler(); |
|
70
|
|
|
$this->registerShutdownFunction(); |
|
71
|
|
|
$this->dispatch(); |
|
72
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Allow overwriting of an eventual pre treatment |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function preDispatch() |
|
79
|
|
|
{ |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return $this |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function dispatch() |
|
86
|
|
|
{ |
|
87
|
|
|
list($controllerName, $action) = $this->getRoute(); |
|
88
|
|
|
$this->instantiate($controllerName, $action); |
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Allow overwriting of an eventual post treatment |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function postDispatch() |
|
96
|
|
|
{ |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
protected function instantiate($sys_controller, $sys_function) |
|
100
|
|
|
{ |
|
101
|
|
|
// Création d'une instance du controlleur |
|
102
|
|
|
$controllerName = \FMUP\StringHandling::toCamelCase($sys_controller); |
|
103
|
|
|
|
|
104
|
|
|
/** @var $controllerInstance \FMUP\Controller */ |
|
105
|
|
|
$controllerInstance = new $controllerName(); |
|
106
|
|
|
$controllerInstance->preFilter($sys_function); |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
if (method_exists($controllerInstance, $sys_function)) { |
|
109
|
|
|
call_user_func(array($controllerInstance, $sys_function)); |
|
110
|
|
|
} else { |
|
111
|
|
|
throw new \FMUP\Exception\Status\NotFound("Fonction introuvable : $sys_function"); |
|
112
|
|
|
} |
|
113
|
|
|
$controllerInstance->postFilter(); |
|
|
|
|
|
|
114
|
|
|
return $controllerInstance; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Sends exception in case of error |
|
119
|
|
|
* @param int $code |
|
120
|
|
|
* @param string $msg |
|
121
|
|
|
* @throws \FMUP\Exception |
|
122
|
|
|
* @deprecated |
|
123
|
|
|
*/ |
|
124
|
|
|
public function errorHandler($code, $msg, $errFile = null, $errLine = 0, array $errContext = array()) |
|
125
|
|
|
{ |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Cette fonction sera lancée à la fin du script quel que soit la cause (fin normale ou erreur) |
|
130
|
|
|
* Elle nous permet de récupérer les erreurs fatales qui sont ignorées par la fonction précédente |
|
131
|
|
|
* @deprecated maybe you want to override this |
|
132
|
|
|
*/ |
|
133
|
|
|
public function shutDown() |
|
134
|
|
|
{ |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Cette fonction récupère le controleur appelé dans l'URL |
|
139
|
|
|
* Elle va aussi gérer si l'utilisateur doit se connecter ou si le site est en maintenance |
|
140
|
|
|
* (et changer le controleur en conséquence) |
|
141
|
|
|
* @return array : Un tableau contenant le dossier, le controleur et la fonction à appeler |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getRoute() |
|
144
|
|
|
{ |
|
145
|
|
|
preg_match("/^(.*\/)?([0-9a-zA-Z\-_]*)\/([0-9a-zA-Z\-_]*)$/", $this->getRequest()->getRequestUri(), $matches); |
|
146
|
|
|
if (count($matches) < 3) { |
|
147
|
|
|
$this->getRouteError(null, null); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$sys_directory = $matches[1]; |
|
151
|
|
|
$sys_controller = "ctrl_" . $matches[2]; |
|
152
|
|
|
$sys_function = \FMUP\StringHandling::toCamelCase($matches[3]); |
|
153
|
|
|
|
|
154
|
|
|
if (!class_exists(\FMUP\StringHandling::toCamelCase($sys_controller)) || |
|
155
|
|
|
!is_callable(array(\FMUP\StringHandling::toCamelCase($sys_controller), $sys_function)) |
|
156
|
|
|
) { |
|
157
|
|
|
$this->getRouteError($sys_directory, $sys_controller); |
|
158
|
|
|
} |
|
159
|
|
|
return array(\FMUP\StringHandling::toCamelCase($sys_controller), $sys_function); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param string $directory |
|
164
|
|
|
* @param string $controller |
|
165
|
|
|
* @throws \FMUP\Exception\Status\NotFound |
|
166
|
|
|
*/ |
|
167
|
|
|
protected function getRouteError($directory, $controller) |
|
168
|
|
|
{ |
|
169
|
|
|
throw new \FMUP\Exception\Status\NotFound( |
|
170
|
|
|
"Controlleur introuvable : $directory$controller " . |
|
171
|
|
|
" (application/" . APPLICATION . "/controller/$directory$controller.php)" |
|
172
|
|
|
); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @return $this |
|
177
|
|
|
*/ |
|
178
|
|
|
protected function registerErrorHandler() |
|
179
|
|
|
{ |
|
180
|
|
|
set_error_handler(array($this, 'errorHandler')); |
|
181
|
|
|
return $this; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @return $this |
|
186
|
|
|
*/ |
|
187
|
|
|
protected function registerShutdownFunction() |
|
188
|
|
|
{ |
|
189
|
|
|
register_shutdown_function(array($this, 'shutDown')); |
|
190
|
|
|
return $this; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.