PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Loader/ClassMapAutoloader.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 248 lines | 101 code | 23 blank | 124 comment | 16 complexity | 2acb3037511952ac7eb2bab70501dcd9 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Loader
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. // Grab SplAutoloader interface
  21. #require_once dirname(__FILE__) . '/SplAutoloader.php';
  22. /**
  23. * Class-map autoloader
  24. *
  25. * Utilizes class-map files to lookup classfile locations.
  26. *
  27. * @package Zend_Loader
  28. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license New BSD {@link http://framework.zend.com/license/new-bsd}
  30. */
  31. class Zend_Loader_ClassMapAutoloader implements Zend_Loader_SplAutoloader
  32. {
  33. /**
  34. * Registry of map files that have already been loaded
  35. * @var array
  36. */
  37. protected $mapsLoaded = array();
  38. /**
  39. * Class name/filename map
  40. * @var array
  41. */
  42. protected $map = array();
  43. /**
  44. * Constructor
  45. *
  46. * Create a new instance, and optionally configure the autoloader.
  47. *
  48. * @param null|array|Traversable $options
  49. * @return void
  50. */
  51. public function __construct($options = null)
  52. {
  53. if (null !== $options) {
  54. $this->setOptions($options);
  55. }
  56. }
  57. /**
  58. * Configure the autoloader
  59. *
  60. * Proxies to {@link registerAutoloadMaps()}.
  61. *
  62. * @param array|Traversable $options
  63. * @return Zend_Loader_ClassMapAutoloader
  64. */
  65. public function setOptions($options)
  66. {
  67. $this->registerAutoloadMaps($options);
  68. return $this;
  69. }
  70. /**
  71. * Register an autoload map
  72. *
  73. * An autoload map may be either an associative array, or a file returning
  74. * an associative array.
  75. *
  76. * An autoload map should be an associative array containing
  77. * classname/file pairs.
  78. *
  79. * @param string|array $location
  80. * @return Zend_Loader_ClassMapAutoloader
  81. */
  82. public function registerAutoloadMap($map)
  83. {
  84. if (is_string($map)) {
  85. $location = $map;
  86. if ($this === ($map = $this->loadMapFromFile($location))) {
  87. return $this;
  88. }
  89. }
  90. if (!is_array($map)) {
  91. #require_once dirname(__FILE__) . '/Exception/InvalidArgumentException.php';
  92. throw new Zend_Loader_Exception_InvalidArgumentException('Map file provided does not return a map');
  93. }
  94. $this->map = array_merge($this->map, $map);
  95. if (isset($location)) {
  96. $this->mapsLoaded[] = $location;
  97. }
  98. return $this;
  99. }
  100. /**
  101. * Register many autoload maps at once
  102. *
  103. * @param array $locations
  104. * @return Zend_Loader_ClassMapAutoloader
  105. */
  106. public function registerAutoloadMaps($locations)
  107. {
  108. if (!is_array($locations) && !($locations instanceof Traversable)) {
  109. #require_once dirname(__FILE__) . '/Exception/InvalidArgumentException.php';
  110. throw new Zend_Loader_Exception_InvalidArgumentException('Map list must be an array or implement Traversable');
  111. }
  112. foreach ($locations as $location) {
  113. $this->registerAutoloadMap($location);
  114. }
  115. return $this;
  116. }
  117. /**
  118. * Retrieve current autoload map
  119. *
  120. * @return array
  121. */
  122. public function getAutoloadMap()
  123. {
  124. return $this->map;
  125. }
  126. /**
  127. * Defined by Autoloadable
  128. *
  129. * @param string $class
  130. * @return void
  131. */
  132. public function autoload($class)
  133. {
  134. if (isset($this->map[$class])) {
  135. #require_once $this->map[$class];
  136. }
  137. }
  138. /**
  139. * Register the autoloader with spl_autoload registry
  140. *
  141. * @return void
  142. */
  143. public function register()
  144. {
  145. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  146. spl_autoload_register(array($this, 'autoload'), true, true);
  147. } else {
  148. spl_autoload_register(array($this, 'autoload'), true);
  149. }
  150. }
  151. /**
  152. * Load a map from a file
  153. *
  154. * If the map has been previously loaded, returns the current instance;
  155. * otherwise, returns whatever was returned by calling include() on the
  156. * location.
  157. *
  158. * @param string $location
  159. * @return Zend_Loader_ClassMapAutoloader|mixed
  160. * @throws Zend_Loader_Exception_InvalidArgumentException for nonexistent locations
  161. */
  162. protected function loadMapFromFile($location)
  163. {
  164. if (!file_exists($location)) {
  165. #require_once dirname(__FILE__) . '/Exception/InvalidArgumentException.php';
  166. throw new Zend_Loader_Exception_InvalidArgumentException('Map file provided does not exist');
  167. }
  168. if (!$path = self::realPharPath($location)) {
  169. $path = realpath($location);
  170. }
  171. if (in_array($path, $this->mapsLoaded)) {
  172. // Already loaded this map
  173. return $this;
  174. }
  175. $map = include $path;
  176. return $map;
  177. }
  178. /**
  179. * Resolve the real_path() to a file within a phar.
  180. *
  181. * @see https://bugs.php.net/bug.php?id=52769
  182. * @param string $path
  183. * @return string
  184. */
  185. public static function realPharPath($path)
  186. {
  187. if (strpos($path, 'phar:///') !== 0) {
  188. return;
  189. }
  190. $parts = explode('/', str_replace(array('/','\\'), '/', substr($path, 8)));
  191. $parts = array_values(array_filter($parts, array(__CLASS__, 'concatPharParts')));
  192. array_walk($parts, array(__CLASS__, 'resolvePharParentPath'), $parts);
  193. if (file_exists($realPath = 'phar:///' . implode('/', $parts))) {
  194. return $realPath;
  195. }
  196. }
  197. /**
  198. * Helper callback for filtering phar paths
  199. *
  200. * @param string $part
  201. * @return bool
  202. */
  203. public static function concatPharParts($part)
  204. {
  205. return ($part !== '' && $part !== '.');
  206. }
  207. /**
  208. * Helper callback to resolve a parent path in a Phar archive
  209. *
  210. * @param string $value
  211. * @param int $key
  212. * @param array $parts
  213. * @return void
  214. */
  215. public static function resolvePharParentPath($value, $key, &$parts)
  216. {
  217. if ($value !== '...') {
  218. return;
  219. }
  220. unset($parts[$key], $parts[$key-1]);
  221. $parts = array_values($parts);
  222. }
  223. }