Test Failed
Push — master ( 1c1110...a9a6c6 )
by Jean-Christophe
06:40
created

Startup::runCallable()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

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