DebugUniversalClassLoader.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. $loader->useIncludePath($function[0]->getUseIncludePath());
  37. $function[0] = $loader;
  38. }
  39. spl_autoload_register($function);
  40. }
  41. }
  42. /**
  43. * {@inheritDoc}
  44. */
  45. public function loadClass($class)
  46. {
  47. if ($file = $this->findFile($class)) {
  48. require $file;
  49. if (!class_exists($class, false) && !interface_exists($class, false) && (!function_exists('trait_exists') || !trait_exists($class, false))) {
  50. 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));
  51. }
  52. }
  53. }
  54. }