Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
DDDManager.php
Go to the documentation of this file.
1<?php
2
3
5
10
21 private static string $base='domains';
22 private static string $activeDomain='';
23
24 private static function getNamespace(string $type='controllers'): string{
25 $prefix='';
26 if(self::$activeDomain!='') {
27 $prefix = self::$base . '\\' . self::$activeDomain . '\\';
28 }
29 return $prefix.((Startup::$config['mvcNS'][$type]) ?? $type);
30 }
31
36 public static function start(): void{
37 self::$base=Startup::$config['mvcNS']['domains']??'domains';
38 }
39
45 public static function setDomain(string $domain): void {
46 self::$activeDomain = $domain;
47 Startup::setActiveDomainBase($domain, self::$base);
48 }
49
53 public static function resetActiveDomain(): void {
54 self::$activeDomain='';
55 Startup::resetActiveDomainBase();
56 }
57
63 public static function getDomains(): array {
64 return \array_map('basename', \glob(\ROOT.self::$base . '/*' , \GLOB_ONLYDIR));
65 }
66
71 public static function hasDomains(): bool {
72 return \file_exists(\ROOT.self::$base) && \count(self::getDomains())>0;
73 }
74
81 public static function domainExists(string $domain): bool {
82 $domains=self::getDomains();
83 return \array_search($domain,$domains)!==false;
84 }
85
90 public static function getActiveDomain(): string {
91 return self::$activeDomain;
92 }
93
99 public static function getActiveViewFolder(): string {
100 if(self::$activeDomain != '' && \file_exists($folder = \ROOT . self::$base . \DS . self::$activeDomain . \DS . 'views' . \DS)) {
101 return $folder;
102 }
103 return \ROOT.'views'.\DS;
104 }
105
111 public static function getViewNamespace(): string {
112 if(($activeDomain=self::$activeDomain)!=''){
113 return '@'.$activeDomain.'/';
114 }
115 return '';
116 }
117
124 public static function getDomainBase(string $domain): string {
125 return self::$base.\DS. \trim($domain, '\\') . '\\';
126 }
127
134 public static function createDomain(string $domainName): bool {
135 $baseFolder=\ROOT.self::$base.\DS.$domainName.\DS;
136 $result=self::createFolder($baseFolder.'views');
137 if($result) {
138 $result = self::createFolder($baseFolder . (Startup::$config['mvcNS']['controllers']) ?? 'controllers');
139 if($result){
140 $result=self::createFolder($baseFolder . (Startup::$config['mvcNS']['models']) ?? 'models');
141 }
142 }
143 return $result;
144 }
145
146 private static function createFolder(string $folder): bool {
147 if(UFileSystem::safeMkdir($folder)){
148 return false!==\file_put_contents($folder.\DS.'.gitkeep','');
149 }
150 return false;
151 }
152
153 private static function updateClassesNamespace(string $oldBase,string $newBase): void {
154 $files=UFileSystem::glob_recursive(\ROOT.$newBase.\DS.'*.{php}',GLOB_BRACE);
155 foreach ($files as $file){
156 if(($content=\file_get_contents($file))!==false){
157 $content=\str_replace($oldBase.'\\',$newBase.'\\',$content);
158 \file_put_contents($file,$content);
159 }
160 }
161 }
162
163
169 public static function getBase(): string {
170 return self::$base;
171 }
172
179 public static function setBase(string $base): bool {
180 if (self::$base !== $base) {
181 $newBaseFolder=\realpath(\ROOT).\DS.$base;
182 $oldBaseFolder=realpath(\ROOT.self::$base);
183 if (\file_exists($oldBaseFolder) && !\file_exists(realpath($newBaseFolder))) {
184 if(\chmod($oldBaseFolder,'0777')) {
185 if (\rename($oldBaseFolder, $newBaseFolder)) {
186 self::updateClassesNamespace(self::$base, $base);
187 }else{
188 return false;
189 }
190 }else{
191 return false;
192 }
193 } else {
194 UFileSystem::safeMkdir(\ROOT . $base);
195 }
196 self::$base = $base;
197 $config = Startup::$config;
198 $config['mvcNS']['domains'] = $base;
199 Startup::updateConfig($config);
200 return true;
201 }
202 return false;
203 }
204
205
211 public static function getDatabases(): array {
212 $modelsDbs=CacheManager::getModelsDatabases();
213 $ns=self::getNamespace('models');
214 $result=[];
215 foreach ($modelsDbs as $model=>$db){
216 if(UString::startswith($model,$ns)){
217 $result[$db]=true;
218 }
219 }
220 return \array_keys($result);
221 }
222}
Manager for caches (Router, Rest, models).
Starts the framework.
Definition Startup.php:19
Manager for a Domain Driven Design approach.
static start()
Starts the domain manager.
static domainExists(string $domain)
Check if the domain exist.
static updateClassesNamespace(string $oldBase, string $newBase)
static getActiveViewFolder()
Returns the active view folder.
static getNamespace(string $type='controllers')
static createFolder(string $folder)
static getDomainBase(string $domain)
Returns the base folder for a domain.
static setDomain(string $domain)
Sets the active domain.
static getDatabases()
Returns the databases with models in the current domain.
static getViewNamespace()
Returns the active view namespace.
static hasDomains()
Check if there are any domains.
static setBase(string $base)
Changes the base directory for domains.
static getActiveDomain()
Returns the active domain name.
static getBase()
Returns the domains base directory.
static getDomains()
Returns an array of existing domains.
static createDomain(string $domainName)
Creates a new domain.
static resetActiveDomain()
Removes the active domain.
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.
String utilities.
Definition UString.php:15