1 | <?php |
||
15 | class Resolver |
||
16 | { |
||
17 | /** |
||
18 | * Instance of composer, since this will be used to load the ps4 prefixes |
||
19 | * |
||
20 | * @var ClassLoader |
||
21 | */ |
||
22 | private $composer; |
||
23 | |||
24 | /** |
||
25 | * Resolver constructor. |
||
26 | * |
||
27 | * @param ClassLoader $composer |
||
28 | */ |
||
29 | public function __construct(ClassLoader $composer) |
||
33 | |||
34 | /** |
||
35 | * Find all of the avaiable classes under a specific namespace |
||
36 | * |
||
37 | * @param string $namespace The namespace to search for |
||
38 | * @param string $instanceOf optional, restrict the classes found to those |
||
39 | * that extend from this base |
||
40 | * @return array a list of FQCN's that match |
||
41 | */ |
||
42 | public function findClasses(string $namespace, string $instanceOf = null): array |
||
43 | { |
||
44 | $availablePaths = $this->resolveDirectory($namespace); |
||
45 | |||
46 | $classes = []; |
||
47 | foreach ($availablePaths as $path) { |
||
48 | foreach ($this->getDirectoryIterator($path) as $file) { |
||
49 | $fqcn = $namespace.strtr(substr($file[0], strlen($path), -4), '//', '\\'); |
||
50 | if ($this->langaugeConstructExists($fqcn)) { |
||
51 | $classes[] = $fqcn; |
||
52 | } |
||
53 | } |
||
54 | } |
||
55 | |||
56 | sort($classes); |
||
57 | |||
58 | if ($instanceOf) { |
||
|
|||
59 | $classes = array_values(array_filter($classes, function ($className) use ($instanceOf) { |
||
60 | return is_subclass_of($className, $instanceOf); |
||
61 | })); |
||
62 | } |
||
63 | |||
64 | return $classes; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Resolve a psr4 based namespace to an absolute directory |
||
69 | * |
||
70 | * @param string $namespace |
||
71 | * @return array |
||
72 | * @throws Exception |
||
73 | */ |
||
74 | public function resolveDirectory(string $namespace): array |
||
94 | |||
95 | /** |
||
96 | * Find the best psr4 namespace prefix, based on the supplied namespace, and |
||
97 | * list of provided prefix |
||
98 | * |
||
99 | * @param string $namespace |
||
100 | * @param array $prefixes |
||
101 | * @return string |
||
102 | */ |
||
103 | private function findPrefix(string $namespace, array $prefixes): string |
||
118 | |||
119 | /** |
||
120 | * Convert the supplied namespace string into a standard format |
||
121 | * no prefix, ends with trailing slash |
||
122 | * |
||
123 | * Example: |
||
124 | * Psr4\Prefix\ |
||
125 | * Something\ |
||
126 | * |
||
127 | * @param string $namespace |
||
128 | * @return string |
||
129 | * @throws Exception |
||
130 | */ |
||
131 | private function normalise(string $namespace): string |
||
140 | |||
141 | /** |
||
142 | * Get an absolute path for the provided namespace, based on a existing |
||
143 | * directory and its psr4 prefix |
||
144 | * |
||
145 | * @param string $namespace |
||
146 | * @param string $psr4Prefix the psr4 prefix |
||
147 | * @param string $psr4Path and it's related path |
||
148 | * @return string the absolute directory path the provided namespace, given |
||
149 | * the correct prefix and path empty string if path can't |
||
150 | * be resolved |
||
151 | */ |
||
152 | private function findAbsolutePathForPsr4(string $namespace, string $psr4Prefix, string $psr4Path): string |
||
166 | |||
167 | |||
168 | /** |
||
169 | * Retrieve a directory iterator for the supplied path |
||
170 | * |
||
171 | * @param string $path The directory to iterate |
||
172 | * @return \RegexIterator |
||
173 | */ |
||
174 | private function getDirectoryIterator(string $path): \RegexIterator |
||
180 | |||
181 | /** |
||
182 | * Determine if the construct (class, interface or trait) exists |
||
183 | * |
||
184 | * @param string $artifactName |
||
185 | * @return bool |
||
186 | */ |
||
187 | private function langaugeConstructExists(string $artifactName): bool |
||
197 | } |
||
198 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: