4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\ClassLoader;
14 @trigger_error('The '.__NAMESPACE__.'\DebugClassLoader class is deprecated since version 2.4 and will be removed in 3.0. Use the Symfony\Component\Debug\DebugClassLoader class instead.', E_USER_DEPRECATED);
17 * Autoloader checking if the class is really defined in the file found.
19 * The DebugClassLoader will wrap all registered autoloaders providing a
20 * findFile method and will throw an exception if a file is found but does
21 * not declare the class.
23 * @author Fabien Potencier <fabien@symfony.com>
24 * @author Christophe Coevoet <stof@notk.org>
26 * @deprecated since version 2.4, to be removed in 3.0.
27 * Use {@link \Symfony\Component\Debug\DebugClassLoader} instead.
29 class DebugClassLoader
36 * @param object $classFinder
38 public function __construct($classFinder)
40 $this->classFinder = $classFinder;
44 * Gets the wrapped class loader.
46 * @return object a class loader instance
48 public function getClassLoader()
50 return $this->classFinder;
54 * Replaces all autoloaders implementing a findFile method by a DebugClassLoader wrapper.
56 public static function enable()
58 if (!is_array($functions = spl_autoload_functions())) {
62 foreach ($functions as $function) {
63 spl_autoload_unregister($function);
66 foreach ($functions as $function) {
67 if (is_array($function) && !$function[0] instanceof self && method_exists($function[0], 'findFile')) {
68 $function = array(new static($function[0]), 'loadClass');
71 spl_autoload_register($function);
76 * Unregisters this instance as an autoloader.
78 public function unregister()
80 spl_autoload_unregister(array($this, 'loadClass'));
84 * Finds a file by class name.
86 * @param string $class A class name to resolve to file
90 public function findFile($class)
92 return $this->classFinder->findFile($class) ?: null;
96 * Loads the given class or interface.
98 * @param string $class The name of the class
100 * @return bool|null True, if loaded
102 * @throws \RuntimeException
104 public function loadClass($class)
106 if ($file = $this->classFinder->findFile($class)) {
109 if (!class_exists($class, false) && !interface_exists($class, false) && (!function_exists('trait_exists') || !trait_exists($class, false))) {
110 if (false !== strpos($class, '/')) {
111 throw new \RuntimeException(sprintf('Trying to autoload a class with an invalid name "%s". Be careful that the namespace separator is "\" in PHP, not "/".', $class));
114 throw new \RuntimeException(sprintf('The autoloader expected class "%s" to be defined in file "%s". The file was found but the class was not in it, the class name or namespace probably has a typo.', $class, $file));