PluginProbe
WindPress – Tailwind CSS integration for WordPress / 3.0.11
WindPress – Tailwind CSS integration for WordPress v3.0.11
3.2.89 3.2.88 3.2.87 3.2.86 3.2.85 3.2.84 3.2.83 3.2.82 3.2.81 trunk 3.0.0 3.0.1 3.0.10 3.0.11 3.0.12 3.0.13 3.0.14 3.0.15 3.0.16 3.0.17 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 All 143 releases
windpress / vendor / composer / ClassLoader.php

ClassLoader.php in WindPress – Tailwind CSS integration for WordPress 3.0.11, at vendor/composer/ClassLoader.php

499 lines 15.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of Composer.
5 *
6 * (c) Nils Adermann <naderman@naderman.de>
7 * Jordi Boggiano <j.boggiano@seld.be>
8 *
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
11 */
12 namespace WindPressDeps\Composer\Autoload;
13
14 /**
15 * ClassLoader implements a PSR-0, PSR-4 and classmap class loader.
16 *
17 * $loader = new \Composer\Autoload\ClassLoader();
18 *
19 * // register classes with namespaces
20 * $loader->add('Symfony\Component', __DIR__.'/component');
21 * $loader->add('Symfony', __DIR__.'/framework');
22 *
23 * // activate the autoloader
24 * $loader->register();
25 *
26 * // to enable searching the include path (eg. for PEAR packages)
27 * $loader->setUseIncludePath(true);
28 *
29 * In this example, if you try to use a class in the Symfony\Component
30 * namespace or one of its children (Symfony\Component\Console for instance),
31 * the autoloader will first look for the class under the component/
32 * directory, and it will then fallback to the framework/ directory if not
33 * found before giving up.
34 *
35 * This class is loosely based on the Symfony UniversalClassLoader.
36 *
37 * @author Fabien Potencier <fabien@symfony.com>
38 * @author Jordi Boggiano <j.boggiano@seld.be>
39 * @see https://www.php-fig.org/psr/psr-0/
40 * @see https://www.php-fig.org/psr/psr-4/
41 */
42 class ClassLoader
43 {
44 /** @var \Closure(string):void */
45 private static $includeFile;
46 /** @var string|null */
47 private $vendorDir;
48 // PSR-4
49 /**
50 * @var array<string, array<string, int>>
51 */
52 private $prefixLengthsPsr4 = array();
53 /**
54 * @var array<string, list<string>>
55 */
56 private $prefixDirsPsr4 = array();
57 /**
58 * @var list<string>
59 */
60 private $fallbackDirsPsr4 = array();
61 // PSR-0
62 /**
63 * List of PSR-0 prefixes
64 *
65 * Structured as array('F (first letter)' => array('Foo\Bar (full prefix)' => array('path', 'path2')))
66 *
67 * @var array<string, array<string, list<string>>>
68 */
69 private $prefixesPsr0 = array();
70 /**
71 * @var list<string>
72 */
73 private $fallbackDirsPsr0 = array();
74 /** @var bool */
75 private $useIncludePath = \false;
76 /**
77 * @var array<string, string>
78 */
79 private $classMap = array();
80 /** @var bool */
81 private $classMapAuthoritative = \false;
82 /**
83 * @var array<string, bool>
84 */
85 private $missingClasses = array();
86 /** @var string|null */
87 private $apcuPrefix;
88 /**
89 * @var array<string, self>
90 */
91 private static $registeredLoaders = array();
92 /**
93 * @param string|null $vendorDir
94 */
95 public function __construct($vendorDir = null)
96 {
97 $this->vendorDir = $vendorDir;
98 self::initializeIncludeClosure();
99 }
100 /**
101 * @return array<string, list<string>>
102 */
103 public function getPrefixes()
104 {
105 if (!empty($this->prefixesPsr0)) {
106 return \call_user_func_array('WindPressDeps\\array_merge', \array_values($this->prefixesPsr0));
107 }
108 return array();
109 }
110 /**
111 * @return array<string, list<string>>
112 */
113 public function getPrefixesPsr4()
114 {
115 return $this->prefixDirsPsr4;
116 }
117 /**
118 * @return list<string>
119 */
120 public function getFallbackDirs()
121 {
122 return $this->fallbackDirsPsr0;
123 }
124 /**
125 * @return list<string>
126 */
127 public function getFallbackDirsPsr4()
128 {
129 return $this->fallbackDirsPsr4;
130 }
131 /**
132 * @return array<string, string> Array of classname => path
133 */
134 public function getClassMap()
135 {
136 return $this->classMap;
137 }
138 /**
139 * @param array<string, string> $classMap Class to filename map
140 *
141 * @return void
142 */
143 public function addClassMap(array $classMap)
144 {
145 if ($this->classMap) {
146 $this->classMap = \array_merge($this->classMap, $classMap);
147 } else {
148 $this->classMap = $classMap;
149 }
150 }
151 /**
152 * Registers a set of PSR-0 directories for a given prefix, either
153 * appending or prepending to the ones previously set for this prefix.
154 *
155 * @param string $prefix The prefix
156 * @param list<string>|string $paths The PSR-0 root directories
157 * @param bool $prepend Whether to prepend the directories
158 *
159 * @return void
160 */
161 public function add($prefix, $paths, $prepend = \false)
162 {
163 $paths = (array) $paths;
164 if (!$prefix) {
165 if ($prepend) {
166 $this->fallbackDirsPsr0 = \array_merge($paths, $this->fallbackDirsPsr0);
167 } else {
168 $this->fallbackDirsPsr0 = \array_merge($this->fallbackDirsPsr0, $paths);
169 }
170 return;
171 }
172 $first = $prefix[0];
173 if (!isset($this->prefixesPsr0[$first][$prefix])) {
174 $this->prefixesPsr0[$first][$prefix] = $paths;
175 return;
176 }
177 if ($prepend) {
178 $this->prefixesPsr0[$first][$prefix] = \array_merge($paths, $this->prefixesPsr0[$first][$prefix]);
179 } else {
180 $this->prefixesPsr0[$first][$prefix] = \array_merge($this->prefixesPsr0[$first][$prefix], $paths);
181 }
182 }
183 /**
184 * Registers a set of PSR-4 directories for a given namespace, either
185 * appending or prepending to the ones previously set for this namespace.
186 *
187 * @param string $prefix The prefix/namespace, with trailing '\\'
188 * @param list<string>|string $paths The PSR-4 base directories
189 * @param bool $prepend Whether to prepend the directories
190 *
191 * @throws \InvalidArgumentException
192 *
193 * @return void
194 */
195 public function addPsr4($prefix, $paths, $prepend = \false)
196 {
197 $paths = (array) $paths;
198 if (!$prefix) {
199 // Register directories for the root namespace.
200 if ($prepend) {
201 $this->fallbackDirsPsr4 = \array_merge($paths, $this->fallbackDirsPsr4);
202 } else {
203 $this->fallbackDirsPsr4 = \array_merge($this->fallbackDirsPsr4, $paths);
204 }
205 } elseif (!isset($this->prefixDirsPsr4[$prefix])) {
206 // Register directories for a new namespace.
207 $length = \strlen($prefix);
208 if ('\\' !== $prefix[$length - 1]) {
209 throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
210 }
211 $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
212 $this->prefixDirsPsr4[$prefix] = $paths;
213 } elseif ($prepend) {
214 // Prepend directories for an already registered namespace.
215 $this->prefixDirsPsr4[$prefix] = \array_merge($paths, $this->prefixDirsPsr4[$prefix]);
216 } else {
217 // Append directories for an already registered namespace.
218 $this->prefixDirsPsr4[$prefix] = \array_merge($this->prefixDirsPsr4[$prefix], $paths);
219 }
220 }
221 /**
222 * Registers a set of PSR-0 directories for a given prefix,
223 * replacing any others previously set for this prefix.
224 *
225 * @param string $prefix The prefix
226 * @param list<string>|string $paths The PSR-0 base directories
227 *
228 * @return void
229 */
230 public function set($prefix, $paths)
231 {
232 if (!$prefix) {
233 $this->fallbackDirsPsr0 = (array) $paths;
234 } else {
235 $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths;
236 }
237 }
238 /**
239 * Registers a set of PSR-4 directories for a given namespace,
240 * replacing any others previously set for this namespace.
241 *
242 * @param string $prefix The prefix/namespace, with trailing '\\'
243 * @param list<string>|string $paths The PSR-4 base directories
244 *
245 * @throws \InvalidArgumentException
246 *
247 * @return void
248 */
249 public function setPsr4($prefix, $paths)
250 {
251 if (!$prefix) {
252 $this->fallbackDirsPsr4 = (array) $paths;
253 } else {
254 $length = \strlen($prefix);
255 if ('\\' !== $prefix[$length - 1]) {
256 throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
257 }
258 $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
259 $this->prefixDirsPsr4[$prefix] = (array) $paths;
260 }
261 }
262 /**
263 * Turns on searching the include path for class files.
264 *
265 * @param bool $useIncludePath
266 *
267 * @return void
268 */
269 public function setUseIncludePath($useIncludePath)
270 {
271 $this->useIncludePath = $useIncludePath;
272 }
273 /**
274 * Can be used to check if the autoloader uses the include path to check
275 * for classes.
276 *
277 * @return bool
278 */
279 public function getUseIncludePath()
280 {
281 return $this->useIncludePath;
282 }
283 /**
284 * Turns off searching the prefix and fallback directories for classes
285 * that have not been registered with the class map.
286 *
287 * @param bool $classMapAuthoritative
288 *
289 * @return void
290 */
291 public function setClassMapAuthoritative($classMapAuthoritative)
292 {
293 $this->classMapAuthoritative = $classMapAuthoritative;
294 }
295 /**
296 * Should class lookup fail if not found in the current class map?
297 *
298 * @return bool
299 */
300 public function isClassMapAuthoritative()
301 {
302 return $this->classMapAuthoritative;
303 }
304 /**
305 * APCu prefix to use to cache found/not-found classes, if the extension is enabled.
306 *
307 * @param string|null $apcuPrefix
308 *
309 * @return void
310 */
311 public function setApcuPrefix($apcuPrefix)
312 {
313 $this->apcuPrefix = \function_exists('apcu_fetch') && \filter_var(\ini_get('apc.enabled'), \FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null;
314 }
315 /**
316 * The APCu prefix in use, or null if APCu caching is not enabled.
317 *
318 * @return string|null
319 */
320 public function getApcuPrefix()
321 {
322 return $this->apcuPrefix;
323 }
324 /**
325 * Registers this instance as an autoloader.
326 *
327 * @param bool $prepend Whether to prepend the autoloader or not
328 *
329 * @return void
330 */
331 public function register($prepend = \false)
332 {
333 \spl_autoload_register(array($this, 'loadClass'), \true, $prepend);
334 if (null === $this->vendorDir) {
335 return;
336 }
337 if ($prepend) {
338 self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders;
339 } else {
340 unset(self::$registeredLoaders[$this->vendorDir]);
341 self::$registeredLoaders[$this->vendorDir] = $this;
342 }
343 }
344 /**
345 * Unregisters this instance as an autoloader.
346 *
347 * @return void
348 */
349 public function unregister()
350 {
351 \spl_autoload_unregister(array($this, 'loadClass'));
352 if (null !== $this->vendorDir) {
353 unset(self::$registeredLoaders[$this->vendorDir]);
354 }
355 }
356 /**
357 * Loads the given class or interface.
358 *
359 * @param string $class The name of the class
360 * @return true|null True if loaded, null otherwise
361 */
362 public function loadClass($class)
363 {
364 if ($file = $this->findFile($class)) {
365 $includeFile = self::$includeFile;
366 $includeFile($file);
367 return \true;
368 }
369 return null;
370 }
371 /**
372 * Finds the path to the file where the class is defined.
373 *
374 * @param string $class The name of the class
375 *
376 * @return string|false The path if found, false otherwise
377 */
378 public function findFile($class)
379 {
380 // class map lookup
381 if (isset($this->classMap[$class])) {
382 return $this->classMap[$class];
383 }
384 if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) {
385 return \false;
386 }
387 if (null !== $this->apcuPrefix) {
388 $file = \apcu_fetch($this->apcuPrefix . $class, $hit);
389 if ($hit) {
390 return $file;
391 }
392 }
393 $file = $this->findFileWithExtension($class, '.php');
394 // Search for Hack files if we are running on HHVM
395 if (\false === $file && \defined('WindPressDeps\\HHVM_VERSION')) {
396 $file = $this->findFileWithExtension($class, '.hh');
397 }
398 if (null !== $this->apcuPrefix) {
399 \apcu_add($this->apcuPrefix . $class, $file);
400 }
401 if (\false === $file) {
402 // Remember that this class does not exist.
403 $this->missingClasses[$class] = \true;
404 }
405 return $file;
406 }
407 /**
408 * Returns the currently registered loaders keyed by their corresponding vendor directories.
409 *
410 * @return array<string, self>
411 */
412 public static function getRegisteredLoaders()
413 {
414 return self::$registeredLoaders;
415 }
416 /**
417 * @param string $class
418 * @param string $ext
419 * @return string|false
420 */
421 private function findFileWithExtension($class, $ext)
422 {
423 // PSR-4 lookup
424 $logicalPathPsr4 = \strtr($class, '\\', \DIRECTORY_SEPARATOR) . $ext;
425 $first = $class[0];
426 if (isset($this->prefixLengthsPsr4[$first])) {
427 $subPath = $class;
428 while (\false !== ($lastPos = \strrpos($subPath, '\\'))) {
429 $subPath = \substr($subPath, 0, $lastPos);
430 $search = $subPath . '\\';
431 if (isset($this->prefixDirsPsr4[$search])) {
432 $pathEnd = \DIRECTORY_SEPARATOR . \substr($logicalPathPsr4, $lastPos + 1);
433 foreach ($this->prefixDirsPsr4[$search] as $dir) {
434 if (\file_exists($file = $dir . $pathEnd)) {
435 return $file;
436 }
437 }
438 }
439 }
440 }
441 // PSR-4 fallback dirs
442 foreach ($this->fallbackDirsPsr4 as $dir) {
443 if (\file_exists($file = $dir . \DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
444 return $file;
445 }
446 }
447 // PSR-0 lookup
448 if (\false !== ($pos = \strrpos($class, '\\'))) {
449 // namespaced class name
450 $logicalPathPsr0 = \substr($logicalPathPsr4, 0, $pos + 1) . \strtr(\substr($logicalPathPsr4, $pos + 1), '_', \DIRECTORY_SEPARATOR);
451 } else {
452 // PEAR-like class name
453 $logicalPathPsr0 = \strtr($class, '_', \DIRECTORY_SEPARATOR) . $ext;
454 }
455 if (isset($this->prefixesPsr0[$first])) {
456 foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
457 if (0 === \strpos($class, $prefix)) {
458 foreach ($dirs as $dir) {
459 if (\file_exists($file = $dir . \DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
460 return $file;
461 }
462 }
463 }
464 }
465 }
466 // PSR-0 fallback dirs
467 foreach ($this->fallbackDirsPsr0 as $dir) {
468 if (\file_exists($file = $dir . \DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
469 return $file;
470 }
471 }
472 // PSR-0 include paths.
473 if ($this->useIncludePath && ($file = \stream_resolve_include_path($logicalPathPsr0))) {
474 return $file;
475 }
476 return \false;
477 }
478 /**
479 * @return void
480 */
481 private static function initializeIncludeClosure()
482 {
483 if (self::$includeFile !== null) {
484 return;
485 }
486 /**
487 * Scope isolated include.
488 *
489 * Prevents access to $this/self from included files.
490 *
491 * @param string $file
492 * @return void
493 */
494 self::$includeFile = \Closure::bind(static function ($file) {
495 include $file;
496 }, null, null);
497 }
498 }
499