Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Loader / AutoloaderFactory.php
blob27ab200f3cfb85808bcbeb8ae7e715400e8ead7f
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Loader;
12 use ReflectionClass;
13 use Traversable;
15 if (class_exists('Zend\Loader\AutoloaderFactory')) {
16 return;
19 abstract class AutoloaderFactory
21 const STANDARD_AUTOLOADER = 'Zend\Loader\StandardAutoloader';
23 /**
24 * @var array All autoloaders registered using the factory
26 protected static $loaders = array();
28 /**
29 * @var StandardAutoloader StandardAutoloader instance for resolving
30 * autoloader classes via the include_path
32 protected static $standardAutoloader;
34 /**
35 * Factory for autoloaders
37 * Options should be an array or Traversable object of the following structure:
38 * <code>
39 * array(
40 * '<autoloader class name>' => $autoloaderOptions,
41 * )
42 * </code>
44 * The factory will then loop through and instantiate each autoloader with
45 * the specified options, and register each with the spl_autoloader.
47 * You may retrieve the concrete autoloader instances later using
48 * {@link getRegisteredAutoloaders()}.
50 * Note that the class names must be resolvable on the include_path or via
51 * the Zend library, using PSR-0 rules (unless the class has already been
52 * loaded).
54 * @param array|Traversable $options (optional) options to use. Defaults to Zend\Loader\StandardAutoloader
55 * @return void
56 * @throws Exception\InvalidArgumentException for invalid options
57 * @throws Exception\InvalidArgumentException for unloadable autoloader classes
58 * @throws Exception\DomainException for autoloader classes not implementing SplAutoloader
60 public static function factory($options = null)
62 if (null === $options) {
63 if (!isset(static::$loaders[static::STANDARD_AUTOLOADER])) {
64 $autoloader = static::getStandardAutoloader();
65 $autoloader->register();
66 static::$loaders[static::STANDARD_AUTOLOADER] = $autoloader;
69 // Return so we don't hit the next check's exception (we're done here anyway)
70 return;
73 if (!is_array($options) && !($options instanceof Traversable)) {
74 require_once __DIR__ . '/Exception/InvalidArgumentException.php';
75 throw new Exception\InvalidArgumentException(
76 'Options provided must be an array or Traversable'
80 foreach ($options as $class => $autoloaderOptions) {
81 if (!isset(static::$loaders[$class])) {
82 $autoloader = static::getStandardAutoloader();
83 if (!class_exists($class) && !$autoloader->autoload($class)) {
84 require_once 'Exception/InvalidArgumentException.php';
85 throw new Exception\InvalidArgumentException(
86 sprintf('Autoloader class "%s" not loaded', $class)
90 if (!static::isSubclassOf($class, 'Zend\Loader\SplAutoloader')) {
91 require_once 'Exception/InvalidArgumentException.php';
92 throw new Exception\InvalidArgumentException(
93 sprintf('Autoloader class %s must implement Zend\\Loader\\SplAutoloader', $class)
97 if ($class === static::STANDARD_AUTOLOADER) {
98 $autoloader->setOptions($autoloaderOptions);
99 } else {
100 $autoloader = new $class($autoloaderOptions);
102 $autoloader->register();
103 static::$loaders[$class] = $autoloader;
104 } else {
105 static::$loaders[$class]->setOptions($autoloaderOptions);
111 * Get an list of all autoloaders registered with the factory
113 * Returns an array of autoloader instances.
115 * @return array
117 public static function getRegisteredAutoloaders()
119 return static::$loaders;
123 * Retrieves an autoloader by class name
125 * @param string $class
126 * @return SplAutoloader
127 * @throws Exception\InvalidArgumentException for non-registered class
129 public static function getRegisteredAutoloader($class)
131 if (!isset(static::$loaders[$class])) {
132 require_once 'Exception/InvalidArgumentException.php';
133 throw new Exception\InvalidArgumentException(sprintf('Autoloader class "%s" not loaded', $class));
135 return static::$loaders[$class];
139 * Unregisters all autoloaders that have been registered via the factory.
140 * This will NOT unregister autoloaders registered outside of the fctory.
142 * @return void
144 public static function unregisterAutoloaders()
146 foreach (static::getRegisteredAutoloaders() as $class => $autoloader) {
147 spl_autoload_unregister(array($autoloader, 'autoload'));
148 unset(static::$loaders[$class]);
153 * Unregister a single autoloader by class name
155 * @param string $autoloaderClass
156 * @return bool
158 public static function unregisterAutoloader($autoloaderClass)
160 if (!isset(static::$loaders[$autoloaderClass])) {
161 return false;
164 $autoloader = static::$loaders[$autoloaderClass];
165 spl_autoload_unregister(array($autoloader, 'autoload'));
166 unset(static::$loaders[$autoloaderClass]);
167 return true;
171 * Get an instance of the standard autoloader
173 * Used to attempt to resolve autoloader classes, using the
174 * StandardAutoloader. The instance is marked as a fallback autoloader, to
175 * allow resolving autoloaders not under the "Zend" namespace.
177 * @return SplAutoloader
179 protected static function getStandardAutoloader()
181 if (null !== static::$standardAutoloader) {
182 return static::$standardAutoloader;
186 if (!class_exists(static::STANDARD_AUTOLOADER)) {
187 // Extract the filename from the classname
188 $stdAutoloader = substr(strrchr(static::STANDARD_AUTOLOADER, '\\'), 1);
189 require_once __DIR__ . "/$stdAutoloader.php";
191 $loader = new StandardAutoloader();
192 static::$standardAutoloader = $loader;
193 return static::$standardAutoloader;
197 * Checks if the object has this class as one of its parents
199 * @see https://bugs.php.net/bug.php?id=53727
200 * @see https://github.com/zendframework/zf2/pull/1807
202 * @param string $className
203 * @param string $type
204 * @return bool
206 protected static function isSubclassOf($className, $type)
208 if (is_subclass_of($className, $type)) {
209 return true;
211 if (version_compare(PHP_VERSION, '5.3.7', '>=')) {
212 return false;
214 if (!interface_exists($type)) {
215 return false;
217 $r = new ReflectionClass($className);
218 return $r->implementsInterface($type);