Passed
Branch master (9957c5)
by Jean-Christophe
12:44
created

StartupConfigTrait::getHttpInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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