Passed
Push — master ( d7f029...202469 )
by Jean-Christophe
15:08
created

StartupConfigTrait::getModelsCompletePath()   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
eloc 1
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Ubiquity\controllers\traits;
4
5
use Ubiquity\controllers\Router;
6
use Ubiquity\orm\DAO;
7
use Ubiquity\utils\base\CodeUtils;
8
use Ubiquity\utils\base\UArray;
9
use Ubiquity\utils\base\UFileSystem;
10
use Ubiquity\utils\base\UString;
11
use Ubiquity\utils\http\foundation\AbstractHttp;
12
use Ubiquity\utils\http\foundation\PhpHttp;
13
use Ubiquity\utils\http\session\AbstractSession;
14
use Ubiquity\utils\http\session\PhpSession;
15
16
/**
17
 * Ubiquity\controllers\traits$StartupConfigTrait
18
 * This class is part of Ubiquity
19
 *
20
 * @author jcheron <[email protected]>
21
 * @version 1.1.4
22
 *
23
 */
24
trait StartupConfigTrait {
25
	public static $config;
26
	protected static $ctrlNS;
27
	protected static $httpInstance;
28
	protected static $sessionInstance;
29
30 48
	public static function getConfig(): array {
31 48
		return self::$config;
32
	}
33
34 119
	public static function setConfig($config): void {
35 119
		self::$config = $config;
36 119
	}
37
38 2
	public static function getModelsDir(): string {
39 2
		return self::$config ['mvcNS'] ['models'];
40
	}
41
42 2
	public static function getModelsCompletePath(): string {
43 2
		return \ROOT . \DS . self::getModelsDir ();
44
	}
45
46 2
	protected static function needsKeyInConfigArray(&$result, $array, $needs): void {
47 2
		foreach ( $needs as $need ) {
48 2
			if (! isset ( $array [$need] ) || UString::isNull ( $array [$need] )) {
49
				$result [] = $need;
50
			}
51
		}
52 2
	}
53
54 47
	public static function getNS($part = 'controllers'): string {
55 47
		$ns = self::$config ['mvcNS'] [$part];
56 47
		return ($ns != null) ? $ns .= "\\" : $ns;
57
	}
58
59
	protected static function setCtrlNS(): string {
60
		return self::$ctrlNS = self::getNS ();
61
	}
62
63 2
	public static function checkDbConfig($offset = 'default'): array {
64 2
		$config = self::$config;
65 2
		$result = [ ];
66 2
		$needs = [ "type","dbName","serverName" ];
67 2
		if (! isset ( $config ["database"] )) {
68
			$result [] = "database";
69
		} else {
70 2
			self::needsKeyInConfigArray ( $result, DAO::getDbOffset ( $config, $offset ), $needs );
71
		}
72 2
		return $result;
73
	}
74
75 2
	public static function checkModelsConfig(): array {
76 2
		$config = self::$config;
77 2
		$result = [ ];
78 2
		if (! isset ( $config ['mvcNS'] )) {
79
			$result [] = "mvcNS";
80
		} else {
81 2
			self::needsKeyInConfigArray ( $result, $config ['mvcNS'], [ 'models' ] );
82
		}
83 2
		return $result;
84
	}
85
86 1
	public static function reloadConfig(): array {
87 1
		$appDir = \dirname ( \ROOT );
88 1
		$filename = $appDir . "/app/config/config.php";
89 1
		self::$config = include ($filename);
90 1
		self::startTemplateEngine ( self::$config );
91 1
		return self::$config;
92
	}
93
94
	public static function reloadServices(): void {
95
		$config = self::$config; // used in services.php
96
		include \ROOT . 'config/services.php';
97
	}
98
99 2
	public static function saveConfig(array $contentArray) {
100 2
		$appDir = \dirname ( \ROOT );
101 2
		$filename = $appDir . "/app/config/config.php";
102 2
		$oldFilename = $appDir . "/app/config/config.old.php";
103 2
		$content = "<?php\nreturn " . UArray::asPhpArray ( $contentArray, "array", 1, true ) . ";";
104 2
		if (CodeUtils::isValidCode ( $content )) {
105 2
			if (! file_exists ( $filename ) || copy ( $filename, $oldFilename )) {
106 2
				return UFileSystem::save ( $filename, $content );
107
			}
108
		} else {
109
			throw new \RuntimeException ( 'Config contains invalid code' );
110
		}
111
		return false;
112
	}
113
114
	public static function updateConfig(array $content) {
115
		foreach ( $content as $k => $v ) {
116
			self::$config [$k] = $v;
117
		}
118
		return self::saveConfig ( self::$config );
119
	}
120
121 15
	public static function getHttpInstance(): AbstractHttp {
122 15
		if (! isset ( self::$httpInstance )) {
123 14
			self::$httpInstance = new PhpHttp ();
124
		}
125 15
		return self::$httpInstance;
126
	}
127
128
	public static function setHttpInstance(AbstractHttp $httpInstance): void {
129
		self::$httpInstance = $httpInstance;
130
	}
131
132 34
	public static function getSessionInstance(): AbstractSession {
133 34
		if (! isset ( self::$sessionInstance )) {
134 34
			self::$sessionInstance = new PhpSession ();
135
		}
136 34
		return self::$sessionInstance;
137
	}
138
139
	public static function setSessionInstance(AbstractSession $sessionInstance): void {
140
		self::$sessionInstance = $sessionInstance;
141
	}
142
143
	public static function isValidUrl(string $url): bool {
144
		$u = self::parseUrl ( $url );
145
		if (\is_array ( Router::getRoutes () ) && ($ru = Router::getRoute ( $url, false, self::$config ['debug'] ?? false)) !== false) {
146
			if (\is_array ( $ru )) {
147
				if (\is_string ( $ru [0] )) {
148
					return static::isValidControllerAction ( $ru [0], $ru [1] ?? 'index');
149
				} else {
150
					return is_callable ( $ru );
151
				}
152
			}
153
		} else {
154
			$u [0] = self::$ctrlNS . $u [0];
155
			return static::isValidControllerAction ( $u [0], $u [1] ?? 'index');
156
		}
157
		return false;
158
	}
159
160
	private static function isValidControllerAction(string $controller, string $action): bool {
161
		if (\class_exists ( $controller )) {
162
			return \method_exists ( $controller, $action );
163
		}
164
		return false;
165
	}
166
}
167
168