3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Loader
;
14 if (class_exists('Zend\Loader\AutoloaderFactory')) {
18 abstract class AutoloaderFactory
20 const STANDARD_AUTOLOADER
= 'Zend\Loader\StandardAutoloader';
23 * @var array All autoloaders registered using the factory
25 protected static $loaders = array();
28 * @var StandardAutoloader StandardAutoloader instance for resolving
29 * autoloader classes via the include_path
31 protected static $standardAutoloader;
34 * Factory for autoloaders
36 * Options should be an array or Traversable object of the following structure:
39 * '<autoloader class name>' => $autoloaderOptions,
43 * The factory will then loop through and instantiate each autoloader with
44 * the specified options, and register each with the spl_autoloader.
46 * You may retrieve the concrete autoloader instances later using
47 * {@link getRegisteredAutoloaders()}.
49 * Note that the class names must be resolvable on the include_path or via
50 * the Zend library, using PSR-0 rules (unless the class has already been
53 * @param array|Traversable $options (optional) options to use. Defaults to Zend\Loader\StandardAutoloader
55 * @throws Exception\InvalidArgumentException for invalid options
56 * @throws Exception\InvalidArgumentException for unloadable autoloader classes
57 * @throws Exception\DomainException for autoloader classes not implementing SplAutoloader
59 public static function factory($options = null)
61 if (null === $options) {
62 if (!isset(static::$loaders[static::STANDARD_AUTOLOADER
])) {
63 $autoloader = static::getStandardAutoloader();
64 $autoloader->register();
65 static::$loaders[static::STANDARD_AUTOLOADER
] = $autoloader;
68 // Return so we don't hit the next check's exception (we're done here anyway)
72 if (!is_array($options) && !($options instanceof Traversable
)) {
73 require_once __DIR__
. '/Exception/InvalidArgumentException.php';
74 throw new Exception\
InvalidArgumentException(
75 'Options provided must be an array or Traversable'
79 foreach ($options as $class => $autoloaderOptions) {
80 if (!isset(static::$loaders[$class])) {
81 $autoloader = static::getStandardAutoloader();
82 if (!class_exists($class) && !$autoloader->autoload($class)) {
83 require_once 'Exception/InvalidArgumentException.php';
84 throw new Exception\
InvalidArgumentException(
85 sprintf('Autoloader class "%s" not loaded', $class)
89 if (!is_subclass_of($class, 'Zend\Loader\SplAutoloader')) {
90 require_once 'Exception/InvalidArgumentException.php';
91 throw new Exception\
InvalidArgumentException(
92 sprintf('Autoloader class %s must implement Zend\\Loader\\SplAutoloader', $class)
96 if ($class === static::STANDARD_AUTOLOADER
) {
97 $autoloader->setOptions($autoloaderOptions);
99 $autoloader = new $class($autoloaderOptions);
101 $autoloader->register();
102 static::$loaders[$class] = $autoloader;
104 static::$loaders[$class]->setOptions($autoloaderOptions);
110 * Get a list of all autoloaders registered with the factory
112 * Returns an array of autoloader instances.
116 public static function getRegisteredAutoloaders()
118 return static::$loaders;
122 * Retrieves an autoloader by class name
124 * @param string $class
125 * @return SplAutoloader
126 * @throws Exception\InvalidArgumentException for non-registered class
128 public static function getRegisteredAutoloader($class)
130 if (!isset(static::$loaders[$class])) {
131 require_once 'Exception/InvalidArgumentException.php';
132 throw new Exception\
InvalidArgumentException(sprintf('Autoloader class "%s" not loaded', $class));
134 return static::$loaders[$class];
138 * Unregisters all autoloaders that have been registered via the factory.
139 * This will NOT unregister autoloaders registered outside of the fctory.
143 public static function unregisterAutoloaders()
145 foreach (static::getRegisteredAutoloaders() as $class => $autoloader) {
146 spl_autoload_unregister(array($autoloader, 'autoload'));
147 unset(static::$loaders[$class]);
152 * Unregister a single autoloader by class name
154 * @param string $autoloaderClass
157 public static function unregisterAutoloader($autoloaderClass)
159 if (!isset(static::$loaders[$autoloaderClass])) {
163 $autoloader = static::$loaders[$autoloaderClass];
164 spl_autoload_unregister(array($autoloader, 'autoload'));
165 unset(static::$loaders[$autoloaderClass]);
170 * Get an instance of the standard autoloader
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" namespace.
176 * @return SplAutoloader
178 protected static function getStandardAutoloader()
180 if (null !== static::$standardAutoloader) {
181 return static::$standardAutoloader;
185 if (!class_exists(static::STANDARD_AUTOLOADER
)) {
186 // Extract the filename from the classname
187 $stdAutoloader = substr(strrchr(static::STANDARD_AUTOLOADER
, '\\'), 1);
188 require_once __DIR__
. "/$stdAutoloader.php";
190 $loader = new StandardAutoloader();
191 static::$standardAutoloader = $loader;
192 return static::$standardAutoloader;
196 * Checks if the object has this class as one of its parents
198 * @see https://bugs.php.net/bug.php?id=53727
199 * @see https://github.com/zendframework/zf2/pull/1807
201 * @deprecated since zf 2.3 requires PHP >= 5.3.23
203 * @param string $className
204 * @param string $type
207 protected static function isSubclassOf($className, $type)
209 return is_subclass_of($className, $type);