1. sfAutoloadAgain.class.php
  2. /** * Autoload again for dev environments. * * @package symfony * @subpackage autoload * @author Kris Wallsmith * @version SVN: $Id: sfAutoloadAgain.class.php 22248 2009-09-22 17:15:16Z fabien $ */
  3. class sfAutoloadAgain
  4. {
  5. static protected
  6. $instance = null;
  7. protected
  8. $registered = false,
  9. $reloaded = false;
  10. /**
  11. * Returns the singleton autoloader.
  12. *
  13. * @return sfAutoloadAgain
  14. */
  15. static public function getInstance()
  16. {
  17. if (null === self::$instance)
  18. {
  19. self::$instance = new self();
  20. }
  21. return self::$instance;
  22. }
  23. /**
  24. * Constructor.
  25. */
  26. protected function __construct()
  27. {
  28. }
  29. /**
  30. * Reloads the autoloader.
  31. *
  32. * @param string $class
  33. *
  34. * @return boolean
  35. */
  36. public function autoload($class)
  37. {
  38. // only reload once
  39. if ($this->reloaded)
  40. {
  41. return false;
  42. }
  43. $autoloads = spl_autoload_functions();
  44. // as of PHP 5.2.11, spl_autoload_functions() returns the object as the first element of the array instead of the class name
  45. if (version_compare(PHP_VERSION, '5.2.11', '>='))
  46. {
  47. foreach ($autoloads as $position => $autoload)
  48. {
  49. if ($this === $autoload[0])
  50. {
  51. break;
  52. }
  53. }
  54. }
  55. else
  56. {
  57. $position = array_search(array(__CLASS__, 'autoload'), $autoloads, true);
  58. }
  59. if (isset($autoloads[$position + 1]))
  60. {
  61. $this->unregister();
  62. $this->register();
  63. // since we're rearranged things, call the chain again
  64. spl_autoload_call($class);
  65. return class_exists($class, false) || interface_exists($class, false);
  66. }
  67. $autoload = sfAutoload::getInstance();
  68. $autoload->reloadClasses(true);
  69. $this->reloaded = true;
  70. return $autoload->autoload($class);
  71. }
  72. /**
  73. * Returns true if the autoloader is registered.
  74. *
  75. * @return boolean
  76. */
  77. public function isRegistered()
  78. {
  79. return $this->registered;
  80. }
  81. /**
  82. * Registers the autoloader function.
  83. */
  84. public function register()
  85. {
  86. if (!$this->isRegistered())
  87. {
  88. spl_autoload_register(array($this, 'autoload'));
  89. $this->registered = true;
  90. }
  91. }
  92. /**
  93. * Unregisters the autoloader function.
  94. */
  95. public function unregister()
  96. {
  97. spl_autoload_unregister(array($this, 'autoload'));
  98. $this->registered = false;
  99. }
  100. }

Debug toolbar