Completed
Push — master ( f64d9c...5ef3c4 )
by Jean-Christophe
01:47
created

Startup::getRealModelsDir()   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 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
		@\set_exception_handler(array ('Startup','errorHandler' ));
15
		self::$config=$config;
16
		self::startTemplateEngine($config);
17
18
		session_start();
19
20
		$u=self::parseUrl($url);
21
		if (($ru=Router::getRoute($url)) !== false) {
22
			if (\is_array($ru))
23
				self::runAction($ru);
24
			else
25
				echo $ru;
26
		} else {
27
			self::setCtrlNS($config);
28
			$u[0]=self::$ctrlNS . $u[0];
29
			if (\class_exists($u[0])) {
30
				self::runAction($u);
31
			} else {
32
				print "Le contrôleur `" . $u[0] . "` n'existe pas <br/>";
33
			}
34
		}
35
	}
36
37
	public static function getNS($part="controllers"){
38
		$config=self::$config;
39
		$ns=$config["mvcNS"][$part];
40
		if ($ns !== "" && $ns !== null) {
41
			$ns.="\\";
42
		}
43
		return $ns;
44
	}
45
46
	private static function setCtrlNS($config) {
0 ignored issues
show
Unused Code introduced by
The parameter $config 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...
47
		self::$ctrlNS=self::getNS();
48
	}
49
50
	private static function parseUrl(&$url) {
51
		if (!$url) {
52
			$url="_default";
53
		}
54 View Code Duplication
		if (StrUtils::endswith($url, "/"))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
55
			$url=\substr($url, 0, strlen($url) - 1);
56
		self::$urlParts=\explode("/", $url);
57
58
		return self::$urlParts;
59
	}
60
61
	private static function startTemplateEngine($config) {
62
		try {
63
			$engineOptions=array ('cache' => ROOT . DS . "views/cache/" );
64
			if (isset($config["templateEngine"])) {
65
				$templateEngine=$config["templateEngine"];
66
				if (isset($config["templateEngineOptions"])) {
67
					$engineOptions=$config["templateEngineOptions"];
68
				}
69
				$engine=new $templateEngine($engineOptions);
70
				if ($engine instanceof TemplateEngine) {
71
					self::$config["templateEngine"]=$engine;
72
				}
73
			}
74
		} catch ( \Exception $e ) {
75
			echo $e->getTraceAsString();
76
		}
77
	}
78
79
	public static function runAction($u, $initialize=true, $finalize=true) {
80
		$config=self::getConfig();
81
		$ctrl=$u[0];
82
		$controller=new $ctrl();
83
		if (!$controller instanceof Controller) {
84
			print "`{$u[0]}` n'est pas une instance de contrôleur.`<br/>";
85
			return;
86
		}
87
		// Dependency injection
88
		if (\array_key_exists("di", $config)) {
89
			$di=$config["di"];
90
			if (\is_array($di)) {
91
				foreach ( $di as $k => $v ) {
92
					$controller->$k=$v();
93
				}
94
			}
95
		}
96
97
		if ($initialize)
98
			$controller->initialize();
99
		self::callController($controller, $u);
100
		if ($finalize)
101
			$controller->finalize();
102
	}
103
104
	public static function runAsString($u, $initialize=true, $finalize=true) {
105
		\ob_start();
106
		self::runAction($u, $initialize, $finalize);
107
		return \ob_get_clean();
108
	}
109
110
	private static function callController(Controller $controller, $u) {
111
		$urlSize=sizeof($u);
112
		try {
113
			switch($urlSize) {
114
				case 1:
115
					$controller->index();
116
					break;
117
				case 2:
118
					$action=$u[1];
119
					// Appel de la méthode (2ème élément du tableau)
120
					if (\method_exists($controller, $action)) {
121
						$controller->$action();
122
					} else {
123
						print "La méthode `{$action}` n'existe pas sur le contrôleur `" . $u[0] . "`<br/>";
124
					}
125
					break;
126
				default:
127
					// Appel de la méthode en lui passant en paramètre le reste du tableau
128
					\call_user_func_array(array ($controller,$u[1] ), array_slice($u, 2));
129
					break;
130
			}
131
		} catch ( \Exception $e ) {
132
			print "Error!: " . $e->getMessage() . "<br/>";
133
		}
134
	}
135
136
	public static function getConfig() {
137
		return self::$config;
138
	}
139
140
	public static function getModelsDir() {
141
		return self::$config["mvcNS"]["models"];
142
	}
143
144
	public static function getRealModelsDir() {
145
		return \realpath(self::$config["siteUrl"]."/".self::$config["mvcNS"]["models"]);
146
	}
147
148
	public static function errorHandler($severity, $message, $filename, $lineno) {
149
		if (\error_reporting() == 0) {
150
			return;
151
		}
152
		if (\error_reporting() & $severity) {
153
			throw new \ErrorException($message, 0, $severity, $filename, $lineno);
154
		}
155
	}
156
}
157