1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Ubiquity\domains; |
5
|
|
|
|
6
|
|
|
use Ubiquity\cache\CacheManager; |
7
|
|
|
use Ubiquity\controllers\Startup; |
8
|
|
|
use Ubiquity\utils\base\UFileSystem; |
9
|
|
|
use Ubiquity\utils\base\UString; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Manager for a Domain Driven Design approach. |
13
|
|
|
* Ubiquity\domains$DDDManager |
14
|
|
|
* This class is part of Ubiquity |
15
|
|
|
* |
16
|
|
|
* @author jcheron <[email protected]> |
17
|
|
|
* @version 0.0.0 |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
class DDDManager { |
21
|
|
|
private static $base='domains'; |
22
|
|
|
private static $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
|
|
|
|
32
|
|
|
public static function start(): void{ |
33
|
|
|
self::$base=Startup::$config['mvcNS']['domains']??'domains'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function setDomain(string $domain): void { |
37
|
|
|
self::$activeDomain = $domain; |
38
|
|
|
Startup::setActiveDomainBase($domain, self::$base); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public static function resetActiveDomain(): void { |
42
|
|
|
self::$activeDomain=''; |
43
|
|
|
Startup::resetActiveDomainBase(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function getDomains(): array { |
47
|
|
|
return \array_map('basename', \glob(\ROOT.self::$base . '/*' , \GLOB_ONLYDIR)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function hasDomains(): bool { |
51
|
|
|
return \file_exists(\ROOT.self::$base) && \count(self::getDomains())>0; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public static function getActiveDomain(): string { |
55
|
|
|
return self::$activeDomain; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function getActiveViewFolder(): string { |
59
|
|
|
if(self::$activeDomain!=''){ |
60
|
|
|
if(\file_exists($folder=\ROOT.self::$base.\DS.self::$activeDomain.\DS.'views'.\DS)){ |
61
|
|
|
return $folder; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
return \ROOT.'views'.\DS; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public static function getViewNamespace(): string { |
68
|
|
|
$activeDomain=self::$activeDomain; |
69
|
|
|
if($activeDomain!=''){ |
70
|
|
|
return '@'.$activeDomain.'/'; |
71
|
|
|
} |
72
|
|
|
return ''; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public static function getDomainBase(string $domain): string { |
76
|
|
|
return self::$base.\DS. \trim($domain, '\\') . '\\'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public static function createDomain(string $domainName): bool { |
80
|
|
|
$baseFolder=$folder=\ROOT.self::$base.\DS.$domainName.\DS; |
|
|
|
|
81
|
|
|
$result=self::createFolder($baseFolder.'views'); |
82
|
|
|
if($result) { |
83
|
|
|
$result = self::createFolder($baseFolder . (Startup::$config['mvcNS']['controllers']) ?? 'controllers'); |
84
|
|
|
if($result){ |
85
|
|
|
$result=self::createFolder($baseFolder . (Startup::$config['mvcNS']['models']) ?? 'models'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
return $result; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private static function createFolder(string $folder): bool { |
92
|
|
|
if(UFileSystem::safeMkdir($folder)){ |
93
|
|
|
return false!==\file_put_contents($folder.\DS.'.gitkeep',''); |
94
|
|
|
} |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private static function updateClassesNamespace(string $oldBase,string $newBase): void { |
99
|
|
|
$files=UFileSystem::glob_recursive(\ROOT.$newBase.\DS.'*.{php}',GLOB_BRACE); |
100
|
|
|
foreach ($files as $file){ |
101
|
|
|
if(($content=\file_get_contents($file))!==false){ |
102
|
|
|
$content=\str_replace($oldBase.'\\',$newBase.'\\',$content); |
103
|
|
|
\file_put_contents($file,$content); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public static function getBase(): string { |
113
|
|
|
return self::$base; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $base |
118
|
|
|
*/ |
119
|
|
|
public static function setBase(string $base): void { |
120
|
|
|
if (self::$base !== $base) { |
121
|
|
|
if (\file_exists(\ROOT . self::$base)) { |
122
|
|
|
if(\rename(\ROOT . self::$base, \ROOT . $base)){ |
123
|
|
|
self::updateClassesNamespace(self::$base,$base); |
124
|
|
|
} |
125
|
|
|
} else { |
126
|
|
|
UFileSystem::safeMkdir(\ROOT . $base); |
127
|
|
|
} |
128
|
|
|
self::$base = $base; |
129
|
|
|
$config = Startup::$config; |
130
|
|
|
$config['mvcNS']['domains'] = $base; |
131
|
|
|
Startup::updateConfig($config); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public static function getDatabases(): array { |
136
|
|
|
$modelsDbs=CacheManager::getModelsDatabases(); |
137
|
|
|
$ns=self::getNamespace('models'); |
138
|
|
|
$result=[]; |
139
|
|
|
foreach ($modelsDbs as $model=>$db){ |
140
|
|
|
if(UString::startswith($model,$ns)){ |
141
|
|
|
$result[$db]=true; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
return \array_keys($result); |
145
|
|
|
} |
146
|
|
|
} |