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->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
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Reslove a namespace from a path relative to app root directory. |
53
|
|
|
* |
54
|
|
|
* @param string $path |
55
|
|
|
* @param string $additional |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public static function resolveFromRelativePath($path, $additional) |
60
|
|
|
{ |
61
|
|
|
$pathArray = array_filter(explode(DIRECTORY_SEPARATOR, $path)); |
62
|
|
|
|
63
|
|
|
array_unshift($pathArray, self::getRootNamespace()); |
64
|
|
|
|
65
|
|
|
$namespace = implode('\\', array_map('ucfirst', $pathArray)); |
66
|
|
|
|
67
|
|
|
return Str::appendWith($namespace, ucfirst($additional), '\\'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Resolve a namespace from an absolute path. |
72
|
|
|
* |
73
|
|
|
* @param string $path |
74
|
|
|
* @param string $additional |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public static function resolveFromAbsolutePath($path, $additional) |
79
|
|
|
{ |
80
|
|
|
$appPathArray = array_filter( |
81
|
|
|
explode('/', Config::getInstance()->getAppPath()) |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
$relativePath = array_diff( |
85
|
|
|
array_filter(explode(DIRECTORY_SEPARATOR, $path)), |
86
|
|
|
$appPathArray |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
array_unshift($relativePath, self::getRootNamespace()); |
90
|
|
|
|
91
|
|
|
$namespace = implode('\\', array_map('ucfirst', $relativePath)); |
92
|
|
|
|
93
|
|
|
return Str::appendWith($namespace, ucfirst($additional), '\\'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the app root namespace. |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public static function getRootNamespace() |
102
|
|
|
{ |
103
|
|
|
$config = Config::getInstance(); |
104
|
|
|
|
105
|
|
|
if ($config->has(['namespaces', 'root'])) { |
106
|
|
|
return $config->namespaces->root; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return 'App'; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.