Completed
Push — master ( 1f65a3...0feaa6 )
by Jean-Christophe
01:28
created

Startup::getAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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