composer package updates
[openemr.git] / vendor / doctrine / common / lib / Doctrine / Common / ClassLoader.php
blob78eeb35d3a40dc49e2cbdf5cb97b523374dbbe68
1 <?php
2 /*
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\Common;
22 /**
23 * A <tt>ClassLoader</tt> is an autoloader for class files that can be
24 * installed on the SPL autoload stack. It is a class loader that either loads only classes
25 * of a specific namespace or all namespaces and it is suitable for working together
26 * with other autoloaders in the SPL autoload stack.
28 * If no include path is configured through the constructor or {@link setIncludePath}, a ClassLoader
29 * relies on the PHP <code>include_path</code>.
31 * @author Roman Borschel <roman@code-factory.org>
32 * @since 2.0
34 * @deprecated the ClassLoader is deprecated and will be removed in version 3.0 of doctrine/common.
36 class ClassLoader
38 /**
39 * PHP file extension.
41 * @var string
43 protected $fileExtension = '.php';
45 /**
46 * Current namespace.
48 * @var string|null
50 protected $namespace;
52 /**
53 * Current include path.
55 * @var string|null
57 protected $includePath;
59 /**
60 * PHP namespace separator.
62 * @var string
64 protected $namespaceSeparator = '\\';
66 /**
67 * Creates a new <tt>ClassLoader</tt> that loads classes of the
68 * specified namespace from the specified include path.
70 * If no include path is given, the ClassLoader relies on the PHP include_path.
71 * If neither a namespace nor an include path is given, the ClassLoader will
72 * be responsible for loading all classes, thereby relying on the PHP include_path.
74 * @param string|null $ns The namespace of the classes to load.
75 * @param string|null $includePath The base include path to use.
77 public function __construct($ns = null, $includePath = null)
79 $this->namespace = $ns;
80 $this->includePath = $includePath;
83 /**
84 * Sets the namespace separator used by classes in the namespace of this ClassLoader.
86 * @param string $sep The separator to use.
88 * @return void
90 public function setNamespaceSeparator($sep)
92 $this->namespaceSeparator = $sep;
95 /**
96 * Gets the namespace separator used by classes in the namespace of this ClassLoader.
98 * @return string
100 public function getNamespaceSeparator()
102 return $this->namespaceSeparator;
106 * Sets the base include path for all class files in the namespace of this ClassLoader.
108 * @param string|null $includePath
110 * @return void
112 public function setIncludePath($includePath)
114 $this->includePath = $includePath;
118 * Gets the base include path for all class files in the namespace of this ClassLoader.
120 * @return string|null
122 public function getIncludePath()
124 return $this->includePath;
128 * Sets the file extension of class files in the namespace of this ClassLoader.
130 * @param string $fileExtension
132 * @return void
134 public function setFileExtension($fileExtension)
136 $this->fileExtension = $fileExtension;
140 * Gets the file extension of class files in the namespace of this ClassLoader.
142 * @return string
144 public function getFileExtension()
146 return $this->fileExtension;
150 * Registers this ClassLoader on the SPL autoload stack.
152 * @return void
154 public function register()
156 spl_autoload_register([$this, 'loadClass']);
160 * Removes this ClassLoader from the SPL autoload stack.
162 * @return void
164 public function unregister()
166 spl_autoload_unregister([$this, 'loadClass']);
170 * Loads the given class or interface.
172 * @param string $className The name of the class to load.
174 * @return boolean TRUE if the class has been successfully loaded, FALSE otherwise.
176 public function loadClass($className)
178 if (self::typeExists($className)) {
179 return true;
182 if (! $this->canLoadClass($className)) {
183 return false;
186 require ($this->includePath !== null ? $this->includePath . DIRECTORY_SEPARATOR : '')
187 . str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $className)
188 . $this->fileExtension;
190 return self::typeExists($className);
194 * Asks this ClassLoader whether it can potentially load the class (file) with
195 * the given name.
197 * @param string $className The fully-qualified name of the class.
199 * @return boolean TRUE if this ClassLoader can load the class, FALSE otherwise.
201 public function canLoadClass($className)
203 if ($this->namespace !== null && strpos($className, $this->namespace.$this->namespaceSeparator) !== 0) {
204 return false;
207 $file = str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $className) . $this->fileExtension;
209 if ($this->includePath !== null) {
210 return is_file($this->includePath . DIRECTORY_SEPARATOR . $file);
213 return (false !== stream_resolve_include_path($file));
217 * Checks whether a class with a given name exists. A class "exists" if it is either
218 * already defined in the current request or if there is an autoloader on the SPL
219 * autoload stack that is a) responsible for the class in question and b) is able to
220 * load a class file in which the class definition resides.
222 * If the class is not already defined, each autoloader in the SPL autoload stack
223 * is asked whether it is able to tell if the class exists. If the autoloader is
224 * a <tt>ClassLoader</tt>, {@link canLoadClass} is used, otherwise the autoload
225 * function of the autoloader is invoked and expected to return a value that
226 * evaluates to TRUE if the class (file) exists. As soon as one autoloader reports
227 * that the class exists, TRUE is returned.
229 * Note that, depending on what kinds of autoloaders are installed on the SPL
230 * autoload stack, the class (file) might already be loaded as a result of checking
231 * for its existence. This is not the case with a <tt>ClassLoader</tt>, who separates
232 * these responsibilities.
234 * @param string $className The fully-qualified name of the class.
236 * @return boolean TRUE if the class exists as per the definition given above, FALSE otherwise.
238 public static function classExists($className)
240 return self::typeExists($className, true);
244 * Gets the <tt>ClassLoader</tt> from the SPL autoload stack that is responsible
245 * for (and is able to load) the class with the given name.
247 * @param string $className The name of the class.
249 * @return ClassLoader The <tt>ClassLoader</tt> for the class or NULL if no such <tt>ClassLoader</tt> exists.
251 public static function getClassLoader($className)
253 foreach (spl_autoload_functions() as $loader) {
254 if (is_array($loader)
255 && ($classLoader = reset($loader))
256 && $classLoader instanceof ClassLoader
257 && $classLoader->canLoadClass($className)
259 return $classLoader;
263 return null;
267 * Checks whether a given type exists
269 * @param string $type
270 * @param bool $autoload
272 * @return bool
274 private static function typeExists($type, $autoload = false)
276 return class_exists($type, $autoload)
277 || interface_exists($type, $autoload)
278 || trait_exists($type, $autoload);