Passed
Push — master ( e59108...b06663 )
by Jean-Christophe
17:44 queued 14s
created

DDDManager::getActiveViewFolder()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.576

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 4
c 2
b 0
f 2
dl 0
loc 7
ccs 3
cts 5
cp 0.6
rs 10
cc 3
nc 3
nop 0
crap 3.576
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
	/**
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 15
	public static function getActiveDomain(): string {
91 15
		return self::$activeDomain;
92
	}
93
	
94
	/**
95
	 * Returns the active view folder.
96
	 *
97
	 * @return string
98
	 */
99 4
	public static function getActiveViewFolder(): string {
100 4
		if(self::$activeDomain!=''){
101
			if(\file_exists($folder=\ROOT.self::$base.\DS.self::$activeDomain.\DS.'views'.\DS)){
102
				return $folder;
103
			}
104
		}
105 4
		return \ROOT.'views'.\DS;
106
	}
107
	
108
	/**
109
	 * Returns the active view namespace.
110
	 *
111
	 * @return string
112
	 */
113 4
	public static function getViewNamespace(): string {
114 4
		$activeDomain=self::$activeDomain;
115 4
		if($activeDomain!=''){
116
			return '@'.$activeDomain.'/';
117
		}
118 4
		return '';
119
	}
120
	
121
	/**
122
	 * Returns the base folder for a domain.
123
	 *
124
	 * @param string $domain
125
	 * @return string
126
	 */
127
	public static function getDomainBase(string $domain): string {
128
		return self::$base.\DS. \trim($domain, '\\') . '\\';
129
	}
130
	
131
	/**
132
	 * Creates a new domain.
133
	 *
134
	 * @param string $domainName
135
	 * @return bool
136
	 */
137
	public static function createDomain(string $domainName): bool {
138
		$baseFolder=\ROOT.self::$base.\DS.$domainName.\DS;
139
		$result=self::createFolder($baseFolder.'views');
140
		if($result) {
141
			$result = self::createFolder($baseFolder . (Startup::$config['mvcNS']['controllers']) ?? 'controllers');
142
			if($result){
143
				$result=self::createFolder($baseFolder . (Startup::$config['mvcNS']['models']) ?? 'models');
144
			}
145
		}
146
		return $result;
147
	}
148
	
149
	private static function createFolder(string $folder): bool {
150
		if(UFileSystem::safeMkdir($folder)){
151
			return false!==\file_put_contents($folder.\DS.'.gitkeep','');
152
		}
153
		return false;
154
	}
155
	
156
	private static function updateClassesNamespace(string $oldBase,string $newBase): void {
157
		$files=UFileSystem::glob_recursive(\ROOT.$newBase.\DS.'*.{php}',GLOB_BRACE);
158
		foreach ($files as $file){
159
			if(($content=\file_get_contents($file))!==false){
160
				$content=\str_replace($oldBase.'\\',$newBase.'\\',$content);
161
				\file_put_contents($file,$content);
162
			}
163
		}
164
	}
165
	
166
	
167
	/**
168
	 * Returns the domains base directory.
169
	 *
170
	 * @return string
171
	 */
172
	public static function getBase(): string {
173
		return self::$base;
174
	}
175
	
176
	/**
177
	 * Changes the base directory for domains.
178
	 * Do not use in production!
179
	 * @param string $base
180
	 * @return bool
181
	 */
182
	public static function setBase(string $base): bool {
183
		if (self::$base !== $base) {
184
			$newBaseFolder=\realpath(\ROOT).\DS.$base;
185
			$oldBaseFolder=realpath(\ROOT.self::$base);
186
			if (\file_exists($oldBaseFolder) && !\file_exists(realpath($newBaseFolder))) {
187
				if(\chmod($oldBaseFolder,'0777')) {
188
					if (\rename($oldBaseFolder, $newBaseFolder)) {
189
						self::updateClassesNamespace(self::$base, $base);
190
					}else{
191
						return false;
192
					}
193
				}else{
194
					return false;
195
				}
196
			} else {
197
				UFileSystem::safeMkdir(\ROOT . $base);
198
			}
199
			self::$base = $base;
200
			$config = Startup::$config;
201
			$config['mvcNS']['domains'] = $base;
202
			Startup::updateConfig($config);
203
			return true;
204
		}
205
		return false;
206
	}
207
	
208
	
209
	/**
210
	 * Returns the databases with models in the current domain.
211
	 *
212
	 * @return array
213
	 */
214
	public static function getDatabases(): array {
215
		$modelsDbs=CacheManager::getModelsDatabases();
216
		$ns=self::getNamespace('models');
217
		$result=[];
218
		foreach ($modelsDbs as $model=>$db){
219
			if(UString::startswith($model,$ns)){
220
				$result[$db]=true;
221
			}
222
		}
223
		return \array_keys($result);
224
	}
225
}
226