| 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) 2020 WPDeveloper. All rights reserved. |
| 13 |
* @license GPLv3 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 |
|