Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
StartupConfigTrait.php
Go to the documentation of this file.
1<?php
2
4
9use Ubiquity\utils\base\UArray;
16
25class 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 public static function getConfig(): array {
35 return self::$config;
36 }
37
38 public static function setConfig($config): void {
39 self::$config = $config;
40 }
41
42 public static function getModelsDir(): string {
43 return \str_replace('\\', \DS, self::getNS('models'));
44 }
45
46 public static function getModelsCompletePath(): string {
47 return \ROOT . \DS . self::getModelsDir();
48 }
49
50 protected static function needsKeyInConfigArray(&$result, $array, $needs): void {
51 foreach ($needs as $need) {
52 if (!isset ($array [$need]) || UString::isNull($array [$need])) {
53 $result [] = $need;
54 }
55 }
56 }
57
58 public static function getNS($part = 'controllers'): string {
59 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 public static function checkDbConfig($offset = 'default'): array {
67 $config = self::$config;
68 $result = [];
69 $needs = ['type', 'dbName', 'serverName'];
70 if (!isset ($config ['database'])) {
71 $result [] = 'database';
72 } else {
73 self::needsKeyInConfigArray($result, DAO::getDbOffset($config, $offset), $needs);
74 }
75 return $result;
76 }
77
78 public static function checkModelsConfig(): array {
79 $config = self::$config;
80 $result = [];
81 if (!isset ($config ['mvcNS'])) {
82 $result [] = 'mvcNS';
83 } else {
84 self::needsKeyInConfigArray($result, $config ['mvcNS'], ['models']);
85 }
86 return $result;
87 }
88
89 public static function reloadConfig(bool $preserveSiteUrl = true): array {
90 $siteUrl = self::$config['siteUrl'];
91 $filename = \ROOT . self::$CONFIG_CACHE_LOCATION;
92 self::$config = include($filename);
93 if ($preserveSiteUrl) {
94 self::$config ['siteUrl'] = $siteUrl;
95 }
96 self::startTemplateEngine(self::$config);
97 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 public static function saveConfig(array $contentArray, string $configFilename = 'config') {
106 $appDir = \dirname(\ROOT);
107 $filename = $appDir . "/app/config/$configFilename.php";
108 $oldFilename = $appDir . '/app/config/config.old.php';
109 $content = "<?php\nreturn " . UArray::asPhpArray($contentArray, 'array', 1, true) . ";";
110 if (CodeUtils::isValidCode($content)) {
111 if (!\file_exists($filename) || \copy($filename, $oldFilename)) {
112 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 public static function getHttpInstance(): AbstractHttp {
128 if (!isset (self::$httpInstance)) {
129 self::$httpInstance = new PhpHttp ();
130 }
131 return self::$httpInstance;
132 }
133
134 public static function setHttpInstance(AbstractHttp $httpInstance): void {
135 self::$httpInstance = $httpInstance;
136 }
137
138 public static function getSessionInstance(): AbstractSession {
139 if (!isset (self::$sessionInstance)) {
140 self::$sessionInstance = new PhpSession ();
141 }
142 return self::$sessionInstance;
143 }
144
145 public static function setSessionInstance(AbstractSession $sessionInstance): void {
146 self::$sessionInstance = $sessionInstance;
147 }
148
149 public static function isValidUrl(string $url): bool {
150 $u = self::parseUrl($url);
151 if (\is_array(Router::getRoutes()) && ($ru = Router::getRoute($url, false, self::$config ['debug'] ?? false)) !== false) {
152 if (\is_array($ru)) {
153 if (\is_string($ru ['controller'])) {
154 return static::isValidControllerAction($ru ['controller'], $ru ['action'] ?? 'index');
155 } else {
156 return \is_callable($ru);
157 }
158 }
159 } else {
160 $u [0] = self::$ctrlNS . $u [0];
161 return static::isValidControllerAction($u [0], $u [1] ?? 'index');
162 }
163 return false;
164 }
165
166 private static function isValidControllerAction(string $controller, string $action): bool {
167 if (\class_exists($controller)) {
168 return \method_exists($controller, $action);
169 }
170 return false;
171 }
172
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 public static function getActiveDomainBase(): string {
187 return self::$activeDomainBase;
188 }
189
190 public static function resetActiveDomainBase(): void {
191 self::$activeDomainBase = '';
192 }
193}
194
Gateway class between database and object model.
Definition DAO.php:33
Ubiquity\utils\base$CodeUtils This class is part of Ubiquity.
Definition CodeUtils.php:13
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.
String utilities.
Definition UString.php:15
Ubiquity\utils\http\foundation$AbstractHttp This class is part of Ubiquity.
Ubiquity\utils\http\foundation$PhpHttp This class is part of Ubiquity.
Definition PhpHttp.php:13
Ubiquity\utils\http\session$AbstractSession This class is part of Ubiquity.