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