123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?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.
- */
- /**
- * sfAutoload 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: sfAutoload.class.php 12676 2008-11-06 08:57:12Z fabien $
- */
- class sfAutoload
- {
- static protected
- $freshCache = false,
- $instance = null;
- protected
- $overriden = array(),
- $classes = array();
- protected function __construct()
- {
- }
- /**
- * Retrieves the singleton instance of this class.
- *
- * @return sfCoreAutoload A sfCoreAutoload implementation instance.
- */
- static public function getInstance()
- {
- if (!isset(self::$instance))
- {
- self::$instance = new sfAutoload();
- }
- return self::$instance;
- }
- /**
- * Register sfAutoload in spl autoloader.
- *
- * @return void
- */
- static public function register()
- {
- 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())));
- }
- }
- /**
- * Unregister sfAutoload from spl autoloader.
- *
- * @return void
- */
- static public function unregister()
- {
- spl_autoload_unregister(array(self::getInstance(), 'autoload'));
- }
- public function setClassPath($class, $path)
- {
- $this->overriden[$class] = $path;
- $this->classes[$class] = $path;
- }
- public function getClassPath($class)
- {
- return isset($this->classes[$class]) ? $this->classes[$class] : null;
- }
- public function reloadClasses($force = false)
- {
- // only (re)load the autoloading cache once per request
- if (self::$freshCache)
- {
- return;
- }
- $configuration = sfProjectConfiguration::getActive();
- if (!$configuration || !$configuration instanceof sfApplicationConfiguration)
- {
- return;
- }
- self::$freshCache = true;
- if (file_exists($configuration->getConfigCache()->getCacheName('config/autoload.yml')))
- {
- self::$freshCache = false;
- if ($force)
- {
- unlink($configuration->getConfigCache()->getCacheName('config/autoload.yml'));
- }
- }
- $file = $configuration->getConfigCache()->checkConfig('config/autoload.yml');
- $this->classes = include($file);
- foreach ($this->overriden as $class => $path)
- {
- $this->classes[$class] = $path;
- }
- }
- /**
- * Handles autoloading of classes that have been specified in autoload.yml.
- *
- * @param string $class A class name.
- *
- * @return boolean Returns true if the class has been loaded
- */
- public function autoload($class)
- {
- // load the list of autoload classes
- if (!$this->classes)
- {
- self::reloadClasses();
- }
- return self::loadClass($class);
- }
- public function autoloadAgain($class)
- {
- self::reloadClasses(true);
- return self::loadClass($class);
- }
- /**
- * Tries to load a class that has been specified in autoload.yml.
- *
- * @param string $class A class name.
- *
- * @return boolean Returns true if the class has been loaded
- */
- public function loadClass($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;
- }
- // see if the file exists in the current module lib directory
- // must be in a module context
- if (sfContext::hasInstance() && ($module = sfContext::getInstance()->getModuleName()) && isset($this->classes[$module.'/'.$class]))
- {
- require($this->classes[$module.'/'.$class]);
- return true;
- }
- return false;
- }
- }
|