sfSimpleAutoload.class.php 5.8 KB

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