Passed
Branch master (7dd754)
by Jean-Christophe
16:28
created

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