21 private static string $base=
'domains';
24 private static function getNamespace(
string $type=
'controllers'): string{
26 if(self::$activeDomain!=
'') {
27 $prefix = self::$base .
'\\' . self::$activeDomain .
'\\';
29 return $prefix.((Startup::$config[
'mvcNS'][$type]) ?? $type);
36 public static function start(): void{
37 self::$base=
Startup::$config[
'mvcNS'][
'domains']??
'domains';
45 public static function setDomain(
string $domain): void {
46 self::$activeDomain = $domain;
47 Startup::setActiveDomainBase($domain, self::$base);
54 self::$activeDomain=
'';
55 Startup::resetActiveDomainBase();
64 return \array_map(
'basename', \glob(\ROOT.self::$base .
'/*' , \GLOB_ONLYDIR));
72 return \file_exists(\ROOT.self::$base) && \count(self::getDomains())>0;
82 $domains=self::getDomains();
83 return \array_search($domain,$domains)!==
false;
91 return self::$activeDomain;
100 if(self::$activeDomain !=
'' && \file_exists($folder = \ROOT . self::$base . \DS . self::$activeDomain . \DS .
'views' . \DS)) {
103 return \ROOT.
'views'.\DS;
112 if(($activeDomain=self::$activeDomain)!=
''){
113 return '@'.$activeDomain.
'/';
125 return self::$base.\DS. \trim($domain,
'\\') .
'\\';
135 $baseFolder=\ROOT.self::$base.\DS.$domainName.\DS;
136 $result=self::createFolder($baseFolder.
'views');
138 $result = self::createFolder($baseFolder . (Startup::$config[
'mvcNS'][
'controllers']) ??
'controllers');
140 $result=self::createFolder($baseFolder . (Startup::$config[
'mvcNS'][
'models']) ??
'models');
148 return false!==\file_put_contents($folder.\DS.
'.gitkeep',
'');
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);
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);
194 UFileSystem::safeMkdir(\ROOT . $base);
197 $config = Startup::$config;
198 $config[
'mvcNS'][
'domains'] = $base;
199 Startup::updateConfig($config);
213 $ns=self::getNamespace(
'models');
215 foreach ($modelsDbs as $model=>$db){
216 if(UString::startswith($model,$ns)){
220 return \array_keys($result);
Manager for caches (Router, Rest, models).
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 string $activeDomain
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.