Completed
Push — master ( d4e951...14a860 )
by Jean-Christophe
04:01
created

Startup::getControllerSimpleName()   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 16
	public static function run(array &$config) {
28 16
		self::$config = $config;
29 16
		self::startTemplateEngine ( $config );
30 16
		if (isset ( $config ['sessionName'] ))
31 16
			USession::start ( $config ['sessionName'] );
32 16
		self::forward ( $_GET ['c'] );
33 16
	}
34
35 17
	public static function forward($url, $initialize = true, $finalize = true) {
36 17
		$u = self::parseUrl ( $url );
37 17
		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 16
			self::setCtrlNS ();
49 16
			$u [0] = self::$ctrlNS . $u [0];
50 16
			self::_preRunAction($u,$initialize,$finalize);
51
		}
52 17
	}
53
	
54 17
	private static function _preRunAction(&$u,$initialize=true,$finalize=true){
55 17
		if (\class_exists ( $u [0] )) {
56 17
			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 17
	}
62
63 17
	private static function parseUrl(&$url) {
64 17
		if (! $url) {
65 2
			$url = "_default";
66
		}
67 17
		if (UString::endswith ( $url, "/" ))
68 5
			$url = \substr ( $url, 0, strlen ( $url ) - 1 );
69 17
		self::$urlParts = \explode ( "/", $url );
70
71 17
		return self::$urlParts;
72
	}
73
74 16
	private static function startTemplateEngine(&$config) {
75
		try {
76 16
			if (isset ( $config ['templateEngine'] )) {
77 16
				$templateEngine = $config ['templateEngine'];
78 16
				$engineOptions = $config ['templateEngineOptions'] ?? array ('cache' => \ROOT . \DS . 'views/cache/' );
79 16
				$engine = new $templateEngine ( $engineOptions );
80 16
				if ($engine instanceof TemplateEngine) {
81 16
					self::$templateEngine = $engine;
82
				}
83
			}
84
		} catch ( \Exception $e ) {
85
			echo $e->getTraceAsString ();
86
		}
87 16
	}
88
89 18
	public static function runAction(array &$u, $initialize = true, $finalize = true) {
90 18
		$ctrl = $u [0];
91 18
		self::$controller = $ctrl;
92 18
		self::$action = "index";
93 18
		self::$actionParams = [ ];
94 18
		if (\sizeof ( $u ) > 1)
95 18
			self::$action = $u [1];
96 18
		if (\sizeof ( $u ) > 2)
97 6
			self::$actionParams = array_slice ( $u, 2 );
98
99 18
		$controller = new $ctrl ();
100 18
		if (! $controller instanceof Controller) {
101
			print "`{$u[0]}` isn't a controller instance.`<br/>";
102
			return;
103
		}
104
		// Dependency injection
105 18
		self::injectDependences ( $controller );
106 18
		if (! $controller->isValid ( self::$action )) {
107 5
			$controller->onInvalidControl ();
108
		} else {
109 18
			if ($initialize)
110 18
				$controller->initialize ();
111 18
			self::callController ( $controller, $u );
112 18
			if ($finalize)
113 18
				$controller->finalize ();
114
		}
115 18
	}
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 19
	public static function injectDependences($controller) {
132 19
		if (isset ( self::$config ['di'] )) {
133 19
			$di = self::$config ['di'];
134 19
			if (\is_array ( $di )) {
135 19
				foreach ( $di as $k => $v ) {
136 19
					$controller->$k = $v ( $controller );
137
				}
138
			}
139
		}
140 19
	}
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 18
	private static function callController(Controller $controller, array &$u) {
149 18
		$urlSize = sizeof ( $u );
150
		switch ($urlSize) {
151 18
			case 1 :
152 6
				$controller->index ();
153 6
				break;
154 16
			case 2 :
155 15
				$action = $u [1];
156
				// Appel de la méthode (2ème élément du tableau)
157 15
				if (\method_exists ( $controller, $action )) {
158 15
					$controller->$action ();
159
				} else {
160
					Logger::warn ( "Startup", "The method `{$action}` doesn't exists on controller `" . $u [0] . "`", "callController" );
161
				}
162 15
				break;
163
			default :
164
				// Appel de la méthode en lui passant en paramètre le reste du tableau
165 6
				\call_user_func_array ( array ($controller,$u [1] ), self::$actionParams );
166 6
				break;
167
		}
168 18
	}
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 3
	public static function getController() {
180 3
		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 5
	public static function getAction() {
192 5
		return self::$action;
193
	}
194
195 2
	public static function getActionParams() {
196 2
		return self::$actionParams;
197
	}
198
199 17
	public static function getFrameworkDir() {
200 17
		return \dirname ( __FILE__ );
201
	}
202
203 3
	public static function getApplicationDir() {
204 3
		return \dirname ( \ROOT );
205
	}
206
207 1
	public static function getApplicationName() {
208 1
		return basename ( \dirname ( \ROOT ) );
209
	}
210
}
211