Completed
Push — master ( ab54c8...77228d )
by Jean-Christophe
01:47
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\base\UString;
6
use Ubiquity\views\engine\TemplateEngine;
7
use Ubiquity\utils\http\USession;
8
9
class Startup {
10
	public static $urlParts;
11
	private static $config;
12
	private static $ctrlNS;
13
	private static $controller;
14
	private static $action;
15
16
	public static function run(array &$config, $url) {
17
		self::$config = $config;
18
		self::startTemplateEngine ( $config );
19
		if (isset ( $config ["sessionName"] ))
20
			USession::start ( $config ["sessionName"] );
21
		self::forward ( $url );
22
	}
23
24
	public static function forward($url) {
25
		$u = self::parseUrl ( $url );
26
		if (($ru = Router::getRoute ( $url )) !== false) {
27
			if (\is_array ( $ru ))
28
				self::runAction ( $ru );
29
			else
30
				echo $ru;
31
		} else {
32
			self::setCtrlNS ();
33
			$u [0] = self::$ctrlNS . $u [0];
34
			if (\class_exists ( $u [0] )) {
35
				self::runAction ( $u );
36
			} else {
37
				\header ( 'HTTP/1.0 404 Not Found', true, 404 );
38
				print "Le contrôleur `" . $u [0] . "` n'existe pas <br/>";
39
			}
40
		}
41
	}
42
43
	public static function getNS($part = "controllers") {
44
		$config = self::$config;
45
		$ns = $config ["mvcNS"] [$part];
46
		if ($ns !== "" && $ns !== null) {
47
			$ns .= "\\";
48
		}
49
		return $ns;
50
	}
51
52
	private static function setCtrlNS() {
53
		self::$ctrlNS = self::getNS ();
54
	}
55
56
	private static function parseUrl(&$url) {
57
		if (! $url) {
58
			$url = "_default";
59
		}
60
		if (UString::endswith ( $url, "/" ))
61
			$url = \substr ( $url, 0, strlen ( $url ) - 1 );
62
		self::$urlParts = \explode ( "/", $url );
63
64
		return self::$urlParts;
65
	}
66
67
	private static function startTemplateEngine($config) {
68
		try {
69
			$engineOptions = array ('cache' => ROOT . DS . "views/cache/" );
70
			if (isset ( $config ["templateEngine"] )) {
71
				$templateEngine = $config ["templateEngine"];
72
				if (isset ( $config ["templateEngineOptions"] )) {
73
					$engineOptions = $config ["templateEngineOptions"];
74
				}
75
				$engine = new $templateEngine ( $engineOptions );
76
				if ($engine instanceof TemplateEngine) {
77
					self::$config ["templateEngine"] = $engine;
78
				}
79
			}
80
		} catch ( \Exception $e ) {
81
			echo $e->getTraceAsString ();
82
		}
83
	}
84
85
	public static function runAction($u, $initialize = true, $finalize = true) {
86
		$config = self::getConfig ();
87
		$ctrl = $u [0];
88
		self::$controller = $ctrl;
89
		self::$action = "index";
90
		if (\sizeof ( $u ) > 1)
91
			self::$action = $u [1];
92
93
		$controller = new $ctrl ();
94
		if (! $controller instanceof Controller) {
95
			print "`{$u[0]}` n'est pas une instance de contrôleur.`<br/>";
96
			return;
97
		}
98
		// Dependency injection
99
		if (\array_key_exists ( "di", $config )) {
100
			$di = $config ["di"];
101
			if (\is_array ( $di )) {
102
				foreach ( $di as $k => $v ) {
103
					$controller->$k = $v ( $controller );
104
				}
105
			}
106
		}
107
108
		if ($initialize)
109
			$controller->initialize ();
110
		self::callController ( $controller, $u );
111
		if ($finalize)
112
			$controller->finalize ();
113
	}
114
115
	public static function runAsString($u, $initialize = true, $finalize = true) {
116
		\ob_start ();
117
		self::runAction ( $u, $initialize, $finalize );
118
		return \ob_get_clean ();
119
	}
120
121
	private static function callController(Controller $controller, $u) {
122
		$urlSize = sizeof ( $u );
123
		switch ($urlSize) {
124
			case 1 :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
125
				$controller->index ();
126
				break;
127
			case 2 :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
128
				$action = $u [1];
129
				// Appel de la méthode (2ème élément du tableau)
130
				if (\method_exists ( $controller, $action )) {
131
					$controller->$action ();
132
				} else {
133
					print "La méthode `{$action}` n'existe pas sur le contrôleur `" . $u [0] . "`<br/>";
134
				}
135
				break;
136
			default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
137
				// Appel de la méthode en lui passant en paramètre le reste du tableau
138
				\call_user_func_array ( array ($controller,$u [1] ), array_slice ( $u, 2 ) );
139
				break;
140
		}
141
	}
142
143
	public static function getConfig() {
144
		return self::$config;
145
	}
146
147
	public static function setConfig($config) {
148
		self::$config = $config;
149
	}
150
151
	private static function needsKeyInConfigArray(&$result, $array, $needs) {
152
		foreach ( $needs as $need ) {
153
			if (! isset ( $array [$need] ) || UString::isNull ( $array [$need] )) {
154
				$result [] = $need;
155
			}
156
		}
157
	}
158
159
	public static function checkDbConfig() {
160
		$config = self::$config;
161
		$result = [ ];
162
		$needs = [ "type","dbName","serverName" ];
163
		if (! isset ( $config ["database"] )) {
164
			$result [] = "database";
165
		} else {
166
			self::needsKeyInConfigArray ( $result, $config ["database"], $needs );
167
		}
168
		return $result;
169
	}
170
171
	public static function checkModelsConfig() {
172
		$config = self::$config;
173
		$result = [ ];
174
		if (! isset ( $config ["mvcNS"] )) {
175
			$result [] = "mvcNS";
176
		} else {
177
			self::needsKeyInConfigArray ( $result, $config ["mvcNS"], [ "models" ] );
178
		}
179
		return $result;
180
	}
181
182
	public static function getModelsDir() {
183
		return self::$config ["mvcNS"] ["models"];
184
	}
185
186
	public static function getModelsCompletePath() {
187
		return ROOT . DS . self::getModelsDir ();
188
	}
189
190
	public static function errorHandler($message = "", $code = 0, $severity = 1, $filename = null, int $lineno = 0, $previous = NULL) {
191
		if (\error_reporting () == 0) {
192
			return;
193
		}
194
		if (\error_reporting () & $severity) {
195
			throw new \ErrorException ( $message, 0, $severity, $filename, $lineno, $previous );
196
		}
197
	}
198
199
	public static function getController() {
200
		return self::$controller;
201
	}
202
203
	public static function getAction() {
204
		return self::$action;
205
	}
206
207
	public static function getFrameworkDir() {
208
		return \dirname ( __FILE__ );
209
	}
210
211
	public static function getApplicationDir() {
212
		return \dirname ( ROOT );
213
	}
214
	
215
	public static function getApplicationName() {
216
		return basename(\dirname ( ROOT ));
217
	}
218
}
219