sfSimpleAutoload.class.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfSimpleAutoload class.
  11. *
  12. * This class is a singleton as PHP seems to be unable to register 2 autoloaders that are instances
  13. * of the same class (why?).
  14. *
  15. * @package symfony
  16. * @subpackage autoload
  17. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  18. * @version SVN: $Id: sfSimpleAutoload.class.php 14689 2009-01-13 16:33:49Z Kris.Wallsmith $
  19. */
  20. class sfSimpleAutoload
  21. {
  22. static protected
  23. $registered = false,
  24. $instance = null;
  25. protected
  26. $cacheFile = null,
  27. $cacheLoaded = false,
  28. $cacheChanged = false,
  29. $dirs = array(),
  30. $files = array(),
  31. $classes = array();
  32. protected function __construct($cacheFile = null)
  33. {
  34. if (!is_null($cacheFile))
  35. {
  36. $this->cacheFile = $cacheFile;
  37. }
  38. $this->loadCache();
  39. }
  40. /**
  41. * Retrieves the singleton instance of this class.
  42. *
  43. * @param string $cacheFile The file path to save the cache
  44. *
  45. * @return sfSimpleAutoload A sfSimpleAutoload implementation instance.
  46. */
  47. static public function getInstance($cacheFile = null)
  48. {
  49. if (!isset(self::$instance))
  50. {
  51. self::$instance = new sfSimpleAutoload($cacheFile);
  52. }
  53. return self::$instance;
  54. }
  55. /**
  56. * Register sfSimpleAutoload in spl autoloader.
  57. *
  58. * @return void
  59. */
  60. static public function register()
  61. {
  62. if (self::$registered)
  63. {
  64. return;
  65. }
  66. ini_set('unserialize_callback_func', 'spl_autoload_call');
  67. if (false === spl_autoload_register(array(self::getInstance(), 'autoload')))
  68. {
  69. throw new sfException(sprintf('Unable to register %s::autoload as an autoloading method.', get_class(self::getInstance())));
  70. }
  71. if (self::getInstance()->cacheFile)
  72. {
  73. register_shutdown_function(array(self::getInstance(), 'saveCache'));
  74. }
  75. self::$registered = true;
  76. }
  77. /**
  78. * Unregister sfSimpleAutoload from spl autoloader.
  79. *
  80. * @return void
  81. */
  82. static public function unregister()
  83. {
  84. spl_autoload_unregister(array(self::getInstance(), 'autoload'));
  85. self::$registered = false;
  86. }
  87. /**
  88. * Handles autoloading of classes.
  89. *
  90. * @param string A class name.
  91. *
  92. * @return boolean Returns true if the class has been loaded
  93. */
  94. public function autoload($class)
  95. {
  96. // class already exists
  97. if (class_exists($class, false) || interface_exists($class, false))
  98. {
  99. return true;
  100. }
  101. // we have a class path, let's include it
  102. if (isset($this->classes[$class]))
  103. {
  104. require($this->classes[$class]);
  105. return true;
  106. }
  107. return false;
  108. }
  109. /**
  110. * Loads the cache.
  111. */
  112. public function loadCache()
  113. {
  114. if (!$this->cacheFile || !is_readable($this->cacheFile))
  115. {
  116. return;
  117. }
  118. list($this->classes, $this->dirs, $this->files) = unserialize(file_get_contents($this->cacheFile));
  119. $this->cacheLoaded = true;
  120. $this->cacheChanged = false;
  121. }
  122. /**
  123. * Saves the cache.
  124. */
  125. public function saveCache()
  126. {
  127. if ($this->cacheChanged)
  128. {
  129. if (is_writable(dirname($this->cacheFile)))
  130. {
  131. file_put_contents($this->cacheFile, serialize(array($this->classes, $this->dirs, $this->files)));
  132. }
  133. $this->cacheChanged = false;
  134. }
  135. }
  136. /**
  137. * Reloads cache.
  138. */
  139. public function reload()
  140. {
  141. $this->classes = array();
  142. $this->cacheLoaded = false;
  143. foreach ($this->dirs as $dir)
  144. {
  145. $this->addDirectory($dir);
  146. }
  147. foreach ($this->files as $file)
  148. {
  149. $this->addFile($file);
  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 The directory to look for classes
  165. * @param string 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 An array of files
  196. * @param Boolean 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 A file path
  209. * @param Boolean 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[$class] = $file;
  239. }
  240. }
  241. public function setClassPath($class, $path)
  242. {
  243. $this->overriden[$class] = $path;
  244. $this->classes[$class] = $path;
  245. }
  246. }