1
|
|
|
<?php |
2
|
|
|
namespace keeko\tools\utils; |
3
|
|
|
|
4
|
|
|
use keeko\framework\schema\PackageSchema; |
5
|
|
|
use phootwork\file\Path; |
6
|
|
|
use phootwork\lang\Text; |
7
|
|
|
|
8
|
|
|
class NamespaceResolver { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Returns the source path for a given namespace or null if none can be found. |
12
|
|
|
* |
13
|
|
|
* @param string $namespace |
14
|
|
|
* @param PackageSchema $package |
15
|
|
|
* @return string|null |
16
|
|
|
*/ |
17
|
16 |
|
public static function getSourcePath($namespace, PackageSchema $package) { |
18
|
16 |
|
$relativeSourcePath = null; |
19
|
16 |
|
$autoload = $package->getAutoload(); |
20
|
|
|
|
21
|
16 |
|
$suffix = ''; |
22
|
16 |
|
$ns = new Path(str_replace('\\', '/', $namespace)); |
23
|
16 |
|
$ns->removeTrailingSeparator(); |
24
|
|
|
|
25
|
|
|
do { |
26
|
16 |
|
$namespace = $ns->getPathname()->replace('/', '\\')->toString(); |
27
|
|
|
|
28
|
|
|
// find paths in psr-s |
29
|
16 |
|
$relativeSourcePath = $autoload->getPsr4()->getPath($namespace . '\\'); |
30
|
|
|
|
31
|
16 |
|
if ($relativeSourcePath === null) { |
32
|
13 |
|
$relativeSourcePath = $autoload->getPsr0()->getPath($namespace); |
33
|
13 |
|
} |
34
|
|
|
|
35
|
|
|
// keep track of suffix |
36
|
16 |
|
if ($relativeSourcePath === null) { |
37
|
13 |
|
$suffix = $ns->lastSegment() . (!empty($suffix) ? '/' : '') . $suffix; |
38
|
13 |
|
$ns = $ns->upToSegment($ns->segmentCount() - 1); |
39
|
13 |
|
} |
40
|
16 |
|
} while (!$ns->isEmpty() xor $relativeSourcePath !== null); |
41
|
|
|
|
42
|
16 |
|
if ($relativeSourcePath === null) { |
43
|
16 |
|
return null; |
44
|
16 |
|
} |
45
|
16 |
|
|
46
|
|
|
$path = new Path($relativeSourcePath); |
47
|
16 |
|
$path->removeTrailingSeparator(); |
48
|
|
|
$path = $path->append($suffix); |
49
|
|
|
$path->addTrailingSeparator(); |
50
|
|
|
|
51
|
|
|
return $path->getPathname()->toString(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns the namespace for a given path or null if none can be found. |
57
|
|
|
* |
58
|
5 |
|
* @param string $path |
59
|
5 |
|
* @param PackageSchema $package |
60
|
5 |
|
* @return string|null |
61
|
|
|
*/ |
62
|
5 |
|
public static function getNamespace($path, PackageSchema $package) { |
63
|
5 |
|
$autoload = $package->getAutoload(); |
64
|
5 |
|
$suffix = ''; |
65
|
5 |
|
$namespace = null; |
66
|
|
|
$path = new Path($path); |
67
|
|
|
$path->removeTrailingSeparator(); |
68
|
5 |
|
|
69
|
|
|
do { |
70
|
|
|
$pathname = $path->getPathname()->toString(); |
71
|
5 |
|
|
72
|
5 |
|
// find namespace in psr-4 |
73
|
5 |
|
$namespace = $autoload->getPsr4()->getNamespace($pathname); |
74
|
5 |
|
if ($namespace === null) { |
75
|
|
|
$namespace = $autoload->getPsr4()->getNamespace($pathname . '/'); |
76
|
|
|
} |
77
|
5 |
|
|
78
|
5 |
|
// find namespace in psr-0 |
79
|
5 |
|
if ($namespace === null) { |
80
|
5 |
|
$namespace = $autoload->getPsr0()->getNamespace($pathname); |
81
|
5 |
|
} |
82
|
5 |
|
if ($namespace === null) { |
83
|
|
|
$namespace = $autoload->getPsr0()->getNamespace($pathname . '/'); |
84
|
|
|
} |
85
|
5 |
|
|
86
|
5 |
|
// keep track of suffix |
87
|
5 |
|
// shrink down path by one segment |
88
|
5 |
|
if ($namespace === null) { |
89
|
5 |
|
$suffix = $path->lastSegment() . (!empty($suffix) ? '\\' : '') . $suffix; |
90
|
|
|
$path = $path->upToSegment($path->segmentCount() - 1); |
91
|
5 |
|
} |
92
|
5 |
|
} while (!$path->isEmpty() xor $namespace !== null); |
93
|
2 |
|
|
94
|
2 |
|
if ($namespace === null) { |
95
|
|
|
return null; |
96
|
5 |
|
} |
97
|
|
|
|
98
|
|
|
$namespace = new Text($namespace . $suffix); |
99
|
|
|
if ($namespace->endsWith('\\')) { |
100
|
|
|
$namespace = $namespace->substring(0, -1); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $namespace->toString(); |
104
|
|
|
} |
105
|
|
|
} |