Drupal investigation

DebugClassLoader.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 version 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. * Constructor.
  30. *
  31. * @param object $classFinder
  32. */
  33. public function __construct($classFinder)
  34. {
  35. $this->classFinder = $classFinder;
  36. }
  37. /**
  38. * Gets the wrapped class loader.
  39. *
  40. * @return object a class loader instance
  41. */
  42. public function getClassLoader()
  43. {
  44. return $this->classFinder;
  45. }
  46. /**
  47. * Replaces all autoloaders implementing a findFile method by a DebugClassLoader wrapper.
  48. */
  49. public static function enable()
  50. {
  51. if (!is_array($functions = spl_autoload_functions())) {
  52. return;
  53. }
  54. foreach ($functions as $function) {
  55. spl_autoload_unregister($function);
  56. }
  57. foreach ($functions as $function) {
  58. if (is_array($function) && !$function[0] instanceof self && method_exists($function[0], 'findFile')) {
  59. $function = array(new static($function[0]), 'loadClass');
  60. }
  61. spl_autoload_register($function);
  62. }
  63. }
  64. /**
  65. * Unregisters this instance as an autoloader.
  66. */
  67. public function unregister()
  68. {
  69. spl_autoload_unregister(array($this, 'loadClass'));
  70. }
  71. /**
  72. * Finds a file by class name.
  73. *
  74. * @param string $class A class name to resolve to file
  75. *
  76. * @return string|null
  77. */
  78. public function findFile($class)
  79. {
  80. return $this->classFinder->findFile($class) ?: null;
  81. }
  82. /**
  83. * Loads the given class or interface.
  84. *
  85. * @param string $class The name of the class
  86. *
  87. * @return bool|null True, if loaded
  88. *
  89. * @throws \RuntimeException
  90. */
  91. public function loadClass($class)
  92. {
  93. if ($file = $this->classFinder->findFile($class)) {
  94. require $file;
  95. if (!class_exists($class, false) && !interface_exists($class, false) && (!function_exists('trait_exists') || !trait_exists($class, false))) {
  96. if (false !== strpos($class, '/')) {
  97. 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));
  98. }
  99. 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));
  100. }
  101. return true;
  102. }
  103. }
  104. }