Completed
Push — master ( fcf532...996d51 )
by Jean-Christophe
05:34
created

StartupConfigTrait::reloadConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 6
ccs 6
cts 6
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
8
trait StartupConfigTrait {
9
	protected static $config;
10
	protected static $ctrlNS;
11
	
12 15
	public static function getConfig() {
13 15
		return self::$config;
14
	}
15
	
16 1
	public static function setConfig($config) {
17 1
		self::$config = $config;
18 1
	}
19
	
20 1
	public static function getModelsDir() {
21 1
		return self::$config ["mvcNS"] ["models"];
22
	}
23
	
24 1
	public static function getModelsCompletePath() {
25 1
		return \ROOT . \DS . self::getModelsDir ();
26
	}
27
	
28 2
	protected static function needsKeyInConfigArray(&$result, $array, $needs) {
29 2
		foreach ( $needs as $need ) {
30 2
			if (! isset ( $array [$need] ) || UString::isNull ( $array [$need] )) {
31 1
				$result [] = $need;
32
			}
33
		}
34 2
	}
35
	
36 19
	public static function getNS($part = "controllers") {
37 19
		$config = self::$config;
38 19
		$ns = $config ["mvcNS"] [$part];
39 19
		if ($ns !== "" && $ns !== null) {
40 19
			$ns .= "\\";
41
		}
42 19
		return $ns;
43
	}
44
	
45 19
	protected static function setCtrlNS() {
46 19
		self::$ctrlNS = self::getNS ();
47 19
	}
48
	
49 2
	public static function checkDbConfig() {
50 2
		$config = self::$config;
51 2
		$result = [ ];
52 2
		$needs = [ "type","dbName","serverName" ];
53 2
		if (! isset ( $config ["database"] )) {
54
			$result [] = "database";
55
		} else {
56 2
			self::needsKeyInConfigArray ( $result, $config ["database"], $needs );
57
		}
58 2
		return $result;
59
	}
60
	
61 2
	public static function checkModelsConfig() {
62 2
		$config = self::$config;
63 2
		$result = [ ];
64 2
		if (! isset ( $config ["mvcNS"] )) {
65
			$result [] = "mvcNS";
66
		} else {
67 2
			self::needsKeyInConfigArray ( $result, $config ["mvcNS"], [ "models" ] );
68
		}
69 2
		return $result;
70
	}
71
	
72
	
73 1
	public static function reloadConfig(){
74 1
		$appDir=\dirname ( \ROOT );
75 1
		$filename=$appDir."/app/config/config.php";
76 1
		self::$config=include($filename);
77 1
		self::startTemplateEngine(self::$config);
78 1
		return self::$config;
79
	}
80
	
81 1
	public static function saveConfig($content){
82 1
		$appDir=\dirname ( \ROOT );
83 1
		$filename=$appDir."/app/config/config.php";
84 1
		$oldFilename=$appDir."/app/config/config.old.php";
85 1
		if (!file_exists($filename) || copy($filename, $oldFilename)) {
86 1
			return UFileSystem::save($filename,$content);
87
		}
88
		return false;
89
	}
90
}
91
92