1 | <?php |
||
32 | class Resolver |
||
33 | 21 | { |
|
34 | /** |
||
35 | 21 | * Instance of composer, since this will be used to load the ps4 prefixes |
|
36 | 21 | * |
|
37 | * @var ClassLoader |
||
38 | */ |
||
39 | private $composer; |
||
40 | |||
41 | /** |
||
42 | * @var Psr4Namespace |
||
43 | */ |
||
44 | private $namespace; |
||
45 | |||
46 | 6 | /** |
|
47 | * Resolver constructor. |
||
48 | 6 | * |
|
49 | 6 | * @param Psr4Namespace|string $namespace |
|
50 | * @param ClassLoader $composer |
||
51 | 6 | */ |
|
52 | public function __construct($namespace, ClassLoader $composer) |
||
57 | 3 | ||
58 | /** |
||
59 | * Set the namespace to resolve |
||
60 | 6 | * |
|
61 | * @param Psr4Namespace|string $namespace $namespace |
||
62 | */ |
||
63 | public function setNamespace($namespace) |
||
70 | 21 | ||
71 | /** |
||
72 | 21 | * Get the current namespace |
|
73 | * |
||
74 | 15 | * @return Psr4Namespace |
|
75 | */ |
||
76 | 15 | public function getNamespace(): Psr4Namespace |
|
80 | |||
81 | 12 | /** |
|
82 | * Find all of the avaiable constructs under a specific namespace |
||
83 | * |
||
84 | * @param string $instanceOf optional, restrict the classes found to those |
||
85 | * that extend from this base |
||
86 | * @return array a list of FQCN's that match |
||
87 | */ |
||
88 | public function findConstructs(string $instanceOf = null): array |
||
103 | |||
104 | /** |
||
105 | * Resolve a psr4 based namespace to a list of absolute directory paths |
||
106 | * |
||
107 | * @return array list of directories this namespace is mapped to |
||
108 | * @throws Exception |
||
109 | */ |
||
110 | public function findDirectories(): array |
||
121 | 13 | ||
122 | /** |
||
123 | 13 | * Build a list of absolute paths, for the given namespace, based on the relative $prefix |
|
124 | * |
||
125 | * @param array $directories the list of directories (their position relates to $prefix) |
||
126 | 15 | * @param Psr4Namespace $namespace The base namespace |
|
127 | * @param Psr4Namespace $prefix The psr4 namespace related to the list of provided directories |
||
128 | * @return array directory paths for provided namespace |
||
129 | */ |
||
130 | private function buildDirectoryList(array $directories, Psr4Namespace $namespace, Psr4Namespace $prefix): array |
||
142 | |||
143 | 21 | /** |
|
144 | 21 | * Find the best psr4 namespace prefix, based on the supplied namespace, and |
|
145 | 6 | * list of provided prefix |
|
146 | * |
||
147 | * @param Psr4Namespace $namespace |
||
148 | 15 | * @param array $namespacePrefixes |
|
149 | * @return Psr4Namespace |
||
150 | */ |
||
151 | private function findNamespacePrefix(Psr4Namespace $namespace, array $namespacePrefixes) |
||
168 | |||
169 | 12 | /** |
|
170 | 12 | * Retrieve a directory iterator for the supplied path |
|
171 | 12 | * |
|
172 | 12 | * @param string $path The directory to iterate |
|
173 | * @return RegexIterator |
||
174 | 12 | */ |
|
175 | private function getDirectoryIterator(string $path): RegexIterator |
||
181 | |||
182 | /** |
||
183 | * Determine if the construct (class, interface or trait) exists |
||
184 | * |
||
185 | * @param string $constructName |
||
186 | 6 | * @return bool |
|
187 | */ |
||
188 | 6 | private function langaugeConstructExists(string $constructName): bool |
|
194 | |||
195 | /** |
||
196 | * Determine if the construct exists |
||
197 | * |
||
198 | * @param string $constructName |
||
199 | 6 | * @param bool $autoload trigger the autoloader to be fired, if the construct |
|
200 | * doesn't exist |
||
201 | * @return bool |
||
202 | 6 | */ |
|
203 | 6 | private function checkConstructExists(string $constructName, bool $autoload = true): bool |
|
210 | |||
211 | /** |
||
212 | * Process a list of directories, searching for langauge constructs (classes, |
||
213 | * interfaces, traits) that exist in them, based on the supplied base |
||
214 | 6 | * namespace |
|
215 | * |
||
216 | * @param array $directories list of absolute directory paths |
||
217 | 6 | * @param Psr4Namespace $namespace The namespace these directories are representing |
|
218 | 6 | * @return array |
|
219 | 6 | */ |
|
220 | private function findNamespacedConstuctsInDirectories(array $directories, Psr4Namespace $namespace): array |
||
231 | 6 | ||
232 | /** |
||
233 | 6 | * Recurisvely scan the supplied directory for langauge constructs that are |
|
234 | 6 | * $namespaced |
|
235 | 6 | * |
|
236 | * @param string $directory The directory to scan |
||
237 | * @param Psr4Namespace $namespace the namespace that represents this directory |
||
238 | 6 | * @return array |
|
239 | */ |
||
240 | 6 | private function findNamespacedConstuctsInDirectory(string $directory, Psr4Namespace $namespace): array |
|
253 | } |
||
254 |