1 | <?php |
||
19 | class Resolver |
||
20 | { |
||
21 | /** |
||
22 | * Instance of composer, since this will be used to load the ps4 prefixes |
||
23 | * |
||
24 | * @var ClassLoader |
||
25 | */ |
||
26 | private $composer; |
||
27 | |||
28 | /** |
||
29 | * Resolver constructor. |
||
30 | * |
||
31 | * @param ClassLoader $composer |
||
32 | */ |
||
33 | 21 | public function __construct(ClassLoader $composer) |
|
37 | |||
38 | /** |
||
39 | * Find all of the avaiable classes under a specific namespace |
||
40 | * |
||
41 | * @param string $namespace The namespace to search for |
||
42 | * @param string $instanceOf optional, restrict the classes found to those |
||
43 | * that extend from this base |
||
44 | * @return array a list of FQCN's that match |
||
45 | */ |
||
46 | 6 | public function findClasses(string $namespace, string $instanceOf = null): array |
|
62 | |||
63 | /** |
||
64 | * Resolve a psr4 based namespace to a list of absolute directory paths |
||
65 | * |
||
66 | * @param string $namespace |
||
67 | * @return array list of directories this namespace is mapped to |
||
68 | * @throws Exception |
||
69 | */ |
||
70 | 21 | public function resolveDirectory(string $namespace): array |
|
83 | |||
84 | /** |
||
85 | * Build a list of absolute paths, for the given namespace, based on the relative $prefix |
||
86 | * |
||
87 | * @param array $directories the list of directories (their position relates to $prefix) |
||
88 | * @param string $namespace The base namespace |
||
89 | * @param string $prefix The psr4 namespace related to the list of provided directories |
||
90 | * @return array directory paths for provided namespace |
||
91 | */ |
||
92 | 12 | private function buildDirectoryList(array $directories, string $namespace, string $prefix): array |
|
104 | |||
105 | /** |
||
106 | * Find the best psr4 namespace prefix, based on the supplied namespace, and |
||
107 | * list of provided prefix |
||
108 | * |
||
109 | * @param string $namespace |
||
110 | * @param array $namespacePrefixes |
||
111 | * @return string |
||
112 | */ |
||
113 | 15 | private function findNamespacePrefix(string $namespace, array $namespacePrefixes): string |
|
128 | |||
129 | /** |
||
130 | * Convert the supplied namespace string into a standard format |
||
131 | * no prefix, ends with trailing slash |
||
132 | * |
||
133 | * Example: |
||
134 | * Psr4\Prefix\ |
||
135 | * Something\ |
||
136 | * |
||
137 | * @param string $namespace |
||
138 | * @return string |
||
139 | * @throws Exception |
||
140 | */ |
||
141 | 21 | private function normalise(string $namespace): string |
|
150 | |||
151 | /** |
||
152 | * Get an absolute path for the provided namespace, based on a existing |
||
153 | * directory and its psr4 prefix |
||
154 | * |
||
155 | * @param string $namespace |
||
156 | * @param string $psr4Prefix the psr4 prefix |
||
157 | * @param string $psr4Path and it's related path |
||
158 | * @return string the absolute directory path the provided namespace, given |
||
159 | * the correct prefix and path empty string if path can't |
||
160 | * be resolved |
||
161 | */ |
||
162 | 12 | private function findAbsolutePathForPsr4(string $namespace, string $psr4Prefix, string $psr4Path): string |
|
178 | |||
179 | |||
180 | /** |
||
181 | * Retrieve a directory iterator for the supplied path |
||
182 | * |
||
183 | * @param string $path The directory to iterate |
||
184 | * @return RegexIterator |
||
185 | */ |
||
186 | 6 | private function getDirectoryIterator(string $path): RegexIterator |
|
192 | |||
193 | /** |
||
194 | * Determine if the construct (class, interface or trait) exists |
||
195 | * |
||
196 | * @param string $artifactName |
||
197 | * @return bool |
||
198 | */ |
||
199 | 6 | private function langaugeConstructExists(string $artifactName): bool |
|
205 | |||
206 | /** |
||
207 | * Determine if the contract exists |
||
208 | * |
||
209 | * @param string $artifactName |
||
210 | * @param bool $autoload trigger the autoloader to be fired, if the construct |
||
211 | * doesn't exist |
||
212 | * @return bool |
||
213 | */ |
||
214 | 6 | private function checkConstructExists(string $artifactName, bool $autoload = true): bool |
|
221 | |||
222 | /** |
||
223 | * Process a list of directories, searching for langauge constructs (classes, |
||
224 | * interfaces, traits) that exist in them, based on the supplied base |
||
225 | * namespace |
||
226 | * |
||
227 | * @param array $directories list of absolute directory paths |
||
228 | * @param string $namespace The namespace these directories are representing |
||
229 | * @return array |
||
230 | */ |
||
231 | 6 | private function findNamespacedConstuctsInDirectories(array $directories, string $namespace): array |
|
242 | |||
243 | /** |
||
244 | * Recurisvely scan the supplied directory for langauge constructs that are |
||
245 | * $namespaced |
||
246 | * |
||
247 | * @param string $directory The directory to scan |
||
248 | * @param string $namespace the namespace that represents this directory |
||
249 | * @return array |
||
250 | */ |
||
251 | 6 | private function findNamespacedConstuctsInDirectory(string $directory, string $namespace): array |
|
264 | } |
||
265 |