Passed
Push — master ( 4d0f3d...b95030 )
by Jean-Christophe
02:31
created

Startup::parseUrl()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 1
dl 0
loc 9
ccs 5
cts 7
cp 0.7143
crap 3.2098
rs 10
c 0
b 0
f 0
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
use Ubiquity\log\Logger;
9
use Ubiquity\controllers\traits\StartupConfigTrait;
10
11
class Startup {
12
	use StartupConfigTrait;
13
	
14
	public static $urlParts;
15
	public static $templateEngine;
16
	private static $controller;
17
	private static $action;
18
	private static $actionParams;
19
20 1
	public static function run(array &$config) {
21 1
		self::$config = $config;
22 1
		self::startTemplateEngine ( $config );
23 1
		if (isset ( $config ['sessionName'] ))
24 1
			USession::start ( $config ['sessionName'] );
25 1
		self::forward ( $_GET['c'] );
26 1
	}
27
28 2
	public static function forward($url,$initialize=true,$finalize=true) {
29 2
		$u = self::parseUrl ( $url );
30 2
		if (($ru = Router::getRoute ( $url )) !== false) {
31
			if (\is_array ( $ru )){
32
				self::runAction ( $ru ,$initialize,$finalize);
33
			}else{
34
				echo $ru;//Displays route response from cache
35
			}
36
		} else {
37 2
			self::setCtrlNS ();
38 2
			$u [0] = self::$ctrlNS . $u [0];
39 2
			if (\class_exists ( $u [0] )) {
40 2
				self::runAction ( $u ,$initialize,$finalize);
41
			} else {
42
				\header ( 'HTTP/1.0 404 Not Found', true, 404 );
43
				Logger::warn("Startup", "The controller `" . $u [0] . "` doesn't exists! <br/>","forward");
44
			}
45
		}
46 2
	}
47
48 2
	private static function parseUrl(&$url) {
49 2
		if (! $url) {
50
			$url = "_default";
51
		}
52 2
		if (UString::endswith ( $url, "/" ))
53
			$url = \substr ( $url, 0, strlen ( $url ) - 1 );
54 2
		self::$urlParts = \explode ( "/", $url );
55
56 2
		return self::$urlParts;
57
	}
58
59 1
	private static function startTemplateEngine(&$config) {
60
		try {
61 1
			if (isset ( $config ['templateEngine'] )) {
62
				$templateEngine = $config ['templateEngine'];
63
				$engineOptions = $config ['templateEngineOptions']??array ('cache' => \ROOT . \DS . 'views/cache/' );
64
				$engine = new $templateEngine ( $engineOptions );
65
				if ($engine instanceof TemplateEngine) {
66 1
					self::$templateEngine = $engine;
67
				}
68
			}
69
		} catch ( \Exception $e ) {
70
			echo $e->getTraceAsString ();
71
		}
72 1
	}
73
74 2
	public static function runAction(array &$u, $initialize = true, $finalize = true) {
75 2
		$ctrl = $u [0];
76 2
		self::$controller = $ctrl;
77 2
		self::$action = "index";
78 2
		if (\sizeof ( $u ) > 1)
79 1
			self::$action = $u [1];
80 2
		if (\sizeof ( $u ) > 2)
81
			self::$actionParams=array_slice ( $u, 2 );
82
83 2
		$controller = new $ctrl ();
84 2
		if (! $controller instanceof Controller) {
85
			print "`{$u[0]}` isn't a controller instance.`<br/>";
86
			return;
87
		}
88
		// Dependency injection
89 2
		self::injectDependences($controller);
90 2
		if(!$controller->isValid(self::$action)){
91
			$controller->onInvalidControl();
92
		}else{
93 2
			if ($initialize)
94 2
				$controller->initialize ();
95 2
			self::callController ( $controller, $u );
96 2
			if ($finalize)
97 2
				$controller->finalize ();
98
		}
99 2
	}
100
	
101 2
	public static function injectDependences($controller){
102 2
		if (isset(self::$config['di'])) {
103 2
			$di = self::$config['di'];
104 2
			if (\is_array ( $di )) {
105 2
				foreach ( $di as $k => $v ) {
106
					$controller->$k = $v ( $controller );
107
				}
108
			}
109
		}
110 2
	}
111
112
	public static function runAsString(array &$u, $initialize = true, $finalize = true) {
113
		\ob_start ();
114
		self::runAction ( $u, $initialize, $finalize );
115
		return \ob_get_clean ();
116
	}
117
118 2
	private static function callController(Controller $controller, array &$u) {
119 2
		$urlSize = sizeof ( $u );
120
		switch ($urlSize) {
121 2
			case 1 :
122 1
				$controller->index ();
123 1
				break;
124 1
			case 2 :
125 1
				$action = $u [1];
126
				// Appel de la méthode (2ème élément du tableau)
127 1
				if (\method_exists ( $controller, $action )) {
128 1
					$controller->$action ();
129
				} else {
130
					Logger::warn("Startup","The method `{$action}` doesn't exists on controller `" . $u [0] . "`","callController");
131
				}
132 1
				break;
133
			default :
134
				// Appel de la méthode en lui passant en paramètre le reste du tableau
135
				\call_user_func_array ( array ($controller,$u [1] ), self::$actionParams );
136
				break;
137
		}
138 2
	}
139
140
	public static function errorHandler($message = "", $code = 0, $severity = 1, $filename = null, int $lineno = 0, $previous = NULL) {
141
		if (\error_reporting () == 0) {
142
			return;
143
		}
144
		if (\error_reporting () & $severity) {
145
			throw new \ErrorException ( $message, 0, $severity, $filename, $lineno, $previous );
146
		}
147
	}
148
149 1
	public static function getController() {
150 1
		return self::$controller;
151
	}
152
	
153
	public static function getControllerSimpleName() {
154
		return (new \ReflectionClass(self::$controller))->getShortName();
155
	}
156
	
157
	public static function getViewNameFileExtension(){
158
		return "html";
159
	}
160
161 1
	public static function getAction() {
162 1
		return self::$action;
163
	}
164
	
165 1
	public static function getActionParams() {
166 1
		return self::$actionParams;
167
	}
168
169
	public static function getFrameworkDir() {
170
		return \dirname ( __FILE__ );
171
	}
172
173
	public static function getApplicationDir() {
174
		return \dirname ( \ROOT );
175
	}
176
	
177
	public static function getApplicationName() {
178
		return basename(\dirname ( \ROOT ));
179
	}
180
}
181