Passed
Push — master ( f43320...147f38 )
by Jean-Christophe
03:14
created

Startup::getAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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