|
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.1 |
|
18
|
|
|
* |
|
19
|
|
|
*/ |
|
20
|
|
|
class DDDManager { |
|
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
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Starts the domain manager. |
|
34
|
|
|
* To use only if the domain base is different from domains. |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function start(): void{ |
|
37
|
|
|
self::$base=Startup::$config['mvcNS']['domains']??'domains'; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Sets the active domain. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $domain |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function setDomain(string $domain): void { |
|
46
|
|
|
self::$activeDomain = $domain; |
|
47
|
|
|
Startup::setActiveDomainBase($domain, self::$base); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Removes the active domain. |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function resetActiveDomain(): void { |
|
54
|
|
|
self::$activeDomain=''; |
|
55
|
|
|
Startup::resetActiveDomainBase(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Returns an array of existing domains. |
|
60
|
|
|
* |
|
61
|
|
|
* @return array |
|
62
|
|
|
*/ |
|
63
|
48 |
|
public static function getDomains(): array { |
|
64
|
48 |
|
return \array_map('basename', \glob(\ROOT.self::$base . '/*' , \GLOB_ONLYDIR)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Check if there are any domains. |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
13 |
|
public static function hasDomains(): bool { |
|
72
|
13 |
|
return \file_exists(\ROOT.self::$base) && \count(self::getDomains())>0; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Check if the domain exist. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $domain |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
|
|
public static function domainExists(string $domain): bool { |
|
82
|
|
|
$domains=self::getDomains(); |
|
83
|
|
|
return \array_search($domain,$domains)!==false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns the active domain name. |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
16 |
|
public static function getActiveDomain(): string { |
|
91
|
16 |
|
return self::$activeDomain; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Returns the active view folder. |
|
96
|
|
|
* |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
71 |
|
public static function getActiveViewFolder(): string { |
|
100
|
71 |
|
if(self::$activeDomain != '' && \file_exists($folder = \ROOT . self::$base . \DS . self::$activeDomain . \DS . 'views' . \DS)) { |
|
101
|
|
|
return $folder; |
|
102
|
|
|
} |
|
103
|
71 |
|
return \ROOT.'views'.\DS; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Returns the active view namespace. |
|
108
|
|
|
* |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
4 |
|
public static function getViewNamespace(): string { |
|
112
|
4 |
|
if(($activeDomain=self::$activeDomain)!=''){ |
|
113
|
|
|
return '@'.$activeDomain.'/'; |
|
114
|
|
|
} |
|
115
|
4 |
|
return ''; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Returns the base folder for a domain. |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $domain |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
public static function getDomainBase(string $domain): string { |
|
125
|
|
|
return self::$base.\DS. \trim($domain, '\\') . '\\'; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Creates a new domain. |
|
130
|
|
|
* |
|
131
|
|
|
* @param string $domainName |
|
132
|
|
|
* @return bool |
|
133
|
|
|
*/ |
|
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
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Returns the domains base directory. |
|
166
|
|
|
* |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
|
|
public static function getBase(): string { |
|
170
|
|
|
return self::$base; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Changes the base directory for domains. |
|
175
|
|
|
* Do not use in production! |
|
176
|
|
|
* @param string $base |
|
177
|
|
|
* @return bool |
|
178
|
|
|
*/ |
|
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
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Returns the databases with models in the current domain. |
|
208
|
|
|
* |
|
209
|
|
|
* @return array |
|
210
|
|
|
*/ |
|
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
|
|
|
} |
|
223
|
|
|
|