AutoloaderFactory.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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-2014 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. require_once dirname(__FILE__) . '/SplAutoloader.php';
  21. if (class_exists('Zend_Loader_AutoloaderFactory')) return;
  22. /**
  23. * @package Zend_Loader
  24. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license http://framework.zend.com/license/new-bsd New BSD License
  26. */
  27. abstract class Zend_Loader_AutoloaderFactory
  28. {
  29. const STANDARD_AUTOLOADER = 'Zend_Loader_StandardAutoloader';
  30. /**
  31. * @var array All autoloaders registered using the factory
  32. */
  33. protected static $loaders = array();
  34. /**
  35. * @var Zend_Loader_StandardAutoloader StandardAutoloader instance for resolving
  36. * autoloader classes via the include_path
  37. */
  38. protected static $standardAutoloader;
  39. /**
  40. * Factory for autoloaders
  41. *
  42. * Options should be an array or Traversable object of the following structure:
  43. * <code>
  44. * array(
  45. * '<autoloader class name>' => $autoloaderOptions,
  46. * )
  47. * </code>
  48. *
  49. * The factory will then loop through and instantiate each autoloader with
  50. * the specified options, and register each with the spl_autoloader.
  51. *
  52. * You may retrieve the concrete autoloader instances later using
  53. * {@link getRegisteredAutoloaders()}.
  54. *
  55. * Note that the class names must be resolvable on the include_path or via
  56. * the Zend library, using PSR-0 rules (unless the class has already been
  57. * loaded).
  58. *
  59. * @param array|Traversable $options (optional) options to use. Defaults to Zend_Loader_StandardAutoloader
  60. * @return void
  61. * @throws Zend_Loader_Exception_InvalidArgumentException for invalid options
  62. * @throws Zend_Loader_Exception_InvalidArgumentException for unloadable autoloader classes
  63. */
  64. public static function factory($options = null)
  65. {
  66. if (null === $options) {
  67. if (!isset(self::$loaders[self::STANDARD_AUTOLOADER])) {
  68. $autoloader = self::getStandardAutoloader();
  69. $autoloader->register();
  70. self::$loaders[self::STANDARD_AUTOLOADER] = $autoloader;
  71. }
  72. // Return so we don't hit the next check's exception (we're done here anyway)
  73. return;
  74. }
  75. if (!is_array($options) && !($options instanceof Traversable)) {
  76. require_once 'Exception/InvalidArgumentException.php';
  77. throw new Zend_Loader_Exception_InvalidArgumentException(
  78. 'Options provided must be an array or Traversable'
  79. );
  80. }
  81. foreach ($options as $class => $options) {
  82. if (!isset(self::$loaders[$class])) {
  83. $autoloader = self::getStandardAutoloader();
  84. if (!class_exists($class) && !$autoloader->autoload($class)) {
  85. require_once 'Exception/InvalidArgumentException.php';
  86. throw new Zend_Loader_Exception_InvalidArgumentException(sprintf(
  87. 'Autoloader class "%s" not loaded',
  88. $class
  89. ));
  90. }
  91. // unfortunately is_subclass_of is broken on some 5.3 versions
  92. // additionally instanceof is also broken for this use case
  93. if (version_compare(PHP_VERSION, '5.3.7', '>=')) {
  94. if (!is_subclass_of($class, 'Zend_Loader_SplAutoloader')) {
  95. require_once 'Exception/InvalidArgumentException.php';
  96. throw new Zend_Loader_Exception_InvalidArgumentException(sprintf(
  97. 'Autoloader class %s must implement Zend\\Loader\\SplAutoloader',
  98. $class
  99. ));
  100. }
  101. }
  102. if ($class === self::STANDARD_AUTOLOADER) {
  103. $autoloader->setOptions($options);
  104. } else {
  105. $autoloader = new $class($options);
  106. }
  107. $autoloader->register();
  108. self::$loaders[$class] = $autoloader;
  109. } else {
  110. self::$loaders[$class]->setOptions($options);
  111. }
  112. }
  113. }
  114. /**
  115. * Get an list of all autoloaders registered with the factory
  116. *
  117. * Returns an array of autoloader instances.
  118. *
  119. * @return array
  120. */
  121. public static function getRegisteredAutoloaders()
  122. {
  123. return self::$loaders;
  124. }
  125. /**
  126. * Retrieves an autoloader by class name
  127. *
  128. * @param string $class
  129. * @return Zend_Loader_SplAutoloader
  130. * @throws Zend_Loader_Exception_InvalidArgumentException for non-registered class
  131. */
  132. public static function getRegisteredAutoloader($class)
  133. {
  134. if (!isset(self::$loaders[$class])) {
  135. require_once 'Exception/InvalidArgumentException.php';
  136. throw new Zend_Loader_Exception_InvalidArgumentException(sprintf('Autoloader class "%s" not loaded', $class));
  137. }
  138. return self::$loaders[$class];
  139. }
  140. /**
  141. * Unregisters all autoloaders that have been registered via the factory.
  142. * This will NOT unregister autoloaders registered outside of the fctory.
  143. *
  144. * @return void
  145. */
  146. public static function unregisterAutoloaders()
  147. {
  148. foreach (self::getRegisteredAutoloaders() as $class => $autoloader) {
  149. spl_autoload_unregister(array($autoloader, 'autoload'));
  150. unset(self::$loaders[$class]);
  151. }
  152. }
  153. /**
  154. * Unregister a single autoloader by class name
  155. *
  156. * @param string $autoloaderClass
  157. * @return bool
  158. */
  159. public static function unregisterAutoloader($autoloaderClass)
  160. {
  161. if (!isset(self::$loaders[$autoloaderClass])) {
  162. return false;
  163. }
  164. $autoloader = self::$loaders[$autoloaderClass];
  165. spl_autoload_unregister(array($autoloader, 'autoload'));
  166. unset(self::$loaders[$autoloaderClass]);
  167. return true;
  168. }
  169. /**
  170. * Get an instance of the standard autoloader
  171. *
  172. * Used to attempt to resolve autoloader classes, using the
  173. * StandardAutoloader. The instance is marked as a fallback autoloader, to
  174. * allow resolving autoloaders not under the "Zend" or "Zend" namespaces.
  175. *
  176. * @return Zend_Loader_SplAutoloader
  177. */
  178. protected static function getStandardAutoloader()
  179. {
  180. if (null !== self::$standardAutoloader) {
  181. return self::$standardAutoloader;
  182. }
  183. // Extract the filename from the classname
  184. $stdAutoloader = substr(strrchr(self::STANDARD_AUTOLOADER, '_'), 1);
  185. if (!class_exists(self::STANDARD_AUTOLOADER)) {
  186. require_once dirname(__FILE__) . "/$stdAutoloader.php";
  187. }
  188. $loader = new Zend_Loader_StandardAutoloader();
  189. self::$standardAutoloader = $loader;
  190. return self::$standardAutoloader;
  191. }
  192. }