1. sfAutoload.class.php
  2. /** * sfAutoload class. * * This class is a singleton as PHP seems to be unable to register 2 autoloaders that are instances * of the same class (why?). * * @package symfony * @subpackage autoload * @author Fabien Potencier * @version SVN: $Id: sfAutoload.class.php 23205 2009-10-20 13:20:17Z Kris.Wallsmith $ */
  3. class sfAutoload
  4. {
  5. static protected
  6. $freshCache = false,
  7. $instance = null;
  8. protected
  9. $overriden = array(),
  10. $classes = array();
  11. protected function __construct()
  12. {
  13. }
  14. /**
  15. * Retrieves the singleton instance of this class.
  16. *
  17. * @return sfCoreAutoload A sfCoreAutoload implementation instance.
  18. */
  19. static public function getInstance()
  20. {
  21. if (!isset(self::$instance))
  22. {
  23. self::$instance = new sfAutoload();
  24. }
  25. return self::$instance;
  26. }
  27. /**
  28. * Register sfAutoload in spl autoloader.
  29. *
  30. * @return void
  31. */
  32. static public function register()
  33. {
  34. ini_set('unserialize_callback_func', 'spl_autoload_call');
  35. if (false === spl_autoload_register(array(self::getInstance(), 'autoload')))
  36. {
  37. throw new sfException(sprintf('Unable to register %s::autoload as an autoloading method.', get_class(self::getInstance())));
  38. }
  39. }
  40. /**
  41. * Unregister sfAutoload from spl autoloader.
  42. *
  43. * @return void
  44. */
  45. static public function unregister()
  46. {
  47. spl_autoload_unregister(array(self::getInstance(), 'autoload'));
  48. }
  49. /**
  50. * Sets the path for a particular class.
  51. *
  52. * @param string $class A PHP class name
  53. * @param string $path An absolute path
  54. */
  55. public function setClassPath($class, $path)
  56. {
  57. $class = strtolower($class);
  58. $this->overriden[$class] = $path;
  59. $this->classes[$class] = $path;
  60. }
  61. /**
  62. * Returns the path where a particular class can be found.
  63. *
  64. * @param string $class A PHP class name
  65. *
  66. * @return string|null An absolute path
  67. */
  68. public function getClassPath($class)
  69. {
  70. $class = strtolower($class);
  71. return isset($this->classes[$class]) ? $this->classes[$class] : null;
  72. }
  73. /**
  74. * Reloads the autoloader.
  75. *
  76. * @param boolean $force Whether to force a reload
  77. *
  78. * @return boolean True if the reload was successful, otherwise false
  79. */
  80. public function reloadClasses($force = false)
  81. {
  82. // only (re)load the autoloading cache once per request
  83. if (self::$freshCache && !$force)
  84. {
  85. return false;
  86. }
  87. $configuration = sfProjectConfiguration::getActive();
  88. if (!$configuration || !$configuration instanceof sfApplicationConfiguration)
  89. {
  90. return false;
  91. }
  92. self::$freshCache = true;
  93. if (file_exists($configuration->getConfigCache()->getCacheName('config/autoload.yml')))
  94. {
  95. self::$freshCache = false;
  96. if ($force)
  97. {
  98. unlink($configuration->getConfigCache()->getCacheName('config/autoload.yml'));
  99. }
  100. }
  101. $file = $configuration->getConfigCache()->checkConfig('config/autoload.yml');
  102. $this->classes = include($file);
  103. foreach ($this->overriden as $class => $path)
  104. {
  105. $this->classes[$class] = $path;
  106. }
  107. return true;
  108. }
  109. /**
  110. * Handles autoloading of classes that have been specified in autoload.yml.
  111. *
  112. * @param string $class A class name.
  113. *
  114. * @return boolean Returns true if the class has been loaded
  115. */
  116. public function autoload($class)
  117. {
  118. // load the list of autoload classes
  119. if (!$this->classes)
  120. {
  121. self::reloadClasses();
  122. }
  123. return self::loadClass($class);
  124. }
  125. /**
  126. * Tries to load a class that has been specified in autoload.yml.
  127. *
  128. * @param string $class A class name.
  129. *
  130. * @return boolean Returns true if the class has been loaded
  131. */
  132. public function loadClass($class)
  133. {
  134. $class = strtolower($class);
  135. // class already exists
  136. if (class_exists($class, false) || interface_exists($class, false))
  137. {
  138. return true;
  139. }
  140. // we have a class path, let's include it
  141. if (isset($this->classes[$class]))
  142. {
  143. try
  144. {
  145. require $this->classes[$class];
  146. }
  147. catch (sfException $e)
  148. {
  149. $e->printStackTrace();
  150. }
  151. catch (Exception $e)
  152. {
  153. sfException::createFromException($e)->printStackTrace();
  154. }
  155. return true;
  156. }
  157. // see if the file exists in the current module lib directory
  158. if (
  159. sfContext::hasInstance()
  160. &&
  161. ($module = sfContext::getInstance()->getModuleName())
  162. &&
  163. isset($this->classes[$module.'/'.$class])
  164. )
  165. {
  166. try
  167. {
  168. require $this->classes[$module.'/'.$class];
  169. }
  170. catch (sfException $e)
  171. {
  172. $e->printStackTrace();
  173. }
  174. catch (Exception $e)
  175. {
  176. sfException::createFromException($e)->printStackTrace();
  177. }
  178. return true;
  179. }
  180. return false;
  181. }
  182. }

Debug toolbar