1. sfSimpleAutoload.class.php
  2. /** * sfSimpleAutoload 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: sfSimpleAutoload.class.php 23205 2009-10-20 13:20:17Z Kris.Wallsmith $ */
  3. class sfSimpleAutoload
  4. {
  5. static protected
  6. $registered = false,
  7. $instance = null;
  8. protected
  9. $cacheFile = null,
  10. $cacheLoaded = false,
  11. $cacheChanged = false,
  12. $dirs = array(),
  13. $files = array(),
  14. $classes = array(),
  15. $overriden = array();
  16. protected function __construct($cacheFile = null)
  17. {
  18. if (null !== $cacheFile)
  19. {
  20. $this->cacheFile = $cacheFile;
  21. }
  22. $this->loadCache();
  23. }
  24. /**
  25. * Retrieves the singleton instance of this class.
  26. *
  27. * @param string $cacheFile The file path to save the cache
  28. *
  29. * @return sfSimpleAutoload A sfSimpleAutoload implementation instance.
  30. */
  31. static public function getInstance($cacheFile = null)
  32. {
  33. if (!isset(self::$instance))
  34. {
  35. self::$instance = new sfSimpleAutoload($cacheFile);
  36. }
  37. return self::$instance;
  38. }
  39. /**
  40. * Register sfSimpleAutoload in spl autoloader.
  41. *
  42. * @return void
  43. */
  44. static public function register()
  45. {
  46. if (self::$registered)
  47. {
  48. return;
  49. }
  50. ini_set('unserialize_callback_func', 'spl_autoload_call');
  51. if (false === spl_autoload_register(array(self::getInstance(), 'autoload')))
  52. {
  53. throw new sfException(sprintf('Unable to register %s::autoload as an autoloading method.', get_class(self::getInstance())));
  54. }
  55. if (self::getInstance()->cacheFile)
  56. {
  57. register_shutdown_function(array(self::getInstance(), 'saveCache'));
  58. }
  59. self::$registered = true;
  60. }
  61. /**
  62. * Unregister sfSimpleAutoload from spl autoloader.
  63. *
  64. * @return void
  65. */
  66. static public function unregister()
  67. {
  68. spl_autoload_unregister(array(self::getInstance(), 'autoload'));
  69. self::$registered = false;
  70. }
  71. /**
  72. * Handles autoloading of classes.
  73. *
  74. * @param string $class A class name.
  75. *
  76. * @return boolean Returns true if the class has been loaded
  77. */
  78. public function autoload($class)
  79. {
  80. $class = strtolower($class);
  81. // class already exists
  82. if (class_exists($class, false) || interface_exists($class, false))
  83. {
  84. return true;
  85. }
  86. // we have a class path, let's include it
  87. if (isset($this->classes[$class]))
  88. {
  89. try
  90. {
  91. require $this->classes[$class];
  92. }
  93. catch (sfException $e)
  94. {
  95. $e->printStackTrace();
  96. }
  97. catch (Exception $e)
  98. {
  99. sfException::createFromException($e)->printStackTrace();
  100. }
  101. return true;
  102. }
  103. return false;
  104. }
  105. /**
  106. * Loads the cache.
  107. */
  108. public function loadCache()
  109. {
  110. if (!$this->cacheFile || !is_readable($this->cacheFile))
  111. {
  112. return;
  113. }
  114. list($this->classes, $this->dirs, $this->files) = unserialize(file_get_contents($this->cacheFile));
  115. $this->cacheLoaded = true;
  116. $this->cacheChanged = false;
  117. }
  118. /**
  119. * Saves the cache.
  120. */
  121. public function saveCache()
  122. {
  123. if ($this->cacheChanged)
  124. {
  125. if (is_writable(dirname($this->cacheFile)))
  126. {
  127. file_put_contents($this->cacheFile, serialize(array($this->classes, $this->dirs, $this->files)));
  128. }
  129. $this->cacheChanged = false;
  130. }
  131. }
  132. /**
  133. * Reloads cache.
  134. */
  135. public function reload()
  136. {
  137. $this->classes = array();
  138. $this->cacheLoaded = false;
  139. foreach ($this->dirs as $dir)
  140. {
  141. $this->addDirectory($dir);
  142. }
  143. foreach ($this->files as $file)
  144. {
  145. $this->addFile($file);
  146. }
  147. foreach ($this->overriden as $class => $path)
  148. {
  149. $this->classes[$class] = $path;
  150. }
  151. $this->cacheLoaded = true;
  152. $this->cacheChanged = true;
  153. }
  154. /**
  155. * Removes the cache.
  156. */
  157. public function removeCache()
  158. {
  159. @unlink($this->cacheFile);
  160. }
  161. /**
  162. * Adds a directory to the autoloading system if not yet present and give it the highest possible precedence.
  163. *
  164. * @param string $dir The directory to look for classes
  165. * @param string $ext The extension to look for
  166. */
  167. public function addDirectory($dir, $ext = '.php')
  168. {
  169. $finder = sfFinder::type('file')->follow_link()->name('*'.$ext);
  170. if ($dirs = glob($dir))
  171. {
  172. foreach ($dirs as $dir)
  173. {
  174. if (false !== $key = array_search($dir, $this->dirs))
  175. {
  176. unset($this->dirs[$key]);
  177. $this->dirs[] = $dir;
  178. if ($this->cacheLoaded)
  179. {
  180. continue;
  181. }
  182. }
  183. else
  184. {
  185. $this->dirs[] = $dir;
  186. }
  187. $this->cacheChanged = true;
  188. $this->addFiles($finder->in($dir), false);
  189. }
  190. }
  191. }
  192. /**
  193. * Adds files to the autoloading system.
  194. *
  195. * @param array $files An array of files
  196. * @param Boolean $register Whether to register those files as single entities (used when reloading)
  197. */
  198. public function addFiles(array $files, $register = true)
  199. {
  200. foreach ($files as $file)
  201. {
  202. $this->addFile($file, $register);
  203. }
  204. }
  205. /**
  206. * Adds a file to the autoloading system.
  207. *
  208. * @param string $file A file path
  209. * @param Boolean $register Whether to register those files as single entities (used when reloading)
  210. */
  211. public function addFile($file, $register = true)
  212. {
  213. if (!is_file($file))
  214. {
  215. return;
  216. }
  217. if (in_array($file, $this->files))
  218. {
  219. if ($this->cacheLoaded)
  220. {
  221. return;
  222. }
  223. }
  224. else
  225. {
  226. if ($register)
  227. {
  228. $this->files[] = $file;
  229. }
  230. }
  231. if ($register)
  232. {
  233. $this->cacheChanged = true;
  234. }
  235. preg_match_all('~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi', file_get_contents($file), $classes);
  236. foreach ($classes[1] as $class)
  237. {
  238. $this->classes[strtolower($class)] = $file;
  239. }
  240. }
  241. /**
  242. * Sets the path for a particular class.
  243. *
  244. * @param string $class A PHP class name
  245. * @param string $path An absolute path
  246. */
  247. public function setClassPath($class, $path)
  248. {
  249. $class = strtolower($class);
  250. $this->overriden[$class] = $path;
  251. $this->classes[$class] = $path;
  252. }
  253. /**
  254. * Returns the path where a particular class can be found.
  255. *
  256. * @param string $class A PHP class name
  257. *
  258. * @return string|null An absolute path
  259. */
  260. public function getClassPath($class)
  261. {
  262. $class = strtolower($class);
  263. return isset($this->classes[$class]) ? $this->classes[$class] : null;
  264. }
  265. /**
  266. * Loads configuration from the supplied files.
  267. *
  268. * @param array $files An array of autoload.yml files
  269. *
  270. * @see sfAutoloadConfigHandler
  271. */
  272. public function loadConfiguration(array $files)
  273. {
  274. $config = new sfAutoloadConfigHandler();
  275. foreach ($config->evaluate($files) as $class => $file)
  276. {
  277. $this->setClassPath($class, $file);
  278. }
  279. }
  280. }

Debug toolbar