123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <?php
- /*
- * This file is part of the symfony package.
- * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- /**
- * 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 <fabien.potencier@symfony-project.com>
- * @version SVN: $Id: sfSimpleAutoload.class.php 14689 2009-01-13 16:33:49Z Kris.Wallsmith $
- */
- 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();
- }
- /**
- * Retrieves the singleton instance of this class.
- *
- * @param string $cacheFile The file path to save the cache
- *
- * @return sfSimpleAutoload A sfSimpleAutoload implementation instance.
- */
- static public function getInstance($cacheFile = null)
- {
- if (!isset(self::$instance))
- {
- self::$instance = new sfSimpleAutoload($cacheFile);
- }
- return self::$instance;
- }
- /**
- * Register sfSimpleAutoload in spl autoloader.
- *
- * @return void
- */
- 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;
- }
- /**
- * Unregister sfSimpleAutoload from spl autoloader.
- *
- * @return void
- */
- static public function unregister()
- {
- spl_autoload_unregister(array(self::getInstance(), 'autoload'));
- self::$registered = false;
- }
- /**
- * Handles autoloading of classes.
- *
- * @param string A class name.
- *
- * @return boolean Returns true if the class has been loaded
- */
- public function autoload($class)
- {
- // class already exists
- if (class_exists($class, false) || interface_exists($class, false))
- {
- return true;
- }
- // we have a class path, let's include it
- if (isset($this->classes[$class]))
- {
- require($this->classes[$class]);
- return true;
- }
- return false;
- }
- /**
- * Loads the cache.
- */
- 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;
- }
- /**
- * Saves the cache.
- */
- 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;
- }
- }
- /**
- * Reloads cache.
- */
- 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;
- }
- /**
- * Removes the cache.
- */
- public function removeCache()
- {
- @unlink($this->cacheFile);
- }
- /**
- * Adds a directory to the autoloading system if not yet present and give it the highest possible precedence.
- *
- * @param string The directory to look for classes
- * @param string The extension to look for
- */
- 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);
- }
- }
- }
- /**
- * Adds files to the autoloading system.
- *
- * @param array An array of files
- * @param Boolean Whether to register those files as single entities (used when reloading)
- */
- public function addFiles(array $files, $register = true)
- {
- foreach ($files as $file)
- {
- $this->addFile($file, $register);
- }
- }
- /**
- * Adds a file to the autoloading system.
- *
- * @param string A file path
- * @param Boolean Whether to register those files as single entities (used when reloading)
- */
- 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;
- }
- }
|