[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @see https://github.com/laminas/laminas-zendframework-bridge for the canonical source repository 5 * @copyright https://github.com/laminas/laminas-zendframework-bridge/blob/master/COPYRIGHT.md 6 * @license https://github.com/laminas/laminas-zendframework-bridge/blob/master/LICENSE.md New BSD License 7 */ 8 9 namespace Laminas\ZendFrameworkBridge; 10 11 use ArrayObject; 12 use Composer\Autoload\ClassLoader; 13 use RuntimeException; 14 15 use function array_values; 16 use function class_alias; 17 use function class_exists; 18 use function explode; 19 use function file_exists; 20 use function interface_exists; 21 use function spl_autoload_register; 22 use function strlen; 23 use function strtr; 24 use function substr; 25 use function trait_exists; 26 27 /** 28 * Alias legacy Zend Framework project classes/interfaces/traits to Laminas equivalents. 29 */ 30 class Autoloader 31 { 32 /** 33 * Attach autoloaders for managing legacy ZF artifacts. 34 * 35 * We attach two autoloaders: 36 * 37 * - The first is _prepended_ to handle new classes and add aliases for 38 * legacy classes. PHP expects any interfaces implemented, classes 39 * extended, or traits used when declaring class_alias() to exist and/or 40 * be autoloadable already at the time of declaration. If not, it will 41 * raise a fatal error. This autoloader helps mitigate errors in such 42 * situations. 43 * 44 * - The second is _appended_ in order to create aliases for legacy 45 * classes. 46 */ 47 public static function load() 48 { 49 $loaded = new ArrayObject([]); 50 51 spl_autoload_register(self::createPrependAutoloader( 52 RewriteRules::namespaceReverse(), 53 self::getClassLoader(), 54 $loaded 55 ), true, true); 56 57 spl_autoload_register(self::createAppendAutoloader( 58 RewriteRules::namespaceRewrite(), 59 $loaded 60 )); 61 } 62 63 /** 64 * @return ClassLoader 65 * @throws RuntimeException 66 */ 67 private static function getClassLoader() 68 { 69 if (getenv('COMPOSER_VENDOR_DIR') && file_exists(getenv('COMPOSER_VENDOR_DIR') . '/autoload.php')) { 70 return include getenv('COMPOSER_VENDOR_DIR') . '/autoload.php'; 71 } 72 73 if (file_exists(__DIR__ . '/../../../autoload.php')) { 74 return include __DIR__ . '/../../../autoload.php'; 75 } 76 77 if (file_exists(__DIR__ . '/../vendor/autoload.php')) { 78 return include __DIR__ . '/../vendor/autoload.php'; 79 } 80 81 throw new RuntimeException('Cannot detect composer autoload. Please run composer install'); 82 } 83 84 /** 85 * @return callable 86 */ 87 private static function createPrependAutoloader(array $namespaces, ClassLoader $classLoader, ArrayObject $loaded) 88 { 89 /** 90 * @param string $class Class name to autoload 91 * @return void 92 */ 93 return static function ($class) use ($namespaces, $classLoader, $loaded) { 94 if (isset($loaded[$class])) { 95 return; 96 } 97 98 $segments = explode('\\', $class); 99 100 $i = 0; 101 $check = ''; 102 103 while (isset($segments[$i + 1], $namespaces[$check . $segments[$i] . '\\'])) { 104 $check .= $segments[$i] . '\\'; 105 ++$i; 106 } 107 108 if ($check === '') { 109 return; 110 } 111 112 if ($classLoader->loadClass($class)) { 113 $legacy = $namespaces[$check] 114 . strtr(substr($class, strlen($check)), [ 115 'ApiTools' => 'Apigility', 116 'Mezzio' => 'Expressive', 117 'Laminas' => 'Zend', 118 ]); 119 class_alias($class, $legacy); 120 } 121 }; 122 } 123 124 /** 125 * @return callable 126 */ 127 private static function createAppendAutoloader(array $namespaces, ArrayObject $loaded) 128 { 129 /** 130 * @param string $class Class name to autoload 131 * @return void 132 */ 133 return static function ($class) use ($namespaces, $loaded) { 134 $segments = explode('\\', $class); 135 136 if ($segments[0] === 'ZendService' && isset($segments[1])) { 137 $segments[0] .= '\\' . $segments[1]; 138 unset($segments[1]); 139 $segments = array_values($segments); 140 } 141 142 $i = 0; 143 $check = ''; 144 145 // We are checking segments of the namespace to match quicker 146 while (isset($segments[$i + 1], $namespaces[$check . $segments[$i] . '\\'])) { 147 $check .= $segments[$i] . '\\'; 148 ++$i; 149 } 150 151 if ($check === '') { 152 return; 153 } 154 155 $alias = $namespaces[$check] 156 . strtr(substr($class, strlen($check)), [ 157 'Apigility' => 'ApiTools', 158 'Expressive' => 'Mezzio', 159 'Zend' => 'Laminas', 160 'AbstractZendServer' => 'AbstractZendServer', 161 'ZendServerDisk' => 'ZendServerDisk', 162 'ZendServerShm' => 'ZendServerShm', 163 'ZendMonitor' => 'ZendMonitor', 164 ]); 165 166 $loaded[$alias] = true; 167 if (class_exists($alias) || interface_exists($alias) || trait_exists($alias)) { 168 class_alias($alias, $class); 169 } 170 }; 171 } 172 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |