Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AutoloaderFactory.php
Go to the documentation of this file.
1 <?php
21 #require_once dirname(__FILE__) . '/SplAutoloader.php';
22 
23 if (class_exists('Zend_Loader_AutoloaderFactory')) return;
24 
31 {
32  const STANDARD_AUTOLOADER = 'Zend_Loader_StandardAutoloader';
33  const CLASS_MAP_AUTOLOADER = 'Zend_Loader_ClassMapAutoloader';
34 
38  protected static $loaders = array();
39 
44  protected static $standardAutoloader;
45 
71  public static function factory($options = null)
72  {
73  if (null === $options) {
74  if (!isset(self::$loaders[self::STANDARD_AUTOLOADER])) {
75  $autoloader = self::getStandardAutoloader();
76  $autoloader->register();
77  self::$loaders[self::STANDARD_AUTOLOADER] = $autoloader;
78  }
79 
80  // Return so we don't hit the next check's exception (we're done here anyway)
81  return;
82  }
83 
84  if (!is_array($options) && !($options instanceof Traversable)) {
85  #require_once 'Exception/InvalidArgumentException.php';
87  'Options provided must be an array or Traversable'
88  );
89  }
90 
91  foreach ($options as $class => $options) {
92  if (!isset(self::$loaders[$class])) {
93  // Check class map autoloader
94  if ($class == self::CLASS_MAP_AUTOLOADER) {
95  if (!class_exists(self::CLASS_MAP_AUTOLOADER)) {
96  // Extract the filename from the classname
97  $classMapLoader = substr(
98  strrchr(self::CLASS_MAP_AUTOLOADER, '_'), 1
99  );
100 
101  require_once dirname(__FILE__) . "/$classMapLoader.php";
102  }
103  }
104 
105  // Autoload with standard autoloader
106  $autoloader = self::getStandardAutoloader();
107  if (!class_exists($class) && !$autoloader->autoload($class)) {
108  #require_once 'Exception/InvalidArgumentException.php';
110  'Autoloader class "%s" not loaded',
111  $class
112  ));
113  }
114 
115  // unfortunately is_subclass_of is broken on some 5.3 versions
116  // additionally instanceof is also broken for this use case
117  if (version_compare(PHP_VERSION, '5.3.7', '>=')) {
118  if (!is_subclass_of($class, 'Zend_Loader_SplAutoloader')) {
119  #require_once 'Exception/InvalidArgumentException.php';
121  'Autoloader class %s must implement Zend\\Loader\\SplAutoloader',
122  $class
123  ));
124  }
125  }
126 
127  if ($class === self::STANDARD_AUTOLOADER) {
128  $autoloader->setOptions($options);
129  } else {
130  $autoloader = new $class($options);
131  }
132  $autoloader->register();
133  self::$loaders[$class] = $autoloader;
134  } else {
135  self::$loaders[$class]->setOptions($options);
136  }
137  }
138  }
139 
147  public static function getRegisteredAutoloaders()
148  {
149  return self::$loaders;
150  }
151 
159  public static function getRegisteredAutoloader($class)
160  {
161  if (!isset(self::$loaders[$class])) {
162  #require_once 'Exception/InvalidArgumentException.php';
163  throw new Zend_Loader_Exception_InvalidArgumentException(sprintf('Autoloader class "%s" not loaded', $class));
164  }
165  return self::$loaders[$class];
166  }
167 
174  public static function unregisterAutoloaders()
175  {
176  foreach (self::getRegisteredAutoloaders() as $class => $autoloader) {
177  spl_autoload_unregister(array($autoloader, 'autoload'));
178  unset(self::$loaders[$class]);
179  }
180  }
181 
188  public static function unregisterAutoloader($autoloaderClass)
189  {
190  if (!isset(self::$loaders[$autoloaderClass])) {
191  return false;
192  }
193 
194  $autoloader = self::$loaders[$autoloaderClass];
195  spl_autoload_unregister(array($autoloader, 'autoload'));
196  unset(self::$loaders[$autoloaderClass]);
197  return true;
198  }
199 
209  protected static function getStandardAutoloader()
210  {
211  if (null !== self::$standardAutoloader) {
213  }
214 
215  // Extract the filename from the classname
216  $stdAutoloader = substr(strrchr(self::STANDARD_AUTOLOADER, '_'), 1);
217 
218  if (!class_exists(self::STANDARD_AUTOLOADER)) {
219  #require_once dirname(__FILE__) . "/$stdAutoloader.php";
220  }
222  self::$standardAutoloader = $loader;
224  }
225 }
is_subclass_of($obj, $className)
defined('MTF_BOOT_FILE')||define('MTF_BOOT_FILE' __FILE__
Definition: bootstrap.php:7
$loader
Definition: autoload.php:8
static unregisterAutoloader($autoloaderClass)
$_option $_optionId $class
Definition: date.phtml:13
static factory($options=null)