[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/util/ -> sfContext.class.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the symfony package.
   5   * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
   6   * (c) 2004-2006 Sean Kerr.
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  /**
  13   * sfContext provides information about the current application context, such as
  14   * the module and action names and the module directory. References to the
  15   * current controller, request, and user implementation instances are also
  16   * provided.
  17   *
  18   * @package    symfony
  19   * @subpackage util
  20   * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  21   * @author     Sean Kerr <skerr@mojavi.org>
  22   * @version    SVN: $Id: sfContext.class.php 3493 2007-02-18 09:23:10Z fabien $
  23   */
  24  class sfContext
  25  {
  26    protected
  27      $actionStack       = null,
  28      $controller        = null,
  29      $databaseManager   = null,
  30      $request           = null,
  31      $response          = null,
  32      $storage           = null,
  33      $viewCacheManager  = null,
  34      $i18n              = null,
  35      $logger            = null,
  36      $user              = null;
  37  
  38    protected static
  39      $instance          = null;
  40  
  41    /**
  42     * Removes current sfContext instance
  43     *
  44     * This method only exists for testing purpose. Don't use it in your application code.
  45     */
  46    public static function removeInstance()
  47    {
  48      self::$instance = null;
  49    }
  50  
  51    protected function initialize()
  52    {
  53      $this->logger = sfLogger::getInstance();
  54      if (sfConfig::get('sf_logging_enabled'))
  55      {
  56        $this->logger->info('{sfContext} initialization');
  57      }
  58  
  59      if (sfConfig::get('sf_use_database'))
  60      {
  61        // setup our database connections
  62        $this->databaseManager = new sfDatabaseManager();
  63        $this->databaseManager->initialize();
  64      }
  65  
  66      // create a new action stack
  67      $this->actionStack = new sfActionStack();
  68  
  69      // include the factories configuration
  70      require(sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_config_dir_name').'/factories.yml'));
  71  
  72      // register our shutdown function
  73      register_shutdown_function(array($this, 'shutdown'));
  74    }
  75  
  76    /**
  77     * Retrieve the singleton instance of this class.
  78     *
  79     * @return sfContext A sfContext implementation instance.
  80     */
  81    public static function getInstance()
  82    {
  83      if (!isset(self::$instance))
  84      {
  85        $class = __CLASS__;
  86        self::$instance = new $class();
  87        self::$instance->initialize();
  88      }
  89  
  90      return self::$instance;
  91    }
  92  
  93    public static function hasInstance()
  94    {
  95      return isset(self::$instance);
  96    }
  97  
  98    /**
  99     * Retrieve the action name for this context.
 100     *
 101     * @return string The currently executing action name, if one is set,
 102     *                otherwise null.
 103     */
 104    public function getActionName()
 105    {
 106      // get the last action stack entry
 107      if ($this->actionStack && $lastEntry = $this->actionStack->getLastEntry())
 108      {
 109        return $lastEntry->getActionName();
 110      }
 111    }
 112  
 113  
 114    /**
 115     * Retrieve the ActionStack.
 116     *
 117     * @return sfActionStack the sfActionStack instance
 118     */
 119    public function getActionStack()
 120    {
 121      return $this->actionStack;
 122    }
 123  
 124    /**
 125     * Retrieve the controller.
 126     *
 127     * @return sfController The current sfController implementation instance.
 128     */
 129     public function getController()
 130     {
 131       return $this->controller;
 132     }
 133  
 134     public function getLogger()
 135     {
 136       return $this->logger;
 137     }
 138  
 139    /**
 140     * Retrieve a database connection from the database manager.
 141     *
 142     * This is a shortcut to manually getting a connection from an existing
 143     * database implementation instance.
 144     *
 145     * If the [sf_use_database] setting is off, this will return null.
 146     *
 147     * @param name A database name.
 148     *
 149     * @return mixed A Database instance.
 150     *
 151     * @throws <b>sfDatabaseException</b> If the requested database name does not exist.
 152     */
 153    public function getDatabaseConnection($name = 'default')
 154    {
 155      if ($this->databaseManager != null)
 156      {
 157        return $this->databaseManager->getDatabase($name)->getConnection();
 158      }
 159  
 160      return null;
 161    }
 162  
 163    public function retrieveObjects($class, $peerMethod)
 164    {
 165      $retrievingClass = 'sf'.ucfirst(sfConfig::get('sf_orm', 'propel')).'DataRetriever';
 166  
 167      return call_user_func(array($retrievingClass, 'retrieveObjects'), $class, $peerMethod);
 168    }
 169  
 170    /**
 171     * Retrieve the database manager.
 172     *
 173     * @return sfDatabaseManager The current sfDatabaseManager instance.
 174     */
 175    public function getDatabaseManager()
 176    {
 177      return $this->databaseManager;
 178    }
 179  
 180    /**
 181     * Retrieve the module directory for this context.
 182     *
 183     * @return string An absolute filesystem path to the directory of the
 184     *                currently executing module, if one is set, otherwise null.
 185     */
 186    public function getModuleDirectory()
 187    {
 188      // get the last action stack entry
 189      if ($this->actionStack && $lastEntry = $this->actionStack->getLastEntry())
 190      {
 191        return sfConfig::get('sf_app_module_dir').'/'.$lastEntry->getModuleName();
 192      }
 193    }
 194  
 195    /**
 196     * Retrieve the module name for this context.
 197     *
 198     * @return string The currently executing module name, if one is set,
 199     *                otherwise null.
 200     */
 201    public function getModuleName()
 202    {
 203      // get the last action stack entry
 204      if ($this->actionStack && $lastEntry = $this->actionStack->getLastEntry())
 205      {
 206        return $lastEntry->getModuleName();
 207      }
 208    }
 209  
 210    /**
 211     * Retrieve the curretn view instance for this context.
 212     *
 213     * @return sfView The currently view instance, if one is set,
 214     *                otherwise null.
 215     */
 216    public function getCurrentViewInstance()
 217    {
 218      // get the last action stack entry
 219      if ($this->actionStack && $lastEntry = $this->actionStack->getLastEntry())
 220      {
 221        return $lastEntry->getViewInstance();
 222      }
 223    }
 224  
 225    /**
 226     * Retrieve the request.
 227     *
 228     * @return sfRequest The current sfRequest implementation instance.
 229     */
 230    public function getRequest()
 231    {
 232      return $this->request;
 233    }
 234  
 235    /**
 236     * Retrieve the response.
 237     *
 238     * @return sfResponse The current sfResponse implementation instance.
 239     */
 240    public function getResponse()
 241    {
 242      return $this->response;
 243    }
 244  
 245    /**
 246     * Set the response object.
 247     *
 248     * @param sfResponse A sfResponse instance.
 249     *
 250     * @return void.
 251     */
 252    public function setResponse($response)
 253    {
 254      $this->response = $response;
 255    }
 256  
 257    /**
 258     * Retrieve the storage.
 259     *
 260     * @return sfStorage The current sfStorage implementation instance.
 261     */
 262    public function getStorage()
 263    {
 264      return $this->storage;
 265    }
 266  
 267    /**
 268     * Retrieve the view cache manager
 269     *
 270     * @return sfViewCacheManager The current sfViewCacheManager implementation instance.
 271     */
 272    public function getViewCacheManager()
 273    {
 274      return $this->viewCacheManager;
 275    }
 276  
 277    /**
 278     * Retrieve the i18n instance
 279     *
 280     * @return sfI18N The current sfI18N implementation instance.
 281     */
 282    public function getI18N()
 283    {
 284      if (!$this->i18n && sfConfig::get('sf_i18n'))
 285      {
 286        $this->i18n = sfI18N::getInstance();
 287        $this->i18n->initialize($this);
 288      }
 289  
 290      return $this->i18n;
 291    }
 292  
 293    /**
 294     * Retrieve the user.
 295     *
 296     * @return sfUser The current sfUser implementation instance.
 297     */
 298    public function getUser()
 299    {
 300      return $this->user;
 301    }
 302  
 303    /**
 304     * Execute the shutdown procedure.
 305     *
 306     * @return void
 307     */
 308    public function shutdown()
 309    {
 310      // shutdown all factories
 311      $this->getUser()->shutdown();
 312      $this->getStorage()->shutdown();
 313      $this->getRequest()->shutdown();
 314      $this->getResponse()->shutdown();
 315  
 316      if (sfConfig::get('sf_logging_enabled'))
 317      {
 318        $this->getLogger()->shutdown();
 319      }
 320  
 321      if (sfConfig::get('sf_use_database'))
 322      {
 323        $this->getDatabaseManager()->shutdown();
 324      }
 325  
 326      if (sfConfig::get('sf_cache'))
 327      {
 328        $this->getViewCacheManager()->shutdown();
 329      }
 330    }
 331  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7