Passed
Push — master ( 963c04...636654 )
by Jean-Christophe
08:21
created

StartupConfigTrait::setHttpInstance()   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
	
79 2
	public static function reloadConfig(){
80 2
		$appDir=\dirname ( \ROOT );
81 2
		$filename=$appDir."/app/config/config.php";
82 2
		self::$config=include($filename);
83 2
		self::startTemplateEngine(self::$config);
84 2
		return self::$config;
85
	}
86
	
87 3
	public static function saveConfig($content){
88 3
		$appDir=\dirname ( \ROOT );
89 3
		$filename=$appDir."/app/config/config.php";
90 3
		$oldFilename=$appDir."/app/config/config.old.php";
91 3
		if (!file_exists($filename) || copy($filename, $oldFilename)) {
92 3
			return UFileSystem::save($filename,$content);
93
		}
94
		return false;
95
	}
96
	
97 21
	public static function getHttpInstance(){
98 21
		if(!isset(self::$httpInstance)){
99 20
			self::$httpInstance=new PhpHttp();
100
		}
101 21
		return self::$httpInstance;
102
	}
103
	
104
	public static function setHttpInstance(AbstractHttp $httpInstance) {
105
		self::$httpInstance = $httpInstance;
106
	}
107
	
108 34
	public static function getSessionInstance(){
109 34
		if(!isset(self::$sessionInstance)){
110 34
			self::$sessionInstance=new PhpSession();
111
		}
112 34
		return self::$sessionInstance;
113
	}
114
	
115
	public static function setSessionInstance(AbstractSession $sessionInstance) {
116
		self::$sessionInstance = $sessionInstance;
117
	}
118
119
}
120
121