1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yarak\Helpers; |
4
|
|
|
|
5
|
|
|
use Yarak\Config\Config; |
6
|
|
|
|
7
|
|
|
class NamespaceResolver |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Resolve namespace for the given dir path, add additional values. |
11
|
|
|
* |
12
|
|
|
* @param string $dir |
13
|
|
|
* @param string $additional |
14
|
|
|
* |
15
|
|
|
* @return string|null |
16
|
|
|
*/ |
17
|
|
|
public static function resolve($dir, $additional = '') |
18
|
|
|
{ |
19
|
|
|
$config = Config::getInstance(); |
20
|
|
|
|
21
|
|
|
if ($config->has(['namespaces', $dir])) { |
22
|
|
|
$namespace = $config->get(['namespaces', $dir]); |
23
|
|
|
|
24
|
|
|
return Str::appendWith($namespace, ucfirst($additional), '\\'); |
25
|
|
|
} elseif ($config->has(['application', $dir.'Dir'])) { |
26
|
|
|
return self::resolveFromRegisteredDir($dir, $additional); |
27
|
|
|
} else { |
28
|
|
|
return self::resolveFromRelativePath($dir, $additional); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Resolve a namespace from a registered directory. |
34
|
|
|
* |
35
|
|
|
* @param string $dir |
36
|
|
|
* @param string $additional |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public static function resolveFromRegisteredDir($dir, $additional) |
41
|
|
|
{ |
42
|
|
|
$config = Config::getInstance(); |
43
|
|
|
|
44
|
|
|
$method = 'get'.ucfirst($dir).'Directory'; |
45
|
|
|
|
46
|
|
|
if (method_exists($config, $method)) { |
47
|
|
|
return self::resolveFromAbsolutePath($config->$method(), $additional); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Reslove a namespace from a path relative to app root directory. |
55
|
|
|
* |
56
|
|
|
* @param string $path |
57
|
|
|
* @param string $additional |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public static function resolveFromRelativePath($path, $additional) |
62
|
|
|
{ |
63
|
|
|
$pathArray = array_filter(explode(DIRECTORY_SEPARATOR, $path)); |
64
|
|
|
|
65
|
|
|
array_unshift($pathArray, self::getRootNamespace()); |
66
|
|
|
|
67
|
|
|
$namespace = implode('\\', array_map('ucfirst', $pathArray)); |
68
|
|
|
|
69
|
|
|
return Str::appendWith($namespace, ucfirst($additional), '\\'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Resolve a namespace from an absolute path. |
74
|
|
|
* |
75
|
|
|
* @param string $path |
76
|
|
|
* @param string $additional |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public static function resolveFromAbsolutePath($path, $additional) |
81
|
|
|
{ |
82
|
|
|
$appPathArray = array_filter( |
83
|
|
|
explode('/', Config::getInstance()->getAppPath()) |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$relativePath = array_diff( |
87
|
|
|
array_filter(explode(DIRECTORY_SEPARATOR, $path)), |
88
|
|
|
$appPathArray |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
array_unshift($relativePath, self::getRootNamespace()); |
92
|
|
|
|
93
|
|
|
$namespace = implode('\\', array_map('ucfirst', $relativePath)); |
94
|
|
|
|
95
|
|
|
return Str::appendWith($namespace, ucfirst($additional), '\\'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the app root namespace. |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public static function getRootNamespace() |
104
|
|
|
{ |
105
|
|
|
$config = Config::getInstance(); |
106
|
|
|
|
107
|
|
|
if ($config->has(['namespaces', 'root'])) { |
108
|
|
|
return $config->get(['namespaces', 'root']); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return 'App'; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|