PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Group-I/jobeet/lib/vendor/symfony/lib/autoload/sfSimpleAutoload.class.php

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