2010-02-17 13:55:05 +00:00
|
|
|
<?php
|
|
|
|
|
2010-06-24 06:49:27 +01:00
|
|
|
namespace Symfony\Framework\FoundationBundle;
|
2010-02-17 13:55:05 +00:00
|
|
|
|
|
|
|
use Symfony\Components\EventDispatcher\EventDispatcher;
|
|
|
|
use Symfony\Components\EventDispatcher\Event;
|
2010-06-24 06:49:27 +01:00
|
|
|
use Symfony\Framework\FoundationBundle\Session\SessionInterface;
|
2010-02-17 13:55:05 +00:00
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony framework.
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-07 01:51:29 +01:00
|
|
|
* User.
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
2010-04-07 01:51:29 +01:00
|
|
|
* @package Symfony
|
2010-06-24 06:49:27 +01:00
|
|
|
* @subpackage Framework_FoundationBundle
|
2010-02-17 13:55:05 +00:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
|
|
|
class User
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $session;
|
2010-06-07 09:10:00 +01:00
|
|
|
protected $locale;
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $attributes;
|
|
|
|
protected $oldFlashes;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the user class
|
|
|
|
*
|
|
|
|
* @param EventDispatcher $dispatcher A EventDispatcher instance
|
|
|
|
* @param SessionInterface $session A SessionInterface instance
|
|
|
|
* @param array $options An array of options
|
|
|
|
*/
|
|
|
|
public function __construct(EventDispatcher $dispatcher, SessionInterface $session, $options = array())
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->dispatcher = $dispatcher;
|
|
|
|
$this->session = $session;
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->setAttributes($session->read('_user', array(
|
|
|
|
'_flash' => array(),
|
2010-06-07 09:10:00 +01:00
|
|
|
'_locale' => isset($options['default_locale']) ? $options['default_locale'] : 'en',
|
2010-05-06 12:25:53 +01:00
|
|
|
)));
|
|
|
|
|
|
|
|
// flag current flash to be removed at shutdown
|
|
|
|
$this->oldFlashes = array_flip(array_keys($this->getFlashMessages()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a user attribute
|
|
|
|
*
|
|
|
|
* @param string $name The attribute name
|
|
|
|
* @param mixed $default The default value
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getAttribute($name, $default = null)
|
|
|
|
{
|
|
|
|
return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets an user attribute.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function setAttribute($name, $value)
|
|
|
|
{
|
|
|
|
$this->attributes[$name] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns user attributes
|
|
|
|
*
|
|
|
|
* @return array User attributes
|
|
|
|
*/
|
|
|
|
public function getAttributes()
|
|
|
|
{
|
|
|
|
return $this->attributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets user attributes
|
|
|
|
*
|
|
|
|
* @param array Attributes
|
|
|
|
*/
|
|
|
|
public function setAttributes($attributes)
|
|
|
|
{
|
|
|
|
$this->attributes = $attributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-07 09:10:00 +01:00
|
|
|
* Returns the user locale
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-06-07 09:10:00 +01:00
|
|
|
public function getLocale()
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2010-06-07 09:10:00 +01:00
|
|
|
return $this->getAttribute('_locale');
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-07 09:10:00 +01:00
|
|
|
* Sets the user locale.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-06-07 09:10:00 +01:00
|
|
|
* @param string $locale
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-06-07 09:10:00 +01:00
|
|
|
public function setLocale($locale)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2010-06-07 09:10:00 +01:00
|
|
|
if ($this->locale != $locale) {
|
|
|
|
$this->setAttribute('_locale', $locale);
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2010-06-07 09:10:00 +01:00
|
|
|
$this->dispatcher->notify(new Event($this, 'user.change_locale', array('locale' => $locale)));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFlashMessages()
|
|
|
|
{
|
|
|
|
return $this->attributes['_flash'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setFlashMessages($values)
|
|
|
|
{
|
|
|
|
$this->attributes['_flash'] = $values;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFlash($name, $default = null)
|
|
|
|
{
|
|
|
|
return $this->hasFlash($name) ? $this->attributes['_flash'][$name] : $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setFlash($name, $value)
|
|
|
|
{
|
|
|
|
$this->attributes['_flash'][$name] = $value;
|
|
|
|
unset($this->oldFlashes[$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasFlash($name)
|
|
|
|
{
|
|
|
|
return array_key_exists($name, $this->attributes['_flash']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
$this->attributes['_flash'] = array_diff_key($this->attributes['_flash'], $this->oldFlashes);
|
|
|
|
|
|
|
|
$this->session->write('_user', $this->attributes);
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
}
|