DebugClassLoader.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. @trigger_error('The '.__NAMESPACE__.'\DebugClassLoader class is deprecated since Symfony 2.4 and will be removed in 3.0. Use the Symfony\Component\Debug\DebugClassLoader class instead.', E_USER_DEPRECATED);
  12. /**
  13. * Autoloader checking if the class is really defined in the file found.
  14. *
  15. * The DebugClassLoader will wrap all registered autoloaders providing a
  16. * findFile method and will throw an exception if a file is found but does
  17. * not declare the class.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. * @author Christophe Coevoet <stof@notk.org>
  21. *
  22. * @deprecated since version 2.4, to be removed in 3.0.
  23. * Use {@link \Symfony\Component\Debug\DebugClassLoader} instead.
  24. */
  25. class DebugClassLoader
  26. {
  27. private $classFinder;
  28. /**
  29. * @param object $classFinder
  30. */
  31. public function __construct($classFinder)
  32. {
  33. $this->classFinder = $classFinder;
  34. }
  35. /**
  36. * Gets the wrapped class loader.
  37. *
  38. * @return object a class loader instance
  39. */
  40. public function getClassLoader()
  41. {
  42. return $this->classFinder;
  43. }
  44. /**
  45. * Replaces all autoloaders implementing a findFile method by a DebugClassLoader wrapper.
  46. */
  47. public static function enable()
  48. {
  49. if (!\is_array($functions = spl_autoload_functions())) {
  50. return;
  51. }
  52. foreach ($functions as $function) {
  53. spl_autoload_unregister($function);
  54. }
  55. foreach ($functions as $function) {
  56. if (\is_array($function) && !$function[0] instanceof self && method_exists($function[0], 'findFile')) {
  57. $function = array(new static($function[0]), 'loadClass');
  58. }
  59. spl_autoload_register($function);
  60. }
  61. }
  62. /**
  63. * Unregisters this instance as an autoloader.
  64. */
  65. public function unregister()
  66. {
  67. spl_autoload_unregister(array($this, 'loadClass'));
  68. }
  69. /**
  70. * Finds a file by class name.
  71. *
  72. * @param string $class A class name to resolve to file
  73. *
  74. * @return string|null
  75. */
  76. public function findFile($class)
  77. {
  78. return $this->classFinder->findFile($class) ?: null;
  79. }
  80. /**
  81. * Loads the given class or interface.
  82. *
  83. * @param string $class The name of the class
  84. *
  85. * @return bool|null True, if loaded
  86. *
  87. * @throws \RuntimeException
  88. */
  89. public function loadClass($class)
  90. {
  91. if ($file = $this->classFinder->findFile($class)) {
  92. require $file;
  93. if (!class_exists($class, false) && !interface_exists($class, false) && (!\function_exists('trait_exists') || !trait_exists($class, false))) {
  94. if (false !== strpos($class, '/')) {
  95. 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));
  96. }
  97. 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));
  98. }
  99. return true;
  100. }
  101. }
  102. }