PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more / 2.0.2
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more v2.0.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 8 years ago Ends 8 years ago Plugins 8 years ago Providers 8 years ago AutoLoader.php 8 years ago Core.php 8 years ago Disabler.php 8 years ago Loader.php 8 years ago Shortcode.php 8 years ago Updater.php 9 years ago index.html 9 years ago
AutoLoader.php
238 lines
1 <?php
2 namespace EmbedPress;
3
4 (defined('ABSPATH') && defined('EMBEDPRESS_IS_LOADED')) or die("No direct script access allowed.");
5
6 /**
7 * Entity responsible for autoloading classes using the PSR-4 pattern.
8 *
9 * @package EmbedPress
10 * @author PressShack <help@pressshack.com>
11 * @copyright Copyright (C) 2017 PressShack. All rights reserved.
12 * @license GPLv2 or later
13 * @since 1.0.0
14 */
15 class AutoLoader
16 {
17 /**
18 * Associative array where the key is a namespace prefix and the value
19 * is an array of base directories for classes in that namespace.
20 *
21 * @since 1.0.0
22 * @access protected
23 * @static
24 *
25 * @var array
26 */
27 protected static $prefixes = array();
28
29 /**
30 * Associative array of prefixes for loading specialized camelCase classes
31 * where Uppercase letters in the class name indicate directory structure
32 *
33 * @since 1.0.0
34 * @access protected
35 * @static
36 *
37 * @var array
38 */
39 protected static $camelPrefixes = array();
40
41 /**
42 * @since 1.0.0
43 * @access protected
44 * @static
45 *
46 * @var AutoLoader
47 */
48 protected static $instance = null;
49
50 /**
51 * Register a new loader.
52 *
53 * @since 1.0.0
54 * @access protected
55 * @static
56 *
57 * @param string $method The method name which will be called.
58 * @return void
59 */
60 protected static function registerLoader($method)
61 {
62 if (static::$instance === null) {
63 static::$instance = new static();
64 }
65
66 spl_autoload_register(array(static::$instance, $method));
67 }
68
69 /**
70 * Register a psr4 namespace
71 *
72 * @since 1.0.0
73 * @static
74 *
75 * @param string $prefix The namespace prefix.
76 * @param string $baseDir A base directory for class files in the namespace.
77 * @param bool $prepend If true, prepend the base directory to the stack instead of
78 * appending it; this causes it to be searched first rather than last.
79 *
80 * @return void
81 */
82 public static function register($prefix = null, $baseDir = null, $prepend = false)
83 {
84 if ($prefix === null || $baseDir === null) {
85 // Recognize old-style instantiations for backward compatibility
86 return;
87 }
88
89 if (count(self::$prefixes) == 0) {
90 // Register function on first call
91 static::registerLoader('loadClass');
92 }
93
94 // normalize namespace prefix
95 $prefix = trim($prefix, '\\') . '\\';
96
97 // normalize the base directory with a trailing separator
98 $baseDir = rtrim($baseDir, '\\/') . '/';
99
100 // initialise the namespace prefix array
101 if (empty(self::$prefixes[$prefix])) {
102 self::$prefixes[$prefix] = array();
103 }
104
105 // retain the base directory for the namespace prefix
106 if ($prepend) {
107 array_unshift(self::$prefixes[$prefix], $baseDir);
108 } else {
109 array_push(self::$prefixes[$prefix], $baseDir);
110 }
111 }
112
113 /**
114 * Loads the class file for a given class name.
115 *
116 * @since 1.0.0
117 * @access protected
118 * @static
119 *
120 * @param string $class The fully-qualified class name.
121 *
122 * @return mixed The mapped file name on success, or boolean false on failure.
123 */
124 protected function loadClass($class)
125 {
126 $prefixes = explode('\\', $class);
127 $className = '';
128 while ($prefixes) {
129 $className = array_pop($prefixes) . $className;
130 $prefix = join('\\', $prefixes) . '\\';
131
132 if ($filePath = $this->loadMappedFile($prefix, $className)) {
133 return $filePath;
134 }
135 $className = '\\' . $className;
136 }
137
138 // never found a mapped file
139 return false;
140 }
141
142 /**
143 * Load the mapped file for a namespace prefix and class.
144 *
145 * @since 1.0.0
146 * @access protected
147 * @static
148 *
149 * @param string $prefix The namespace prefix.
150 * @param string $className The relative class name.
151 *
152 * @return mixed False if no mapped file can be loaded | path that was loaded
153 */
154 protected function loadMappedFile($prefix, $className)
155 {
156 // are there any base directories for this namespace prefix?
157 if (isset(self::$prefixes[$prefix]) === false) {
158 return false;
159 }
160
161 // look through base directories for this namespace prefix
162 foreach (self::$prefixes[$prefix] as $baseDir) {
163 $path = $baseDir . str_replace('\\', '/', $className) . '.php';
164
165 if (is_file($path)) {
166 require_once $path;
167 return $path;
168 }
169 }
170
171 // never found it
172 return false;
173 }
174
175 /**
176 * Register a base directory for classes organized using camelCase.
177 * Class names beginning with the prefix will be automatically loaded
178 * if there is a matching file in the directory tree starting with $baseDir.
179 * File names and directory names are all expected to be lower case.
180 *
181 * @since 1.0.0
182 * @static
183 *
184 * @param string $prefix
185 * @param string $baseDir
186 *
187 * @return void
188 * @throws \Exception
189 */
190 public static function registerCamelBase($prefix, $baseDir)
191 {
192 if (!is_dir($baseDir)) {
193 throw new \Exception("Cannot register '{$prefix}'. The requested base directory does not exist!'");
194 }
195
196 if (count(self::$camelPrefixes) == 0) {
197 // Register function on first call
198 static::registerLoader('loadCamelClass');
199 }
200
201 if (empty(self::$camelPrefixes[$prefix])) {
202 self::$camelPrefixes[$prefix] = $baseDir;
203 }
204 }
205
206 /**
207 * Autoload a class using the camelCase structure
208 *
209 * @since 1.0.0
210 * @access protected
211 *
212 * @param string $class
213 *
214 * @return mixed Bool/string
215 */
216 protected function loadCamelClass($class)
217 {
218 if (!class_exists($class)) {
219 foreach (self::$camelPrefixes as $prefix => $baseDir) {
220 if (strpos($class, $prefix) === 0) {
221 $parts = preg_split('/(?<=[a-z])(?=[A-Z])/x', substr($class, strlen($prefix)));
222
223 $file = strtolower(join('/', $parts));
224 $filePath = $baseDir . '/' . $file . '.php';
225
226 if (is_file($filePath)) {
227 require_once $filePath;
228 return $filePath;
229 }
230 }
231 }
232 }
233
234 // No file found
235 return false;
236 }
237 }
238