-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[2.1][HttpFoundation] Added some basic meta-data to Session #3718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
29bd787
d9fd14f
402254c
ec3f88f
39141e8
2f03b31
4fc04fa
8a0e6d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpFoundation\Session\Storage; | ||
|
||
use Symfony\Component\HttpFoundation\Session\SessionBagInterface; | ||
|
||
/** | ||
* Metadata container. | ||
* | ||
* Adds metadata to the session. | ||
* | ||
* @author Drak <drak@zikula.org> | ||
*/ | ||
class MetadataBag implements SessionBagInterface | ||
{ | ||
const CREATED = 'c'; | ||
const UPDATED = 'u'; | ||
const LIFETIME = 'l'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $name = '__metadata'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $storageKey; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $meta = array(); | ||
|
||
/** | ||
* Unix timestamp. | ||
* | ||
* @var integer | ||
*/ | ||
private $lastUsed; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string $storageKey The key used to store bag in the session. | ||
*/ | ||
public function __construct($storageKey = '_sf2_meta') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You missed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the keys here dont matter, it's purely internal to avoid name clashes - it just needs to be sufficiently obscure. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know. For God's sake, it's just for consistency. |
||
{ | ||
$this->storageKey = $storageKey; | ||
$this->meta = array(self::CREATED => 0, self::UPDATED => 0, self::LIFETIME => 0); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function initialize(array &$array) | ||
{ | ||
$this->meta = &$array; | ||
|
||
if (isset($array[self::CREATED])) { | ||
$this->lastUsed = $this->meta[self::UPDATED]; | ||
$this->meta[self::UPDATED] = time(); | ||
} else { | ||
$this->stampCreated(); | ||
} | ||
} | ||
|
||
/** | ||
* Gets the lifetime that the session cookie was set with. | ||
* | ||
* @return integer | ||
*/ | ||
public function getLifetime() | ||
{ | ||
return $this->meta[self::LIFETIME]; | ||
} | ||
|
||
/** | ||
* Stamps a new session's metadata. | ||
* | ||
* @param integer $lifetime Sets the cookie lifetime for the session cookie. A null value | ||
* will leave the system settings unchanged, 0 sets the cookie | ||
* to expire with browser session. Time is in seconds, and is | ||
* not a Unix timestamp. | ||
*/ | ||
public function stampNew($lifetime = null) | ||
{ | ||
$this->stampCreated($lifetime); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getStorageKey() | ||
{ | ||
return $this->storageKey; | ||
} | ||
|
||
/** | ||
* Gets the created timestamp metadata. | ||
* | ||
* @return integer Unix timestamp | ||
*/ | ||
public function getCreated() | ||
{ | ||
return $this->meta[self::CREATED]; | ||
} | ||
|
||
/** | ||
* Gets the last used metadata. | ||
* | ||
* @return integer Unix timestamp | ||
*/ | ||
public function getLastUsed() | ||
{ | ||
return $this->lastUsed; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function clear() | ||
{ | ||
// nothing to do | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* Sets name. | ||
* | ||
* @param string $name | ||
*/ | ||
public function setName($name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
private function stampCreated($lifetime = null) | ||
{ | ||
$timeStamp = time(); | ||
$this->meta[self::CREATED] = $this->meta[self::UPDATED] = $this->lastUsed = $timeStamp; | ||
$this->meta[self::LIFETIME] = (null === $lifetime) ? ini_get('session.cookie_lifetime') : $lifetime; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't be simpler to implement such count in that
bag
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not part of this PR - the method were just moved within the file.