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\Debug;
15 * Autoloader checking if the class is really defined in the file found.
17 * The ClassLoader will wrap all registered autoloaders
18 * and will throw an exception if a file is found but does
19 * not declare the class.
21 * @author Fabien Potencier <fabien@symfony.com>
22 * @author Christophe Coevoet <stof@notk.org>
23 * @author Nicolas Grekas <p@tchwork.com>
25 class DebugClassLoader
30 private static $caseCheck;
31 private static $deprecated = array();
32 private static $php7Reserved = array('int', 'float', 'bool', 'string', 'true', 'false', 'null');
33 private static $darwinCache = array('/' => array('/', array()));
38 * @param callable|object $classLoader Passing an object is @deprecated since version 2.5 and support for it will be removed in 3.0
40 public function __construct($classLoader)
42 $this->wasFinder = is_object($classLoader) && method_exists($classLoader, 'findFile');
44 if ($this->wasFinder) {
45 @trigger_error('The '.__METHOD__.' method will no longer support receiving an object into its $classLoader argument in 3.0.', E_USER_DEPRECATED);
46 $this->classLoader = array($classLoader, 'loadClass');
47 $this->isFinder = true;
49 $this->classLoader = $classLoader;
50 $this->isFinder = is_array($classLoader) && method_exists($classLoader[0], 'findFile');
53 if (!isset(self::$caseCheck)) {
54 $file = file_exists(__FILE__) ? __FILE__ : rtrim(realpath('.'), DIRECTORY_SEPARATOR);
55 $i = strrpos($file, DIRECTORY_SEPARATOR);
56 $dir = substr($file, 0, 1 + $i);
57 $file = substr($file, 1 + $i);
58 $test = strtoupper($file) === $file ? strtolower($file) : strtoupper($file);
59 $test = realpath($dir.$test);
61 if (false === $test || false === $i) {
62 // filesystem is case sensitive
64 } elseif (substr($test, -strlen($file)) === $file) {
65 // filesystem is case insensitive and realpath() normalizes the case of characters
67 } elseif (false !== stripos(PHP_OS, 'darwin')) {
68 // on MacOSX, HFS+ is case insensitive but realpath() doesn't normalize the case of characters
71 // filesystem case checks failed, fallback to disabling them
78 * Gets the wrapped class loader.
80 * @return callable|object A class loader. Since version 2.5, returning an object is @deprecated and support for it will be removed in 3.0
82 public function getClassLoader()
84 return $this->wasFinder ? $this->classLoader[0] : $this->classLoader;
88 * Wraps all autoloaders.
90 public static function enable()
92 // Ensures we don't hit https://bugs.php.net/42098
93 class_exists('Symfony\Component\Debug\ErrorHandler');
94 class_exists('Psr\Log\LogLevel');
96 if (!is_array($functions = spl_autoload_functions())) {
100 foreach ($functions as $function) {
101 spl_autoload_unregister($function);
104 foreach ($functions as $function) {
105 if (!is_array($function) || !$function[0] instanceof self) {
106 $function = array(new static($function), 'loadClass');
109 spl_autoload_register($function);
114 * Disables the wrapping.
116 public static function disable()
118 if (!is_array($functions = spl_autoload_functions())) {
122 foreach ($functions as $function) {
123 spl_autoload_unregister($function);
126 foreach ($functions as $function) {
127 if (is_array($function) && $function[0] instanceof self) {
128 $function = $function[0]->getClassLoader();
131 spl_autoload_register($function);
136 * Finds a file by class name.
138 * @param string $class A class name to resolve to file
140 * @return string|null
142 * @deprecated since version 2.5, to be removed in 3.0.
144 public function findFile($class)
146 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
148 if ($this->wasFinder) {
149 return $this->classLoader[0]->findFile($class);
154 * Loads the given class or interface.
156 * @param string $class The name of the class
158 * @return bool|null True, if loaded
160 * @throws \RuntimeException
162 public function loadClass($class)
164 ErrorHandler::stackErrors();
167 if ($this->isFinder) {
168 if ($file = $this->classLoader[0]->findFile($class)) {
172 call_user_func($this->classLoader, $class);
175 } catch (\Exception $e) {
176 ErrorHandler::unstackErrors();
179 } catch (\Throwable $e) {
180 ErrorHandler::unstackErrors();
185 ErrorHandler::unstackErrors();
187 $exists = class_exists($class, false) || interface_exists($class, false) || (function_exists('trait_exists') && trait_exists($class, false));
189 if ($class && '\\' === $class[0]) {
190 $class = substr($class, 1);
194 $refl = new \ReflectionClass($class);
195 $name = $refl->getName();
197 if ($name !== $class && 0 === strcasecmp($name, $class)) {
198 throw new \RuntimeException(sprintf('Case mismatch between loaded and declared class names: %s vs %s', $class, $name));
201 if (in_array(strtolower($refl->getShortName()), self::$php7Reserved)) {
202 @trigger_error(sprintf('%s uses a reserved class name (%s) that will break on PHP 7 and higher', $name, $refl->getShortName()), E_USER_DEPRECATED);
203 } elseif (preg_match('#\n \* @deprecated (.*?)\r?\n \*(?: @|/$)#s', $refl->getDocComment(), $notice)) {
204 self::$deprecated[$name] = preg_replace('#\s*\r?\n \* +#', ' ', $notice[1]);
206 if (2 > $len = 1 + (strpos($name, '\\', 1 + strpos($name, '\\')) ?: strpos($name, '_'))) {
210 switch ($ns = substr($name, 0, $len)) {
211 case 'Symfony\Bridge\\':
212 case 'Symfony\Bundle\\':
213 case 'Symfony\Component\\':
219 $parent = get_parent_class($class);
221 if (!$parent || strncmp($ns, $parent, $len)) {
222 if ($parent && isset(self::$deprecated[$parent]) && strncmp($ns, $parent, $len)) {
223 @trigger_error(sprintf('The %s class extends %s that is deprecated %s', $name, $parent, self::$deprecated[$parent]), E_USER_DEPRECATED);
226 $parentInterfaces = array();
227 $deprecatedInterfaces = array();
229 foreach (class_implements($parent) as $interface) {
230 $parentInterfaces[$interface] = 1;
234 foreach ($refl->getInterfaceNames() as $interface) {
235 if (isset(self::$deprecated[$interface]) && strncmp($ns, $interface, $len)) {
236 $deprecatedInterfaces[] = $interface;
238 foreach (class_implements($interface) as $interface) {
239 $parentInterfaces[$interface] = 1;
243 foreach ($deprecatedInterfaces as $interface) {
244 if (!isset($parentInterfaces[$interface])) {
245 @trigger_error(sprintf('The %s %s %s that is deprecated %s', $name, $refl->isInterface() ? 'interface extends' : 'class implements', $interface, self::$deprecated[$interface]), E_USER_DEPRECATED);
254 if (false !== strpos($class, '/')) {
255 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));
258 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));
260 if (self::$caseCheck) {
261 $real = explode('\\', $class.strrchr($file, '.'));
262 $tail = explode(DIRECTORY_SEPARATOR, str_replace('/', DIRECTORY_SEPARATOR, $file));
264 $i = count($tail) - 1;
265 $j = count($real) - 1;
267 while (isset($tail[$i], $real[$j]) && $tail[$i] === $real[$j]) {
272 array_splice($tail, 0, $i + 1);
274 if (self::$caseCheck && $tail) {
275 $tail = DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $tail);
276 $tailLen = strlen($tail);
277 $real = $refl->getFileName();
279 if (2 === self::$caseCheck) {
280 // realpath() on MacOSX doesn't normalize the case of characters
282 $i = 1 + strrpos($real, '/');
283 $file = substr($real, $i);
284 $real = substr($real, 0, $i);
286 if (isset(self::$darwinCache[$real])) {
289 $kDir = strtolower($real);
291 if (isset(self::$darwinCache[$kDir])) {
292 $real = self::$darwinCache[$kDir][0];
296 $real = getcwd().'/';
301 $i = strlen($dir) - 1;
302 while (!isset(self::$darwinCache[$k])) {
303 self::$darwinCache[$k] = array($dir, array());
304 self::$darwinCache[$dir] = &self::$darwinCache[$k];
306 while ('/' !== $dir[--$i]) {
308 $k = substr($k, 0, ++$i);
309 $dir = substr($dir, 0, $i--);
314 $dirFiles = self::$darwinCache[$kDir][1];
316 if (isset($dirFiles[$file])) {
319 $kFile = strtolower($file);
321 if (!isset($dirFiles[$kFile])) {
322 foreach (scandir($real, 2) as $f) {
327 } elseif ($f !== $k = strtolower($f)) {
332 self::$darwinCache[$kDir][1] = $dirFiles;
336 $real .= $dirFiles[$kFile];
339 if (0 === substr_compare($real, $tail, -$tailLen, $tailLen, true)
340 && 0 !== substr_compare($real, $tail, -$tailLen, $tailLen, false)
342 throw new \RuntimeException(sprintf('Case mismatch between class and real file names: %s vs %s in %s', substr($tail, -$tailLen + 1), substr($real, -$tailLen + 1), substr($real, 0, -$tailLen + 1)));