PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.22
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.22
1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 All 49 releases
fluent-cart / vendor / openspout / openspout / src / Autoloader / Psr4Autoloader.php

Psr4Autoloader.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.22, at vendor/openspout/openspout/src/Autoloader/Psr4Autoloader.php

128 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\OpenSpout\Autoloader;
4
5 /**
6 * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md#class-example
7 */
8 class Psr4Autoloader
9 {
10 /**
11 * An associative array where the key is a namespace prefix and the value
12 * is an array of base directories for classes in that namespace.
13 *
14 * @var array
15 */
16 protected $prefixes = [];
17 /**
18 * Register loader with SPL autoloader stack.
19 */
20 public function register()
21 {
22 \spl_autoload_register([$this, 'loadClass']);
23 }
24 /**
25 * Adds a base directory for a namespace prefix.
26 *
27 * @param string $prefix the namespace prefix
28 * @param string $baseDir a base directory for class files in the
29 * namespace
30 * @param bool $prepend if true, prepend the base directory to the stack
31 * instead of appending it; this causes it to be searched first rather
32 * than last
33 */
34 public function addNamespace($prefix, $baseDir, $prepend = \false)
35 {
36 // normalize namespace prefix
37 $prefix = \trim($prefix, '\\') . '\\';
38 // normalize the base directory with a trailing separator
39 $baseDir = \rtrim($baseDir, \DIRECTORY_SEPARATOR) . '/';
40 // initialize the namespace prefix array
41 if (\false === isset($this->prefixes[$prefix])) {
42 $this->prefixes[$prefix] = [];
43 }
44 // retain the base directory for the namespace prefix
45 if ($prepend) {
46 \array_unshift($this->prefixes[$prefix], $baseDir);
47 } else {
48 $this->prefixes[$prefix][] = $baseDir;
49 }
50 }
51 /**
52 * Loads the class file for a given class name.
53 *
54 * @param string $class the fully-qualified class name
55 *
56 * @return mixed the mapped file name on success, or boolean false on
57 * failure
58 */
59 public function loadClass($class)
60 {
61 // the current namespace prefix
62 $prefix = $class;
63 // work backwards through the namespace names of the fully-qualified
64 // class name to find a mapped file name
65 while (($pos = \strrpos($prefix, '\\')) !== \false) {
66 // retain the trailing namespace separator in the prefix
67 $prefix = \substr($class, 0, $pos + 1);
68 // the rest is the relative class name
69 $relativeClass = \substr($class, $pos + 1);
70 // try to load a mapped file for the prefix and relative class
71 $mappedFile = $this->loadMappedFile($prefix, $relativeClass);
72 if (\false !== $mappedFile) {
73 return $mappedFile;
74 }
75 // remove the trailing namespace separator for the next iteration
76 // of strrpos()
77 $prefix = \rtrim($prefix, '\\');
78 }
79 // never found a mapped file
80 return \false;
81 }
82 /**
83 * Load the mapped file for a namespace prefix and relative class.
84 *
85 * @param string $prefix the namespace prefix
86 * @param string $relativeClass the relative class name
87 *
88 * @return mixed boolean false if no mapped file can be loaded, or the
89 * name of the mapped file that was loaded
90 */
91 protected function loadMappedFile($prefix, $relativeClass)
92 {
93 // are there any base directories for this namespace prefix?
94 if (\false === isset($this->prefixes[$prefix])) {
95 return \false;
96 }
97 // look through base directories for this namespace prefix
98 foreach ($this->prefixes[$prefix] as $baseDir) {
99 // replace the namespace prefix with the base directory,
100 // replace namespace separators with directory separators
101 // in the relative class name, append with .php
102 $file = $baseDir . \str_replace('\\', '/', $relativeClass) . '.php';
103 // if the mapped file exists, require it
104 if ($this->requireFile($file)) {
105 // yes, we're done
106 return $file;
107 }
108 }
109 // never found it
110 return \false;
111 }
112 /**
113 * If a file exists, require it from the file system.
114 *
115 * @param string $file the file to require
116 *
117 * @return bool true if the file exists, false if not
118 */
119 protected function requireFile($file)
120 {
121 if (\file_exists($file)) {
122 require $file;
123 return \true;
124 }
125 return \false;
126 }
127 }
128