PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more / 2.2.2
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more v2.2.2
4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.14 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.0.9 4.1.0 4.1.1 4.1.10 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.4.0 4.4.1 4.4.10 4.4.11 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 4.5.0 4.5.1
embedpress / EmbedPress / AutoLoader.php
embedpress / EmbedPress Last commit date
AMP 7 years ago Ends 7 years ago Plugins 7 years ago Providers 7 years ago ThirdParty 7 years ago AutoLoader.php 7 years ago Compatibility.php 7 years ago Core.php 7 years ago CoreLegacy.php 7 years ago DisablerLegacy.php 7 years ago Loader.php 7 years ago RestAPI.php 7 years ago Shortcode.php 7 years ago index.html 7 years ago
AutoLoader.php
242 lines
1 <?php
2
3 namespace EmbedPress;
4
5 (defined('ABSPATH') && defined('EMBEDPRESS_IS_LOADED')) or die("No direct script access allowed.");
6
7 /**
8 * Entity responsible for autoloading classes using the PSR-4 pattern.
9 *
10 * @package EmbedPress
11 * @author EmbedPress <help@embedpress.com>
12 * @copyright Copyright (C) 2018 EmbedPress. All rights reserved.
13 * @license GPLv2 or later
14 * @since 1.0.0
15 */
16 class AutoLoader
17 {
18 /**
19 * Associative array where the key is a namespace prefix and the value
20 * is an array of base directories for classes in that namespace.
21 *
22 * @since 1.0.0
23 * @access protected
24 * @static
25 *
26 * @var array
27 */
28 protected static $prefixes = [];
29
30 /**
31 * Associative array of prefixes for loading specialized camelCase classes
32 * where Uppercase letters in the class name indicate directory structure
33 *
34 * @since 1.0.0
35 * @access protected
36 * @static
37 *
38 * @var array
39 */
40 protected static $camelPrefixes = [];
41
42 /**
43 * @since 1.0.0
44 * @access protected
45 * @static
46 *
47 * @var AutoLoader
48 */
49 protected static $instance = null;
50
51 /**
52 * Register a new loader.
53 *
54 * @since 1.0.0
55 * @access protected
56 * @static
57 *
58 * @param string $method The method name which will be called.
59 *
60 * @return void
61 */
62 protected static function registerLoader($method)
63 {
64 if (static::$instance === null) {
65 static::$instance = new static();
66 }
67
68 spl_autoload_register([static::$instance, $method]);
69 }
70
71 /**
72 * Register a psr4 namespace
73 *
74 * @since 1.0.0
75 * @static
76 *
77 * @param string $prefix The namespace prefix.
78 * @param string $baseDir A base directory for class files in the namespace.
79 * @param bool $prepend If true, prepend the base directory to the stack instead of
80 * appending it; this causes it to be searched first rather than last.
81 *
82 * @return void
83 */
84 public static function register($prefix = null, $baseDir = null, $prepend = false)
85 {
86 if ($prefix === null || $baseDir === null) {
87 // Recognize old-style instantiations for backward compatibility
88 return;
89 }
90
91 if (count(self::$prefixes) == 0) {
92 // Register function on first call
93 static::registerLoader('loadClass');
94 }
95
96 // normalize namespace prefix
97 $prefix = trim($prefix, '\\') . '\\';
98
99 // normalize the base directory with a trailing separator
100 $baseDir = rtrim($baseDir, '\\/') . '/';
101
102 // initialise the namespace prefix array
103 if (empty(self::$prefixes[$prefix])) {
104 self::$prefixes[$prefix] = [];
105 }
106
107 // retain the base directory for the namespace prefix
108 if ($prepend) {
109 array_unshift(self::$prefixes[$prefix], $baseDir);
110 } else {
111 array_push(self::$prefixes[$prefix], $baseDir);
112 }
113 }
114
115 /**
116 * Loads the class file for a given class name.
117 *
118 * @since 1.0.0
119 * @access protected
120 * @static
121 *
122 * @param string $class The fully-qualified class name.
123 *
124 * @return mixed The mapped file name on success, or boolean false on failure.
125 */
126 protected function loadClass($class)
127 {
128 $prefixes = explode('\\', $class);
129 $className = '';
130 while ($prefixes) {
131 $className = array_pop($prefixes) . $className;
132 $prefix = join('\\', $prefixes) . '\\';
133
134 if ($filePath = $this->loadMappedFile($prefix, $className)) {
135 return $filePath;
136 }
137 $className = '\\' . $className;
138 }
139
140 // never found a mapped file
141 return false;
142 }
143
144 /**
145 * Load the mapped file for a namespace prefix and class.
146 *
147 * @since 1.0.0
148 * @access protected
149 * @static
150 *
151 * @param string $prefix The namespace prefix.
152 * @param string $className The relative class name.
153 *
154 * @return mixed False if no mapped file can be loaded | path that was loaded
155 */
156 protected function loadMappedFile($prefix, $className)
157 {
158 // are there any base directories for this namespace prefix?
159 if (isset(self::$prefixes[$prefix]) === false) {
160 return false;
161 }
162
163 // look through base directories for this namespace prefix
164 foreach (self::$prefixes[$prefix] as $baseDir) {
165 $path = $baseDir . str_replace('\\', '/', $className) . '.php';
166
167 if (is_file($path)) {
168 require_once $path;
169
170 return $path;
171 }
172 }
173
174 // never found it
175 return false;
176 }
177
178 /**
179 * Register a base directory for classes organized using camelCase.
180 * Class names beginning with the prefix will be automatically loaded
181 * if there is a matching file in the directory tree starting with $baseDir.
182 * File names and directory names are all expected to be lower case.
183 *
184 * @since 1.0.0
185 * @static
186 *
187 * @param string $prefix
188 * @param string $baseDir
189 *
190 * @return void
191 * @throws \Exception
192 */
193 public static function registerCamelBase($prefix, $baseDir)
194 {
195 if ( ! is_dir($baseDir)) {
196 throw new \Exception("Cannot register '{$prefix}'. The requested base directory does not exist!'");
197 }
198
199 if (count(self::$camelPrefixes) == 0) {
200 // Register function on first call
201 static::registerLoader('loadCamelClass');
202 }
203
204 if (empty(self::$camelPrefixes[$prefix])) {
205 self::$camelPrefixes[$prefix] = $baseDir;
206 }
207 }
208
209 /**
210 * Autoload a class using the camelCase structure
211 *
212 * @since 1.0.0
213 * @access protected
214 *
215 * @param string $class
216 *
217 * @return mixed Bool/string
218 */
219 protected function loadCamelClass($class)
220 {
221 if ( ! class_exists($class)) {
222 foreach (self::$camelPrefixes as $prefix => $baseDir) {
223 if (strpos($class, $prefix) === 0) {
224 $parts = preg_split('/(?<=[a-z])(?=[A-Z])/x', substr($class, strlen($prefix)));
225
226 $file = strtolower(join('/', $parts));
227 $filePath = $baseDir . '/' . $file . '.php';
228
229 if (is_file($filePath)) {
230 require_once $filePath;
231
232 return $filePath;
233 }
234 }
235 }
236 }
237
238 // No file found
239 return false;
240 }
241 }
242