123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <?php
- class sfSimpleAutoload
- {
- static protected
- $registered = false,
- $instance = null;
- protected
- $cacheFile = null,
- $cacheLoaded = false,
- $cacheChanged = false,
- $dirs = array(),
- $files = array(),
- $classes = array();
- protected function __construct($cacheFile = null)
- {
- if (!is_null($cacheFile))
- {
- $this->cacheFile = $cacheFile;
- }
- $this->loadCache();
- }
-
- static public function getInstance($cacheFile = null)
- {
- if (!isset(self::$instance))
- {
- self::$instance = new sfSimpleAutoload($cacheFile);
- }
- return self::$instance;
- }
-
- static public function register()
- {
- if (self::$registered)
- {
- return;
- }
- ini_set('unserialize_callback_func', 'spl_autoload_call');
- if (false === spl_autoload_register(array(self::getInstance(), 'autoload')))
- {
- throw new sfException(sprintf('Unable to register %s::autoload as an autoloading method.', get_class(self::getInstance())));
- }
- if (self::getInstance()->cacheFile)
- {
- register_shutdown_function(array(self::getInstance(), 'saveCache'));
- }
- self::$registered = true;
- }
-
- static public function unregister()
- {
- spl_autoload_unregister(array(self::getInstance(), 'autoload'));
- self::$registered = false;
- }
-
- public function autoload($class)
- {
-
- if (class_exists($class, false) || interface_exists($class, false))
- {
- return true;
- }
-
- if (isset($this->classes[$class]))
- {
- require($this->classes[$class]);
- return true;
- }
- return false;
- }
-
- public function loadCache()
- {
- if (!$this->cacheFile || !is_readable($this->cacheFile))
- {
- return;
- }
- list($this->classes, $this->dirs, $this->files) = unserialize(file_get_contents($this->cacheFile));
- $this->cacheLoaded = true;
- $this->cacheChanged = false;
- }
-
- public function saveCache()
- {
- if ($this->cacheChanged)
- {
- if (is_writable(dirname($this->cacheFile)))
- {
- file_put_contents($this->cacheFile, serialize(array($this->classes, $this->dirs, $this->files)));
- }
- $this->cacheChanged = false;
- }
- }
-
- public function reload()
- {
- $this->classes = array();
- $this->cacheLoaded = false;
- foreach ($this->dirs as $dir)
- {
- $this->addDirectory($dir);
- }
- foreach ($this->files as $file)
- {
- $this->addFile($file);
- }
- $this->cacheLoaded = true;
- $this->cacheChanged = true;
- }
-
- public function removeCache()
- {
- @unlink($this->cacheFile);
- }
-
- public function addDirectory($dir, $ext = '.php')
- {
- $finder = sfFinder::type('file')->follow_link()->name('*'.$ext);
- if($dirs = glob($dir))
- {
- foreach ($dirs as $dir)
- {
- if (false !== ($key = array_search($dir, $this->dirs)))
- {
- unset($this->dirs[$key]);
- $this->dirs[] = $dir;
- if ($this->cacheLoaded)
- {
- continue;
- }
- }
- else
- {
- $this->dirs[] = $dir;
- }
- $this->cacheChanged = true;
- $this->addFiles($finder->in($dir), false);
- }
- }
- }
-
- public function addFiles(array $files, $register = true)
- {
- foreach ($files as $file)
- {
- $this->addFile($file, $register);
- }
- }
-
- public function addFile($file, $register = true)
- {
- if (!is_file($file))
- {
- return;
- }
- if (in_array($file, $this->files))
- {
- if ($this->cacheLoaded)
- {
- return;
- }
- }
- else
- {
- if ($register)
- {
- $this->files[] = $file;
- }
- }
- if ($register)
- {
- $this->cacheChanged = true;
- }
- preg_match_all('~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi', file_get_contents($file), $classes);
- foreach ($classes[1] as $class)
- {
- $this->classes[$class] = $file;
- }
- }
- public function setClassPath($class, $path)
- {
- $this->overriden[$class] = $path;
- $this->classes[$class] = $path;
- }
- }
|