Passed
Push — master ( a02cde...eb9e44 )
by Jean-Christophe
09:52
created

StartupConfigTrait::setSessionInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
12
trait StartupConfigTrait {
13
	protected static $config;
14
	protected static $ctrlNS;
15
	protected static $httpInstance;
16
	protected static $sessionInstance;
17
18 46
	public static function getConfig() {
19 46
		return self::$config;
20
	}
21
22 10
	public static function setConfig($config) {
23 10
		self::$config = $config;
24 10
	}
25
26 2
	public static function getModelsDir() {
27 2
		return self::$config ['mvcNS'] ['models'];
28
	}
29
30 2
	public static function getModelsCompletePath() {
31 2
		return \ROOT . \DS . self::getModelsDir ();
32
	}
33
34 2
	protected static function needsKeyInConfigArray(&$result, $array, $needs) {
35 2
		foreach ( $needs as $need ) {
36 2
			if (! isset ( $array [$need] ) || UString::isNull ( $array [$need] )) {
37 1
				$result [] = $need;
38
			}
39
		}
40 2
	}
41
42 37
	public static function getNS($part = 'controllers') {
43 37
		$config = self::$config;
44 37
		$ns = $config ['mvcNS'] [$part];
45 37
		if ($ns !== "" && $ns !== null) {
46 37
			$ns .= "\\";
47
		}
48 37
		return $ns;
49
	}
50
51 35
	protected static function setCtrlNS() {
52 35
		self::$ctrlNS = self::getNS ();
53 35
	}
54
55 2
	public static function checkDbConfig() {
56 2
		$config = self::$config;
57 2
		$result = [ ];
58 2
		$needs = [ 'type','dbName','serverName' ];
59 2
		if (! isset ( $config ['database'] )) {
60
			$result [] = 'database';
61
		} else {
62 2
			self::needsKeyInConfigArray ( $result, $config ['database'], $needs );
63
		}
64 2
		return $result;
65
	}
66
67 2
	public static function checkModelsConfig() {
68 2
		$config = self::$config;
69 2
		$result = [ ];
70 2
		if (! isset ( $config ['mvcNS'] )) {
71
			$result [] = "mvcNS";
72
		} else {
73 2
			self::needsKeyInConfigArray ( $result, $config ['mvcNS'], [ 'models' ] );
74
		}
75 2
		return $result;
76
	}
77
78 2
	public static function reloadConfig() {
79 2
		$appDir = \dirname ( \ROOT );
80 2
		$filename = $appDir . "/app/config/config.php";
81 2
		self::$config = include ($filename);
82 2
		self::startTemplateEngine ( self::$config );
83 2
		return self::$config;
84
	}
85
86
	public static function reloadServices() {
87
		$config = self::$config; // used in services.php
88
		include \ROOT . 'config/services.php';
89
	}
90
91 3
	public static function saveConfig($content) {
92 3
		$appDir = \dirname ( \ROOT );
93 3
		$filename = $appDir . "/app/config/config.php";
94 3
		$oldFilename = $appDir . "/app/config/config.old.php";
95 3
		if (! file_exists ( $filename ) || copy ( $filename, $oldFilename )) {
96 3
			return UFileSystem::save ( $filename, $content );
97
		}
98
		return false;
99
	}
100
101 21
	public static function getHttpInstance() {
102 21
		if (! isset ( self::$httpInstance )) {
103 20
			self::$httpInstance = new PhpHttp ();
104
		}
105 21
		return self::$httpInstance;
106
	}
107
108
	public static function setHttpInstance(AbstractHttp $httpInstance) {
109
		self::$httpInstance = $httpInstance;
110
	}
111
112 34
	public static function getSessionInstance() {
113 34
		if (! isset ( self::$sessionInstance )) {
114 34
			self::$sessionInstance = new PhpSession ();
115
		}
116 34
		return self::$sessionInstance;
117
	}
118
119
	public static function setSessionInstance(AbstractSession $sessionInstance) {
120
		self::$sessionInstance = $sessionInstance;
121
	}
122
}
123
124