1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers\traits; |
4
|
|
|
|
5
|
|
|
use Ubiquity\controllers\Router; |
6
|
|
|
use Ubiquity\orm\DAO; |
7
|
|
|
use Ubiquity\utils\base\CodeUtils; |
8
|
|
|
use Ubiquity\utils\base\UArray; |
9
|
|
|
use Ubiquity\utils\base\UFileSystem; |
10
|
|
|
use Ubiquity\utils\base\UString; |
11
|
|
|
use Ubiquity\utils\http\foundation\AbstractHttp; |
12
|
|
|
use Ubiquity\utils\http\foundation\PhpHttp; |
13
|
|
|
use Ubiquity\utils\http\session\AbstractSession; |
14
|
|
|
use Ubiquity\utils\http\session\PhpSession; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Ubiquity\controllers\traits$StartupConfigTrait |
18
|
|
|
* This class is part of Ubiquity |
19
|
|
|
* |
20
|
|
|
* @author jcheron <[email protected]> |
21
|
|
|
* @version 1.1.6 |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
trait StartupConfigTrait { |
25
|
|
|
public static $config; |
26
|
|
|
protected static $ctrlNS; |
27
|
|
|
protected static $httpInstance; |
28
|
|
|
protected static $sessionInstance; |
29
|
|
|
protected static $activeDomainBase=''; |
30
|
|
|
|
31
|
55 |
|
public static function getConfig(): array { |
32
|
55 |
|
return self::$config; |
33
|
|
|
} |
34
|
|
|
|
35
|
219 |
|
public static function setConfig($config): void { |
36
|
219 |
|
self::$config = $config; |
37
|
219 |
|
} |
38
|
|
|
|
39
|
2 |
|
public static function getModelsDir(): string { |
40
|
2 |
|
return str_replace('\\',\DS,self::getNS('models')); |
41
|
|
|
} |
42
|
|
|
|
43
|
2 |
|
public static function getModelsCompletePath(): string { |
44
|
2 |
|
return \ROOT . \DS . self::getModelsDir (); |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
protected static function needsKeyInConfigArray(&$result, $array, $needs): void { |
48
|
2 |
|
foreach ( $needs as $need ) { |
49
|
2 |
|
if (! isset ( $array [$need] ) || UString::isNull ( $array [$need] )) { |
50
|
|
|
$result [] = $need; |
51
|
|
|
} |
52
|
|
|
} |
53
|
2 |
|
} |
54
|
|
|
|
55
|
67 |
|
public static function getNS($part = 'controllers'): string { |
56
|
67 |
|
return self::$activeDomainBase.((self::$config ['mvcNS'] [$part])??$part)."\\"; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected static function setCtrlNS(): string { |
60
|
|
|
return self::$ctrlNS = self::getNS (); |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
public static function checkDbConfig($offset = 'default'): array { |
64
|
2 |
|
$config = self::$config; |
65
|
2 |
|
$result = [ ]; |
66
|
2 |
|
$needs = [ 'type','dbName','serverName' ]; |
67
|
2 |
|
if (! isset ( $config ['database'] )) { |
68
|
|
|
$result [] = 'database'; |
69
|
|
|
} else { |
70
|
2 |
|
self::needsKeyInConfigArray ( $result, DAO::getDbOffset ( $config, $offset ), $needs ); |
71
|
|
|
} |
72
|
2 |
|
return $result; |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
public static function checkModelsConfig(): array { |
76
|
2 |
|
$config = self::$config; |
77
|
2 |
|
$result = [ ]; |
78
|
2 |
|
if (! isset ( $config ['mvcNS'] )) { |
79
|
|
|
$result [] = 'mvcNS'; |
80
|
|
|
} else { |
81
|
2 |
|
self::needsKeyInConfigArray ( $result, $config ['mvcNS'], [ 'models' ] ); |
82
|
|
|
} |
83
|
2 |
|
return $result; |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
public static function reloadConfig(): array { |
87
|
2 |
|
$appDir = \dirname ( \ROOT ); |
88
|
2 |
|
$filename = $appDir . '/app/config/config.php'; |
89
|
2 |
|
self::$config = include ($filename); |
90
|
2 |
|
self::startTemplateEngine ( self::$config ); |
91
|
2 |
|
return self::$config; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public static function reloadServices(): void { |
95
|
|
|
$config = self::$config; // used in services.php |
96
|
|
|
include \ROOT . 'config/services.php'; |
97
|
|
|
} |
98
|
|
|
|
99
|
3 |
|
public static function saveConfig(array $contentArray) { |
100
|
3 |
|
$appDir = \dirname ( \ROOT ); |
101
|
3 |
|
$filename = $appDir . '/app/config/config.php'; |
102
|
3 |
|
$oldFilename = $appDir . '/app/config/config.old.php'; |
103
|
3 |
|
$content = "<?php\nreturn " . UArray::asPhpArray ( $contentArray, 'array', 1, true ) . ";"; |
104
|
3 |
|
if (CodeUtils::isValidCode ( $content )) { |
105
|
3 |
|
if (! \file_exists ( $filename ) || \copy ( $filename, $oldFilename )) { |
106
|
3 |
|
return UFileSystem::save ( $filename, $content ); |
107
|
|
|
} |
108
|
|
|
} else { |
109
|
|
|
throw new \RuntimeException ( 'Config contains invalid code' ); |
110
|
|
|
} |
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public static function updateConfig(array $content) { |
115
|
|
|
foreach ( $content as $k => $v ) { |
116
|
|
|
self::$config [$k] = $v; |
117
|
|
|
} |
118
|
|
|
return self::saveConfig ( self::$config ); |
119
|
|
|
} |
120
|
|
|
|
121
|
26 |
|
public static function getHttpInstance(): AbstractHttp { |
122
|
26 |
|
if (! isset ( self::$httpInstance )) { |
123
|
20 |
|
self::$httpInstance = new PhpHttp (); |
124
|
|
|
} |
125
|
26 |
|
return self::$httpInstance; |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
public static function setHttpInstance(AbstractHttp $httpInstance): void { |
129
|
1 |
|
self::$httpInstance = $httpInstance; |
130
|
1 |
|
} |
131
|
|
|
|
132
|
40 |
|
public static function getSessionInstance(): AbstractSession { |
133
|
40 |
|
if (! isset ( self::$sessionInstance )) { |
134
|
39 |
|
self::$sessionInstance = new PhpSession (); |
135
|
|
|
} |
136
|
40 |
|
return self::$sessionInstance; |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
public static function setSessionInstance(AbstractSession $sessionInstance): void { |
140
|
1 |
|
self::$sessionInstance = $sessionInstance; |
141
|
1 |
|
} |
142
|
|
|
|
143
|
1 |
|
public static function isValidUrl(string $url): bool { |
144
|
1 |
|
$u = self::parseUrl ( $url ); |
145
|
1 |
|
if (\is_array ( Router::getRoutes () ) && ($ru = Router::getRoute ( $url, false, self::$config ['debug'] ?? false)) !== false) { |
146
|
1 |
|
if (\is_array ( $ru )) { |
147
|
1 |
|
if (\is_string ( $ru ['controller'] )) { |
148
|
1 |
|
return static::isValidControllerAction ( $ru ['controller'], $ru ['action'] ?? 'index'); |
149
|
|
|
} else { |
150
|
|
|
return \is_callable ( $ru ); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} else { |
154
|
1 |
|
$u [0] = self::$ctrlNS . $u [0]; |
155
|
1 |
|
return static::isValidControllerAction ( $u [0], $u [1] ?? 'index'); |
156
|
|
|
} |
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
|
160
|
1 |
|
private static function isValidControllerAction(string $controller, string $action): bool { |
161
|
1 |
|
if (\class_exists ( $controller )) { |
162
|
1 |
|
return \method_exists ( $controller, $action ); |
163
|
|
|
} |
164
|
1 |
|
return false; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Sets the active domain for a Domain Driven Design approach. |
169
|
|
|
* @param string $domain The new active domain name |
170
|
|
|
* @param string $base The base folder for domains |
171
|
|
|
*/ |
172
|
|
|
public static function setActiveDomainBase(string $domain,string $base='domains'): void { |
173
|
|
|
self::$activeDomainBase = $base . '\\' . \trim($domain, '\\') . '\\'; |
174
|
|
|
if(isset(self::$templateEngine)){ |
175
|
|
|
$viewFolder=\realpath( \str_replace('\\',\DS,\ROOT.self::$activeDomainBase.'views')); |
176
|
|
|
self::$templateEngine->setPaths([$viewFolder],$domain); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
50 |
|
public static function getActiveDomainBase(): string { |
181
|
50 |
|
return self::$activeDomainBase; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public static function resetActiveDomainBase(): void { |
185
|
|
|
self::$activeDomainBase=''; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|