Proxy
9 months ago
Util
2 years ago
ClassLoader.php
2 years ago
CommonException.php
2 years ago
Comparable.php
2 years ago
index.php
2 years ago
ClassLoader.php
107 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Doctrine\Common; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use function class_exists; |
| 5 | use function interface_exists; |
| 6 | use function is_array; |
| 7 | use function is_file; |
| 8 | use function reset; |
| 9 | use function spl_autoload_functions; |
| 10 | use function spl_autoload_register; |
| 11 | use function spl_autoload_unregister; |
| 12 | use function str_replace; |
| 13 | use function stream_resolve_include_path; |
| 14 | use function strpos; |
| 15 | use function trait_exists; |
| 16 | use function trigger_error; |
| 17 | use const DIRECTORY_SEPARATOR; |
| 18 | use const E_USER_DEPRECATED; |
| 19 | @trigger_error(ClassLoader::class . ' is deprecated.', E_USER_DEPRECATED); |
| 20 | class ClassLoader |
| 21 | { |
| 22 | protected $fileExtension = '.php'; |
| 23 | protected $namespace; |
| 24 | protected $includePath; |
| 25 | protected $namespaceSeparator = '\\'; |
| 26 | public function __construct($ns = null, $includePath = null) |
| 27 | { |
| 28 | $this->namespace = $ns; |
| 29 | $this->includePath = $includePath; |
| 30 | } |
| 31 | public function setNamespaceSeparator($sep) |
| 32 | { |
| 33 | $this->namespaceSeparator = $sep; |
| 34 | } |
| 35 | public function getNamespaceSeparator() |
| 36 | { |
| 37 | return $this->namespaceSeparator; |
| 38 | } |
| 39 | public function setIncludePath($includePath) |
| 40 | { |
| 41 | $this->includePath = $includePath; |
| 42 | } |
| 43 | public function getIncludePath() |
| 44 | { |
| 45 | return $this->includePath; |
| 46 | } |
| 47 | public function setFileExtension($fileExtension) |
| 48 | { |
| 49 | $this->fileExtension = $fileExtension; |
| 50 | } |
| 51 | public function getFileExtension() |
| 52 | { |
| 53 | return $this->fileExtension; |
| 54 | } |
| 55 | public function register() |
| 56 | { |
| 57 | spl_autoload_register([$this, 'loadClass']); |
| 58 | } |
| 59 | public function unregister() |
| 60 | { |
| 61 | spl_autoload_unregister([$this, 'loadClass']); |
| 62 | } |
| 63 | public function loadClass($className) |
| 64 | { |
| 65 | if (self::typeExists($className)) { |
| 66 | return \true; |
| 67 | } |
| 68 | if (!$this->canLoadClass($className)) { |
| 69 | return \false; |
| 70 | } |
| 71 | require ($this->includePath !== null ? $this->includePath . DIRECTORY_SEPARATOR : '') . str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $className) . $this->fileExtension; |
| 72 | return self::typeExists($className); |
| 73 | } |
| 74 | public function canLoadClass($className) |
| 75 | { |
| 76 | if ($this->namespace !== null && strpos($className, $this->namespace . $this->namespaceSeparator) !== 0) { |
| 77 | return \false; |
| 78 | } |
| 79 | $file = str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $className) . $this->fileExtension; |
| 80 | if ($this->includePath !== null) { |
| 81 | return is_file($this->includePath . DIRECTORY_SEPARATOR . $file); |
| 82 | } |
| 83 | return stream_resolve_include_path($file) !== \false; |
| 84 | } |
| 85 | public static function classExists($className) |
| 86 | { |
| 87 | return self::typeExists($className, \true); |
| 88 | } |
| 89 | public static function getClassLoader($className) |
| 90 | { |
| 91 | foreach (spl_autoload_functions() as $loader) { |
| 92 | if (!is_array($loader)) { |
| 93 | continue; |
| 94 | } |
| 95 | $classLoader = reset($loader); |
| 96 | if ($classLoader instanceof ClassLoader && $classLoader->canLoadClass($className)) { |
| 97 | return $classLoader; |
| 98 | } |
| 99 | } |
| 100 | return null; |
| 101 | } |
| 102 | private static function typeExists($type, $autoload = \false) |
| 103 | { |
| 104 | return class_exists($type, $autoload) || interface_exists($type, $autoload) || trait_exists($type, $autoload); |
| 105 | } |
| 106 | } |
| 107 |