DebugUniversalClassLoader.php

Namespace

Symfony\Component\ClassLoader

Classes

Namesort descending Description
DebugUniversalClassLoader Checks that the class is actually declared in the included file.

File

Symfony/Component/ClassLoader/DebugUniversalClassLoader.php
View source
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\ClassLoader;
  11. /**
  12. * Checks that the class is actually declared in the included file.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class DebugUniversalClassLoader extends UniversalClassLoader
  17. {
  18. /**
  19. * Replaces all regular UniversalClassLoader instances by a DebugUniversalClassLoader ones.
  20. */
  21. static public function enable()
  22. {
  23. if (!is_array($functions = spl_autoload_functions())) {
  24. return;
  25. }
  26. foreach ($functions as $function) {
  27. spl_autoload_unregister($function);
  28. }
  29. foreach ($functions as $function) {
  30. if (is_array($function) && $function[0] instanceof UniversalClassLoader) {
  31. $loader = new static();
  32. $loader->registerNamespaceFallbacks($function[0]->getNamespaceFallbacks());
  33. $loader->registerPrefixFallbacks($function[0]->getPrefixFallbacks());
  34. $loader->registerNamespaces($function[0]->getNamespaces());
  35. $loader->registerPrefixes($function[0]->getPrefixes());
  36. $function[0] = $loader;
  37. }
  38. spl_autoload_register($function);
  39. }
  40. }
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public function loadClass($class)
  45. {
  46. if ($file = $this->findFile($class)) {
  47. require $file;
  48. if (!class_exists($class, false) && !interface_exists($class, false) && (!function_exists('trait_exists') || !trait_exists($class, false))) {
  49. 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));
  50. }
  51. }
  52. }
  53. }