Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ClassMapAutoloader.php
Go to the documentation of this file.
1 <?php
21 // Grab SplAutoloader interface
22 #require_once dirname(__FILE__) . '/SplAutoloader.php';
23 
34 {
39  protected $mapsLoaded = array();
40 
45  protected $map = array();
46 
55  public function __construct($options = null)
56  {
57  if (null !== $options) {
58  $this->setOptions($options);
59  }
60  }
61 
70  public function setOptions($options)
71  {
73  return $this;
74  }
75 
88  public function registerAutoloadMap($map)
89  {
90  if (is_string($map)) {
91  $location = $map;
92  if ($this === ($map = $this->loadMapFromFile($location))) {
93  return $this;
94  }
95  }
96 
97  if (!is_array($map)) {
98  #require_once dirname(__FILE__) . '/Exception/InvalidArgumentException.php';
99  throw new Zend_Loader_Exception_InvalidArgumentException('Map file provided does not return a map');
100  }
101 
102  $this->map = array_merge($this->map, $map);
103 
104  if (isset($location)) {
105  $this->mapsLoaded[] = $location;
106  }
107 
108  return $this;
109  }
110 
117  public function registerAutoloadMaps($locations)
118  {
119  if (!is_array($locations) && !($locations instanceof Traversable)) {
120  #require_once dirname(__FILE__) . '/Exception/InvalidArgumentException.php';
121  throw new Zend_Loader_Exception_InvalidArgumentException('Map list must be an array or implement Traversable');
122  }
123  foreach ($locations as $location) {
124  $this->registerAutoloadMap($location);
125  }
126  return $this;
127  }
128 
134  public function getAutoloadMap()
135  {
136  return $this->map;
137  }
138 
145  public function autoload($class)
146  {
147  if (isset($this->map[$class])) {
148  #require_once $this->map[$class];
149  }
150  }
151 
157  public function register()
158  {
159  if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
160  spl_autoload_register(array($this, 'autoload'), true, true);
161  } else {
162  spl_autoload_register(array($this, 'autoload'), true);
163  }
164  }
165 
177  protected function loadMapFromFile($location)
178  {
179  if (!file_exists($location)) {
180  #require_once dirname(__FILE__) . '/Exception/InvalidArgumentException.php';
181  throw new Zend_Loader_Exception_InvalidArgumentException('Map file provided does not exist');
182  }
183 
184  if (!$path = self::realPharPath($location)) {
185  $path = realpath($location);
186  }
187 
188  if (in_array($path, $this->mapsLoaded)) {
189  // Already loaded this map
190  return $this;
191  }
192 
193  $map = include $path;
194 
195  return $map;
196  }
197 
205  public static function realPharPath($path)
206  {
207  if (strpos($path, 'phar:///') !== 0) {
208  return;
209  }
210 
211  $parts = explode('/', str_replace(array('/','\\'), '/', substr($path, 8)));
212  $parts = array_values(array_filter($parts, array(__CLASS__, 'concatPharParts')));
213 
214  array_walk($parts, array(__CLASS__, 'resolvePharParentPath'), $parts);
215 
216  if (file_exists($realPath = 'phar:///' . implode('/', $parts))) {
217  return $realPath;
218  }
219  }
220 
227  public static function concatPharParts($part)
228  {
229  return ($part !== '' && $part !== '.');
230  }
231 
240  public static function resolvePharParentPath($value, $key, &$parts)
241  {
242  if ($value !== '...') {
243  return;
244  }
245  unset($parts[$key], $parts[$key-1]);
246  $parts = array_values($parts);
247  }
248 }
$_option $_optionId $class
Definition: date.phtml:13
$value
Definition: gender.phtml:16
static resolvePharParentPath($value, $key, &$parts)