|
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
|
|
|
public static function getConfig() { |
|
13
|
|
|
return self::$config; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
public static function setConfig($config) { |
|
17
|
|
|
self::$config = $config; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public static function getModelsDir() { |
|
21
|
|
|
return self::$config ["mvcNS"] ["models"]; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public static function getModelsCompletePath() { |
|
25
|
|
|
return ROOT . DS . self::getModelsDir (); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
View Code Duplication |
protected static function needsKeyInConfigArray(&$result, $array, $needs) { |
|
|
|
|
|
|
29
|
|
|
foreach ( $needs as $need ) { |
|
30
|
|
|
if (! isset ( $array [$need] ) || UString::isNull ( $array [$need] )) { |
|
31
|
|
|
$result [] = $need; |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
View Code Duplication |
public static function getNS($part = "controllers") { |
|
|
|
|
|
|
37
|
|
|
$config = self::$config; |
|
38
|
|
|
$ns = $config ["mvcNS"] [$part]; |
|
39
|
|
|
if ($ns !== "" && $ns !== null) { |
|
40
|
|
|
$ns .= "\\"; |
|
41
|
|
|
} |
|
42
|
|
|
return $ns; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected static function setCtrlNS() { |
|
46
|
|
|
self::$ctrlNS = self::getNS (); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
View Code Duplication |
public static function checkDbConfig() { |
|
|
|
|
|
|
50
|
|
|
$config = self::$config; |
|
51
|
|
|
$result = [ ]; |
|
52
|
|
|
$needs = [ "type","dbName","serverName" ]; |
|
53
|
|
|
if (! isset ( $config ["database"] )) { |
|
54
|
|
|
$result [] = "database"; |
|
55
|
|
|
} else { |
|
56
|
|
|
self::needsKeyInConfigArray ( $result, $config ["database"], $needs ); |
|
57
|
|
|
} |
|
58
|
|
|
return $result; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
View Code Duplication |
public static function checkModelsConfig() { |
|
|
|
|
|
|
62
|
|
|
$config = self::$config; |
|
63
|
|
|
$result = [ ]; |
|
64
|
|
|
if (! isset ( $config ["mvcNS"] )) { |
|
65
|
|
|
$result [] = "mvcNS"; |
|
66
|
|
|
} else { |
|
67
|
|
|
self::needsKeyInConfigArray ( $result, $config ["mvcNS"], [ "models" ] ); |
|
68
|
|
|
} |
|
69
|
|
|
return $result; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
View Code Duplication |
public static function reloadConfig(){ |
|
|
|
|
|
|
74
|
|
|
$appDir=\dirname ( ROOT ); |
|
75
|
|
|
$filename=$appDir."/app/config/config.php"; |
|
76
|
|
|
self::$config=include($filename); |
|
77
|
|
|
self::startTemplateEngine(self::$config); |
|
78
|
|
|
return self::$config; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
View Code Duplication |
public static function saveConfig($content){ |
|
|
|
|
|
|
82
|
|
|
$appDir=\dirname ( ROOT ); |
|
83
|
|
|
$filename=$appDir."/app/config/config.php"; |
|
84
|
|
|
$oldFilename=$appDir."/app/config/config.old.php"; |
|
85
|
|
|
if (!file_exists($filename) || copy($filename, $oldFilename)) { |
|
86
|
|
|
return UFileSystem::save($filename,$content); |
|
87
|
|
|
} |
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.