Passed
Branch master (941c30)
by Jean-Christophe
08:38
created

StartupConfigTrait::checkModelsConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

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