Completed
Push — master ( 499e80...f97bb0 )
by Jean-Christophe
01:49
created

Startup::errorHandler()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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