9use Ubiquity\utils\base\UArray;
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';
34 public static function getConfig(): array {
38 public static function setConfig($config): void {
39 self::$config = $config;
42 public static function getModelsDir(): string {
43 return \str_replace(
'\\', \DS, self::getNS(
'models'));
46 public static function getModelsCompletePath(): string {
47 return \ROOT . \DS . self::getModelsDir();
50 protected static function needsKeyInConfigArray(&$result, $array, $needs): void {
51 foreach ($needs as $need) {
52 if (!isset ($array [$need]) || UString::isNull($array [$need])) {
58 public static function getNS($part =
'controllers'): string {
59 return self::$activeDomainBase . ((self::$config [
'mvcNS'] [$part]) ?? $part) .
"\\";
62 protected static function setCtrlNS(): string {
63 return self::$ctrlNS = self::getNS();
66 public static function checkDbConfig($offset =
'default'): array {
67 $config = self::$config;
69 $needs = [
'type',
'dbName',
'serverName'];
70 if (!isset ($config [
'database'])) {
71 $result [] =
'database';
73 self::needsKeyInConfigArray($result, DAO::getDbOffset($config, $offset), $needs);
78 public static function checkModelsConfig(): array {
79 $config = self::$config;
81 if (!isset ($config [
'mvcNS'])) {
84 self::needsKeyInConfigArray($result, $config [
'mvcNS'], [
'models']);
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;
96 self::startTemplateEngine(self::$config);
100 public static function reloadServices(): void {
101 $config = self::$config;
102 include \ROOT .
'config/services.php';
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);
115 throw new InvalidCodeException (
'Config contains invalid code');
120 public static function updateConfig(array $content,
string $configFilename =
'config') {
121 foreach ($content as $k => $v) {
122 self::$config [$k] = $v;
124 return self::saveConfig(self::$config, $configFilename);
127 public static function getHttpInstance(): AbstractHttp {
128 if (!isset (self::$httpInstance)) {
129 self::$httpInstance =
new PhpHttp ();
131 return self::$httpInstance;
134 public static function setHttpInstance(AbstractHttp $httpInstance): void {
135 self::$httpInstance = $httpInstance;
138 public static function getSessionInstance(): AbstractSession {
139 if (!isset (self::$sessionInstance)) {
140 self::$sessionInstance =
new PhpSession ();
142 return self::$sessionInstance;
145 public static function setSessionInstance(AbstractSession $sessionInstance): void {
146 self::$sessionInstance = $sessionInstance;
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');
156 return \is_callable($ru);
160 $u [0] = self::$ctrlNS . $u [0];
161 return static::isValidControllerAction($u [0], $u [1] ??
'index');
166 private static function isValidControllerAction(
string $controller,
string $action): bool {
167 if (\class_exists($controller)) {
168 return \method_exists($controller, $action);
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);
186 public static function getActiveDomainBase(): string {
187 return self::$activeDomainBase;
190 public static function resetActiveDomainBase(): void {
191 self::$activeDomainBase =
'';
Exceptions for code checking.
Gateway class between database and object model.
Ubiquity\utils\base$CodeUtils This class is part of Ubiquity.
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.
Ubiquity\utils\http\foundation$AbstractHttp This class is part of Ubiquity.
Ubiquity\utils\http\foundation$PhpHttp This class is part of Ubiquity.
Ubiquity\utils\http\session$AbstractSession This class is part of Ubiquity.