|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace micro\controllers; |
|
4
|
|
|
|
|
5
|
|
|
use micro\utils\StrUtils; |
|
6
|
|
|
use micro\views\engine\TemplateEngine; |
|
7
|
|
|
|
|
8
|
|
|
class Startup { |
|
9
|
|
|
public static $urlParts; |
|
10
|
|
|
private static $config; |
|
11
|
|
|
private static $ctrlNS; |
|
12
|
|
|
|
|
13
|
|
|
public static function run(array &$config, $url) { |
|
14
|
|
|
self::$config=$config; |
|
15
|
|
|
self::startTemplateEngine($config); |
|
16
|
|
|
|
|
17
|
|
|
session_start(); |
|
18
|
|
|
|
|
19
|
|
|
$u=self::parseUrl($url); |
|
20
|
|
|
if (($ru=Router::getRoute($url)) !== false) { |
|
21
|
|
|
if (\is_array($ru)) |
|
22
|
|
|
self::runAction($ru); |
|
23
|
|
|
else |
|
24
|
|
|
echo $ru; |
|
25
|
|
|
} else { |
|
26
|
|
|
self::setCtrlNS(); |
|
27
|
|
|
$u[0]=self::$ctrlNS . $u[0]; |
|
28
|
|
|
if (\class_exists($u[0])) { |
|
29
|
|
|
self::runAction($u); |
|
30
|
|
|
} else { |
|
31
|
|
|
print "Le contrôleur `" . $u[0] . "` n'existe pas <br/>"; |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public static function getNS($part="controllers") { |
|
37
|
|
|
$config=self::$config; |
|
38
|
|
|
$ns=$config["mvcNS"][$part]; |
|
39
|
|
|
if ($ns !== "" && $ns !== null) { |
|
40
|
|
|
$ns.="\\"; |
|
41
|
|
|
} |
|
42
|
|
|
return $ns; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private static function setCtrlNS() { |
|
46
|
|
|
self::$ctrlNS=self::getNS(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
private static function parseUrl(&$url) { |
|
50
|
|
|
if (!$url) { |
|
51
|
|
|
$url="_default"; |
|
52
|
|
|
} |
|
53
|
|
View Code Duplication |
if (StrUtils::endswith($url, "/")) |
|
|
|
|
|
|
54
|
|
|
$url=\substr($url, 0, strlen($url) - 1); |
|
55
|
|
|
self::$urlParts=\explode("/", $url); |
|
56
|
|
|
|
|
57
|
|
|
return self::$urlParts; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
private static function startTemplateEngine($config) { |
|
61
|
|
|
try { |
|
62
|
|
|
$engineOptions=array ('cache' => ROOT . DS . "views/cache/" ); |
|
63
|
|
|
if (isset($config["templateEngine"])) { |
|
64
|
|
|
$templateEngine=$config["templateEngine"]; |
|
65
|
|
|
if (isset($config["templateEngineOptions"])) { |
|
66
|
|
|
$engineOptions=$config["templateEngineOptions"]; |
|
67
|
|
|
} |
|
68
|
|
|
$engine=new $templateEngine($engineOptions); |
|
69
|
|
|
if ($engine instanceof TemplateEngine) { |
|
70
|
|
|
self::$config["templateEngine"]=$engine; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} catch ( \Exception $e ) { |
|
74
|
|
|
echo $e->getTraceAsString(); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public static function runAction($u, $initialize=true, $finalize=true) { |
|
79
|
|
|
$config=self::getConfig(); |
|
80
|
|
|
$ctrl=$u[0]; |
|
81
|
|
|
$controller=new $ctrl(); |
|
82
|
|
|
if (!$controller instanceof Controller) { |
|
83
|
|
|
print "`{$u[0]}` n'est pas une instance de contrôleur.`<br/>"; |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
// Dependency injection |
|
87
|
|
|
if (\array_key_exists("di", $config)) { |
|
88
|
|
|
$di=$config["di"]; |
|
89
|
|
|
if (\is_array($di)) { |
|
90
|
|
|
foreach ( $di as $k => $v ) { |
|
91
|
|
|
$controller->$k=$v(); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if ($initialize) |
|
97
|
|
|
$controller->initialize(); |
|
98
|
|
|
self::callController($controller, $u); |
|
99
|
|
|
if ($finalize) |
|
100
|
|
|
$controller->finalize(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public static function runAsString($u, $initialize=true, $finalize=true) { |
|
104
|
|
|
\ob_start(); |
|
105
|
|
|
self::runAction($u, $initialize, $finalize); |
|
106
|
|
|
return \ob_get_clean(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
private static function callController(Controller $controller, $u) { |
|
110
|
|
|
$urlSize=sizeof($u); |
|
111
|
|
|
try { |
|
112
|
|
|
switch($urlSize) { |
|
113
|
|
|
case 1: |
|
114
|
|
|
$controller->index(); |
|
115
|
|
|
break; |
|
116
|
|
|
case 2: |
|
117
|
|
|
$action=$u[1]; |
|
118
|
|
|
// Appel de la méthode (2ème élément du tableau) |
|
119
|
|
|
if (\method_exists($controller, $action)) { |
|
120
|
|
|
$controller->$action(); |
|
121
|
|
|
} else { |
|
122
|
|
|
print "La méthode `{$action}` n'existe pas sur le contrôleur `" . $u[0] . "`<br/>"; |
|
123
|
|
|
} |
|
124
|
|
|
break; |
|
125
|
|
|
default: |
|
126
|
|
|
// Appel de la méthode en lui passant en paramètre le reste du tableau |
|
127
|
|
|
\call_user_func_array(array ($controller,$u[1] ), array_slice($u, 2)); |
|
128
|
|
|
break; |
|
129
|
|
|
} |
|
130
|
|
|
} catch ( \Exception $e ) { |
|
131
|
|
|
print "Error!: " . $e->getMessage() . "<br/>"; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public static function getConfig() { |
|
136
|
|
|
return self::$config; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public static function setConfig($config){ |
|
140
|
|
|
self::$config=$config; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
private static function needsKeyInConfigArray(&$result,$array,$needs){ |
|
145
|
|
|
foreach ($needs as $need){ |
|
146
|
|
|
if(!isset($array[$need]) || StrUtils::isNull($array[$need])){ |
|
147
|
|
|
$result[]=$need; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public static function checkDbConfig(){ |
|
153
|
|
|
$config=self::$config; |
|
154
|
|
|
$result=[]; |
|
155
|
|
|
$needs=["type","dbName","serverName"]; |
|
156
|
|
|
if(!isset($config["database"])){ |
|
157
|
|
|
$result[]="database"; |
|
158
|
|
|
}else{ |
|
159
|
|
|
self::needsKeyInConfigArray($result, $config["database"], $needs); |
|
160
|
|
|
} |
|
161
|
|
|
return $result; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public static function checkModelsConfig(){ |
|
165
|
|
|
$config=self::$config; |
|
166
|
|
|
$result=[]; |
|
167
|
|
|
if(!isset($config["mvcNS"])){ |
|
168
|
|
|
$result[]="mvcNS"; |
|
169
|
|
|
}else{ |
|
170
|
|
|
self::needsKeyInConfigArray($result, $config["mvcNS"], ["models"]); |
|
171
|
|
|
} |
|
172
|
|
|
return $result; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public static function getModelsDir() { |
|
176
|
|
|
return self::$config["mvcNS"]["models"]; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public static function getModelsCompletePath() { |
|
180
|
|
|
return ROOT.DS.self::getModelsDir(); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
public static function errorHandler($message = "",$code = 0,$severity = 1 ,$filename =null, int $lineno = 0,$previous = NULL) { |
|
|
|
|
|
|
184
|
|
|
if (\error_reporting() == 0) { |
|
185
|
|
|
return; |
|
186
|
|
|
} |
|
187
|
|
|
if (\error_reporting() & $severity) { |
|
188
|
|
|
throw new \ErrorException($message, 0, $severity, $filename, $lineno); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.