Completed
Push — master ( 737095...cfbc7b )
by Jean-Christophe
01:41
created

Startup::injectDependences()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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