Completed
Push — master ( 4e6026...e0b9fa )
by Jean-Christophe
01:30
created

Startup::getModelsCompletePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
188
		if (\error_reporting() == 0) {
189
			return;
190
		}
191
		if (\error_reporting() & $severity) {
192
			throw new \ErrorException($message, 0, $severity, $filename, $lineno,$previous);
193
		}
194
	}
195
196
	public static function getController() {
197
		return self::$controller;
198
	}
199
200
	public static function getAction() {
201
		return self::$action;
202
	}
203
204
}
205