[ Index ]
 

Code source de Symfony 1.0.0

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

title

Body

[fermer]

/lib/user/ -> sfUser.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   *
  14   * sfUser wraps a client session and provides accessor methods for user
  15   * attributes. It also makes storing and retrieving multiple page form data
  16   * rather easy by allowing user attributes to be stored in namespaces, which
  17   * help organize data.
  18   *
  19   * @package    symfony
  20   * @subpackage user
  21   * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  22   * @author     Sean Kerr <skerr@mojavi.org>
  23   * @version    SVN: $Id: sfUser.class.php 2971 2006-12-08 12:14:14Z fabien $
  24   */
  25  class sfUser
  26  {
  27    /**
  28     * The namespace under which attributes will be stored.
  29     */
  30    const ATTRIBUTE_NAMESPACE = 'symfony/user/sfUser/attributes';
  31  
  32    const CULTURE_NAMESPACE = 'symfony/user/sfUser/culture';
  33  
  34    protected
  35      $parameterHolder = null,
  36      $attributeHolder = null,
  37      $culture         = null,
  38      $context         = null;
  39  
  40    /**
  41     * Retrieve the current application context.
  42     *
  43     * @return Context A Context instance.
  44     */
  45    public function getContext()
  46    {
  47      return $this->context;
  48    }
  49  
  50    /**
  51     * Initialize this User.
  52     *
  53     * @param Context A Context instance.
  54     * @param array   An associative array of initialization parameters.
  55     *
  56     * @return bool true, if initialization completes successfully, otherwise
  57     *              false.
  58     *
  59     * @throws <b>sfInitializationException</b> If an error occurs while initializing this User.
  60     */
  61    public function initialize($context, $parameters = array())
  62    {
  63      $this->context = $context;
  64  
  65      $this->parameterHolder = new sfParameterHolder();
  66      $this->parameterHolder->add($parameters);
  67  
  68      $this->attributeHolder = new sfParameterHolder(self::ATTRIBUTE_NAMESPACE);
  69  
  70      // read attributes from storage
  71      $attributes = $context->getStorage()->read(self::ATTRIBUTE_NAMESPACE);
  72      if (is_array($attributes))
  73      {
  74        foreach ($attributes as $namespace => $values)
  75        {
  76          $this->attributeHolder->add($values, $namespace);
  77        }
  78      }
  79  
  80      // set the user culture to sf_culture parameter if present in the request
  81      // otherwise
  82      //  - use the culture defined in the user session
  83      //  - use the default culture set in i18n.yml
  84      if (!($culture = $context->getRequest()->getParameter('sf_culture')))
  85      {
  86        if (null === ($culture = $context->getStorage()->read(self::CULTURE_NAMESPACE)))
  87        {
  88          $culture = sfConfig::get('sf_i18n_default_culture', 'en');
  89        }
  90      }
  91  
  92      $this->setCulture($culture);
  93    }
  94  
  95    /**
  96     * Retrieve a new sfUser implementation instance.
  97     *
  98     * @param string A sfUser implementation name
  99     *
 100     * @return User A sfUser implementation instance.
 101     *
 102     * @throws <b>sfFactoryException</b> If a user implementation instance cannot
 103     */
 104    public static function newInstance($class)
 105    {
 106      // the class exists
 107      $object = new $class();
 108  
 109      if (!($object instanceof sfUser))
 110      {
 111        // the class name is of the wrong type
 112        $error = 'Class "%s" is not of the type sfUser';
 113        $error = sprintf($error, $class);
 114  
 115        throw new sfFactoryException($error);
 116      }
 117  
 118      return $object;
 119    }
 120  
 121    /**
 122     * Sets culture.
 123     *
 124     * @param  string culture
 125     */
 126    public function setCulture($culture)
 127    {
 128      if ($this->culture != $culture)
 129      {
 130        $this->culture = $culture;
 131  
 132        // change the message format object with the new culture
 133        if (sfConfig::get('sf_i18n'))
 134        {
 135          $this->context->getI18N()->setCulture($culture);
 136        }
 137  
 138        // add the culture in the routing default parameters
 139        sfConfig::set('sf_routing_defaults', array_merge((array) sfConfig::get('sf_routing_defaults'), array('sf_culture' => $culture)));
 140      }
 141    }
 142  
 143    /**
 144     * Gets culture.
 145     *
 146     * @return string
 147     */
 148    public function getCulture()
 149    {
 150      return $this->culture;
 151    }
 152  
 153    public function getParameterHolder()
 154    {
 155      return $this->parameterHolder;
 156    }
 157  
 158    public function getAttributeHolder()
 159    {
 160      return $this->attributeHolder;
 161    }
 162  
 163    public function getAttribute($name, $default = null, $ns = null)
 164    {
 165      return $this->attributeHolder->get($name, $default, $ns);
 166    }
 167  
 168    public function hasAttribute($name, $ns = null)
 169    {
 170      return $this->attributeHolder->has($name, $ns);
 171    }
 172  
 173    public function setAttribute($name, $value, $ns = null)
 174    {
 175      return $this->attributeHolder->set($name, $value, $ns);
 176    }
 177  
 178    public function getParameter($name, $default = null, $ns = null)
 179    {
 180      return $this->parameterHolder->get($name, $default, $ns);
 181    }
 182  
 183    public function hasParameter($name, $ns = null)
 184    {
 185      return $this->parameterHolder->has($name, $ns);
 186    }
 187  
 188    public function setParameter($name, $value, $ns = null)
 189    {
 190      return $this->parameterHolder->set($name, $value, $ns);
 191    }
 192  
 193    /**
 194     * Execute the shutdown procedure.
 195     *
 196     * @return void
 197     */
 198    public function shutdown()
 199    {
 200      $storage = $this->getContext()->getStorage();
 201  
 202      $attributes = array();
 203      foreach ($this->attributeHolder->getNamespaces() as $namespace)
 204      {
 205        $attributes[$namespace] = $this->attributeHolder->getAll($namespace);
 206      }
 207  
 208      // write attributes to the storage
 209      $storage->write(self::ATTRIBUTE_NAMESPACE, $attributes);
 210  
 211      // write culture to the storage
 212      $storage->write(self::CULTURE_NAMESPACE, $this->culture);
 213  
 214      session_write_close();
 215    }
 216  
 217    public function __call($method, $arguments)
 218    {
 219      if (!$callable = sfMixer::getCallable('sfUser:'.$method))
 220      {
 221        throw new sfException(sprintf('Call to undefined method sfUser::%s', $method));
 222      }
 223  
 224      array_unshift($arguments, $this);
 225  
 226      return call_user_func_array($callable, $arguments);
 227    }
 228  }


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