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