merged branch drak/session_refactor (PR #2853)
Commits -------cb6fdb1
[HttpFoundation] removed Session::close()c59d880
Docblocks.b8df162
Correct instanceof condition.8a01dd5
renamed getFlashes() to getFlashBag() to avoid clashes282d3ae
updated CHANGELOG for 2.10f6c50a
[HttpFoundation] added some method for a better BC146a502
[FrameworkBundle] added some service aliases to avoid some BC breaks93d81a1
[HttpFoundation] removed configuration for session storages in session.xml as we cannot provide a way to configure them (like before this PR anyway)74ccf70
reverted5b7ef11650
(Simplify session storage class names now we have a separate namespace for sessions)91f4f8a
[HttpFoundation] changed default flash bag to auto-expires to keep BC0494250
removed unused use statements7878a0a
[HttpFoundation] renamed pop() to all() and getAll() to all()0d2745f
[HttpFoundation] Remove constants from FlashBagInterfacedad60ef
[HttpFoundation] Add back get defaults and small clean-up.5b7ef11
[HttpFoundation] Simplify session storage class names now we have a separate namespace for sessions.27530cb
[HttpFoundation] Moved session related classes to own sub-namespace.4683915
[HttpFoundation] Free bags from session storage and move classes to their own namespaces.d64939a
[DoctrineBridge] Refactored driver for changed interface.f9951a3
Fixed formatting.398acc9
[HttpFoundation] Reworked flashes to maintain same behaviour as in Symfony 2.0f98f9ae
[HttpFoundation] Refactor for DRY code.9dd4dbe
Documentation, changelogs and coding standards.1ed6ee3
[DoctribeBridge][SecurityBundle][WebProfiler] Refactor code for HttpFoundation changes.7aaf024
[FrameworkBundle] Refactored code for changes to HttpFoundation component.669bc96
[HttpFoundation] Added pure Memcache, Memcached and Null storage drivers.e185c8d
[HttpFoundation] Refactored component for session workflow.85b5c43
[HttpFoundation] Added drivers for PHP native session save handlers, files, sqlite, memcache and memcached.57ef984
[HttpFoundation] Added unit and functional testing session storage objects.3a263dc
[HttpFoundation] Introduced session storage base class and interfaces.c969423
[HttpFoundation] Added FlashBagInterface and concrete implementation.39288bc
[HttpFoundation] Added AttributesInterface and AttributesBagInterface and concrete implementations. Discussion ---------- [2.1][HttpFoundation] Refactor session handling and flash messages Bug fix: yes Feature addition: yes Backwards compatibility break: yes Symfony2 tests pass: yes Fixes the following tickets: #2607, #2591, #2717, #2773 References the following tickets: #2592, #2543, #2541, #2510, #2714, #2684 Todo: - __Introduction__ This extensive PR is a refactor with minimal BC breaks of the `[HttpFoundation]` component's session management which fixes several issues in the current implementation. This PR includes all necessary changes to other bundles and components is documented in the `CHANGELOG-2.1` and `UPGRADING-2.1`. __Summary of Changes__ __Session:__ - Session object now implements `SessionInterface` __Attributes:__ - Attributes now handled by `AttributeBagInterface` - Added two AttributeBag implementations: `AttributeBag` replicates the current Symfony2 attributes behaviour, and the second, `NamespacedAttributeBag` introduces structured namespaced representation using '/' in the key. Both are BC. `FrameworkBundle` defaults to the old behaviour. __Flash messages:__ - Flash messages now handled by `FlashBagInterface` - Introduced `FlashBag` which changes the way flash messages expire, they now expire on use rather than automatically, useful for ESI. - Introduced `AutoExpireFlashBag` (default) which replicates the old automatic expiry behaviour of flash messages. __Session Storage:__ - Introduced a base object, `AbstractSessionStorage` for session storage drivers - Introduced a `SessionSaveHandlerInterface` when using custom session save handlers - Introduced a `NullSessionStorage` driver which allows for unsaved sessions - Introduced new session storage drivers for Memcache and Memcached - Introduced new session storage drivers for PHP's native SQLite, Memcache and Memcached support __General:__ - Fixed bugs where session attributes are not saved and all cases where flash messages would get lost - Wrote new tests, refactored related existing tests and increased test coverage extensively. __Rationale/Details__ I'll explain more detail in the following sections. __Unit Tests__ All unit and functional tests pass. __Note on Functional Testing__ I've introduced `MockFileSessionStorage` which replaces `FilesystemSessionStorage` to emulate a PHP session for functional testing. Essentially the same methodology of functional testing has been maintained but without interrupting the other session storage drivers interaction with real PHP sessions. The service is now called `session.storage.mock_file`. __Session Workflow__ PHP sessions follow a specific workflow which is not being followed by the current session management implementation and is responsible for some unpredictable bugs and behaviours. Basically, PHP session workflow is as follows: `open`, `read`, `write`, `close`. In between these can occur, `destroy` and `garbage collection`. These actions are handled by `session save handlers` and one is always registered in all cases. By default, the `files` save handler (internally to PHP) is registered by PHP at the start of code execution. PHP offers the possibility to change the save handler to another internal type, for example one provided by a PHP extension (think SQLite, Memcache etc), or you can register a user type and set your own handlers. However __in all cases__ PHP requires the handlers. The handlers are called when the following things occur: - `open` and `read` when `session_start()` or the session autostarts when PHP outputs some display - `destroy` when `session_regenerate_id(true)` is called - `write` and `close` when PHP shuts down or when `session_write_close()` is called - `garbage collection` is called randomly according to configurable probability The next very important aspect of this PR is that `$_SESSION` plays an important part in this workflow because the contents of the $_SESSION is populated __after__ the `read` handler returns the previously saved serialised session data. The `write` handler is sent the serialised `$_SESSION` contents for save. Please note the serialisation is a different format to `serialize()`. For this reason, any session implementation cannot get rid of using `$_SESSION`. I wrote more details on this issue [here](https://github.com/symfony/symfony/issues/2607#issuecomment-2858300) In order to make writing session storage drivers simple, I created a light base class `AbstractSessionStorage` and the `SessionSaveHandlerInterface` which allows you to quickly write native and custom save handler drivers. __Flash Messages [BC BREAK]__ Flash messages currently allow representation of a single message per `$name`. Fabien designed the original system so that `$name` was equivalent to flash message type. The current PR changes the fact that Flash messages expire explicitly when retrieved for display to the user as opposed to immediately on the next page load. The last issue fixes potential cases when flash messages are lost due to an unexpected intervening page-load (an error for example). The API `get()` has a flag which allows you to override the `clear()` action. __Flash message translation__ This PR does not cover translation of flash messages because messages should be translated before calling the flash message API. This is because flash messages are used to present messages to the user after a specific action, and in any case, immediately on the next page load. Since we know the locale of the request in every case we can translate the message before storing. Secondly, translation is simply a string manipulation. Translation API calls should always have the raw untranslated string present because it allows for extraction of translation catalogs. For a complete answer see my answer [here](https://github.com/symfony/symfony/pull/2543#issuecomment-2858707) __Session attribute and structured namespacing__ __This has been implemented without changing the current default behaviour__ but details are below for the alternative: Attributes are currently stored in a flat array which limits the potential of session attributes: Here are some examples to see why this 'structured namespace' methodology is extremely convenient over using a flat system. Let's look at an example with csrf tokens. Let's say we have multiple csrftokens stored by form ID (allowing multiple forms on the page and tabbed browsing). If we're using a flat system, you might have 'tokens' => array('a' => 'a6c1e0b6', 'b' => 'f4a7b1f3') With a flat system when you get the key `tokens`, you will get back an array, so now you have to analyse the array. So if you simply want to add another token, you have to follow three steps: get the session attribute `tokens`, have to add to the array, and lastly set the entire array back to the session. $tokens = $session->get('tokens'); $tokens['c'] = $value; $session->set('tokens', $tokens); Doable, but you can see it's pretty long winded. With structured namespacing you can simply do: $session->set('c', $value, '/tokens'); There are several ways to implement this, either with an additional `$namespace` argument, or by treating a character in the `$key` as a namespacer. `NamespacedAttributeBag` treats `/` as a namespacer so you can represent `user.tokens/a` for example. The namespace character is configurable in `NamespacedAttributeBag`. --------------------------------------------------------------------------- by marijn at 2011-12-18T15:43:17Z I haven't read the code yet but the description from this PR and your line of thought seem very well structured. Seems like a big +1 for me. --------------------------------------------------------------------------- by lsmith77 at 2011-12-19T16:01:19Z @deviantintegral could you look over this to see if it really addresses everything you wanted with PR #2510 ? --------------------------------------------------------------------------- by deviantintegral at 2011-12-24T20:12:03Z I've read through the documentation and upgrade notes, and I can't see anything that's obviously missing from #2510. Being able to support multiple flashes per type is the most important, and the API looks reasonable to me. Drupal does support supressing repeat messages, but that can easily be implemented in our code unless there's a compelling case for it to be a part of Symfony. I wonder if PHP memcache support is required in Symfony given the availability of memcached. I'm not familiar with how other parts of Symfony handle it, but there is often quite a bit of confusion between the two PHP extensions. It could be simpler to remove one, or add a bit of info describing or linking to why there are two nearly identical classes. Is it possible to make one class inherit from the other (memcached is a child of memcache)? --------------------------------------------------------------------------- by Fristi at 2011-12-24T20:29:46Z Interesting, maybe add: session events as I did with the current impl: https://github.com/Fristi/SessionBundle --------------------------------------------------------------------------- by drak at 2011-12-25T00:50:03Z @deviantintegral - I agree about the confusion between memcache and memcached but actually, it is necessary to support both because `memcached` is not available everywhere. For example on Debian Lenny and RHEL/CentOS 5, only memcache is available by default. This would preclude a massive amount of shared hosting environments. Also, it is not possible to inherit one from the other, they are completely different drivers. @Fristi - I also thought about the events, but they do not belong as part of the standalone component as this would create a coupling to the event dispatcher. The way you have done it, ie, in a bundle is the right way to achieve it. --------------------------------------------------------------------------- by matheo at 2011-12-25T01:12:00Z Impressive work, looks like a big improvement and deserves a big +1 --------------------------------------------------------------------------- by datiecher at 2011-12-26T11:57:12Z Took some time to grok all the changes in this PR but all in all it is a keeper. Specially the new flash message API, it's really nicer to work with it then the previous one. Nicely done @drak! --------------------------------------------------------------------------- by lsmith77 at 2012-01-02T15:00:00Z @fabpot did you have time to review this yet? with all the work @drak has done its important that he gets some feedback soon. its clear this PR breaks BC in ways we never wanted to allow. but i think this PR also clearly explains why its necessary none the less. --------------------------------------------------------------------------- by drak at 2012-01-02T15:41:53Z @fabpot - I have removed the WIP status from this PR now and rebased against the current master branch. --------------------------------------------------------------------------- by Tobion at 2012-01-07T07:13:38Z From what I read from the IRC chat logs, the main concern of @fabpot is whether we really need multiple flash messages per type. I'm in favor of this PR and just want to add one point to this discussion. At the moment you can add multiple flash messages of different type/category/identifier. For example you can specify one error message and one info message after an operation. I think most agree that this can be usefull. But then it makes semantically no sense that you currently cannot add 2 info messages. This approach feels a bit half-done. So I think this PR eliminates this paradox. --------------------------------------------------------------------------- by drak at 2012-01-07T09:11:07Z For reference there is also a discussion started by @lsmith77 on the mailing list at https://groups.google.com/forum/#!topic/symfony-devs/cy4wokD0mQI --------------------------------------------------------------------------- by dlsniper at 2012-01-07T16:02:15Z @drak I could also add the next scenario that I currently have to live with, in addition to @lsmith77 ones. I had this issue while working on our shopping cart implementation for a customer where the customer wanted to show the unavailability of the items as different lines in the 'flash-error' section of the cart. We had to set an array as the 'flash message' in order to display that information. So in this case for example having the flash messages types as array would actually make more sense that sending an array to the flasher. Plus the the other issue we had was that we also wanted to add another error in the message but we had to do a check to see if the flash message is an array already or we need to make it an array. I think it's better not to impose a limit of this sort and let the users be able to handle every scenario, even if some are rare, rather that forcing users to overcome limitations such as these. I really hope this PR gets approved faster and thanks everyone for their hard work :) --------------------------------------------------------------------------- by Tobion at 2012-01-07T21:01:07Z @dlsniper I think you misinterpreted my point. --------------------------------------------------------------------------- by dlsniper at 2012-01-07T21:04:04Z @Tobion I'm sorry I did that, I'll edit the message asap. Seems no sleep in 26 hours can cause brain not to function as intended :) --------------------------------------------------------------------------- by lsmith77 at 2012-02-01T14:38:52Z FYI the drupal guys are liking this PR (including the flash changes): http://drupal.org/node/335411 --------------------------------------------------------------------------- by drak at 2012-02-01T14:51:33Z @lsmith77 Fabien asked me to remove the changes to the flash messages so that they are as before - i.e. only one flash per name/type /cc @fabpot --------------------------------------------------------------------------- by fabpot at 2012-02-01T14:58:23Z To be clear, I've asked to split this PR in two parts: * one about the session refactoring (which is non-controversial and should be merged ASAP) * this one with only the flash refactoring --------------------------------------------------------------------------- by drak at 2012-02-02T11:29:26Z @fabpot this is ready to be merged now. I will open a separate PR later today for the flash messages as a bucket. --------------------------------------------------------------------------- by fabpot at 2012-02-02T11:34:39Z I must have missed something, but I still see a lot of changes related to the flash messages. --------------------------------------------------------------------------- by drak at 2012-02-02T11:39:10Z When I spoke to you you said you wanted to make the commit with flash messages with one message per name/type rather than multiple. The old flash messages behaviour is 100% maintained in `AutoExpireFlashBag` which can be the default in the framework if you wish. The `FlashBag` implementation makes Symfony2 ESI compatible. --------------------------------------------------------------------------- by stof at 2012-02-02T11:47:38Z @drak splitting into 2 PRs means you should not refactor the flash messages in this one but in the dedicated one. --------------------------------------------------------------------------- by drak at 2012-02-02T12:29:43Z @stof Yes. I discussed with Fabien over chat there are basically no changes to flashes in `FlashBag` and `AutoExpireFlashBag` maintains the exact behaviour as before. The FlashBag just introduces ESI compatible flashes. There is no way to refactor the sessions without moving the flash messages to their own bag. The next PR will propose the changes to flashes that allow multiple messages per name/type. I can size the PR down a little more removing the new storage drivers and so on to make the PR smaller but that's really as far as I can go. To be clear, while the API has changed a little for flashes, the behaviour is the same.
This commit is contained in:
commit
48414000bb
@ -224,6 +224,34 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
|
||||
* removed the ContentTypeMimeTypeGuesser class as it is deprecated and never used on PHP 5.3
|
||||
* added ResponseHeaderBag::makeDisposition() (implements RFC 6266)
|
||||
* made mimetype to extension conversion configurable
|
||||
* [BC BREAK] Moved all session related classes and interfaces into own namespace, as
|
||||
`Symfony\Component\HttpFoudation\Session` and renamed classes accordingly.
|
||||
* Added `FlashBag`. Flashes expire when retrieved by `get()` or `all()`.
|
||||
This makes the implementation ESI compatible.
|
||||
* Added `AutoExpireFlashBag` (default) to replicate Symfony 2.0.x auto expire behaviour of messages auto expiring
|
||||
after one page page load. Messages must be retrived by `get()` or `all()`.
|
||||
* [BC BREAK] Removed the `close()` method from the Session class
|
||||
* Deprecated the following methods from the Session class: `setFlash()`, `setFlashes()`
|
||||
`getFlash()`, `hasFlash()`, and `removeFlash()`. Use `getFlashBag() instead which returns a `FlashBagInterface`.
|
||||
* `Session->clear()` now only clears session attributes as before it cleared flash messages and
|
||||
attributes. `Session->getFlashBag()->all()` clears flashes now.
|
||||
* Added `Symfony\Component\HttpFoundation\Session\Storage\AbstractSessionStorage` base class for
|
||||
session storage drivers.
|
||||
* Added `Symfony\Component\HttpFoundation\Session\Storage\SessionSaveHandlerInterface` interface
|
||||
which storage drivers should implement after inheriting from
|
||||
`Symfony\Component\HttpFoundation\Session\Storage\AbstractSessionStorage` when writing custom session save handlers.
|
||||
* [BC BREAK] `SessionStorageInterface` methods removed: `write()`, `read()` and `remove()`. Added
|
||||
`getBag()`, `registerBag()`.
|
||||
* Moved attribute storage to `Symfony\Component\HttpFoundation\Attribute\AttributeBagInterface`.
|
||||
* Added `Symfony\Component\HttpFoundation\Attribute\AttributeBag` to replicate attributes storage
|
||||
behaviour from 2.0.x (default).
|
||||
* Added `Symfony\Component\HttpFoundation\Attribute\NamespacedAttributeBag` for namespace session attributes.
|
||||
* Session now implements `Symfony\Component\HttpFoundation\Session\SessionInterface` making
|
||||
implementation customizable and portable.
|
||||
* [BC BREAK] Removed `NativeSessionStorage` and replaced with `NativeFileSessionStorage`.
|
||||
* Added session storage drivers for PHP native Memcache, Memcached and SQLite session save handlers.
|
||||
* Added session storage drivers for custom Memcache, Memcached and Null session save handlers.
|
||||
* Removed `FilesystemSessionStorage`, use `MockFileSessionStorage` for functional testing instead.
|
||||
|
||||
### HttpKernel
|
||||
|
||||
|
@ -1,6 +1,8 @@
|
||||
UPGRADE FROM 2.0 to 2.1
|
||||
=======================
|
||||
|
||||
### General
|
||||
|
||||
* assets_base_urls and base_urls merging strategy has changed
|
||||
|
||||
Unlike most configuration blocks, successive values for
|
||||
@ -11,6 +13,8 @@ UPGRADE FROM 2.0 to 2.1
|
||||
and/or share a common base configuration (i.e. ``config.yml``), merging
|
||||
could yield a set of base URL's for multiple environments.
|
||||
|
||||
### [HttpFoundation]
|
||||
|
||||
* moved management of the locale from the Session class to the Request class
|
||||
|
||||
Configuring the default locale:
|
||||
@ -28,17 +32,20 @@ UPGRADE FROM 2.0 to 2.1
|
||||
|
||||
Retrieving the locale from a Twig template:
|
||||
|
||||
Before: `{{ app.request.session.locale }}` or `{{ app.session.locale }}`
|
||||
Before: `{{ app.request.session.locale }}` or `{{ app.session.locale }}`
|
||||
|
||||
After: `{{ app.request.locale }}`
|
||||
|
||||
Retrieving the locale from a PHP template:
|
||||
|
||||
Before: `$view['session']->getLocale()`
|
||||
Before: `$view['session']->getLocale()`
|
||||
|
||||
After: `$view['request']->getLocale()`
|
||||
|
||||
Retrieving the locale from PHP code:
|
||||
|
||||
Before: `$session->getLocale()`
|
||||
Before: `$session->getLocale()`
|
||||
|
||||
After: `$request->getLocale()`
|
||||
|
||||
* Method `equals` of `Symfony\Component\Security\Core\User\UserInterface` has
|
||||
@ -134,7 +141,7 @@ UPGRADE FROM 2.0 to 2.1
|
||||
|
||||
* The strategy for generating the HTML attributes "id" and "name"
|
||||
of choices in a choice field has changed
|
||||
|
||||
|
||||
Instead of appending the choice value, a generated integer is now appended
|
||||
by default. Take care if your Javascript relies on that. If you can
|
||||
guarantee that your choice values only contain ASCII letters, digits,
|
||||
@ -144,7 +151,7 @@ UPGRADE FROM 2.0 to 2.1
|
||||
|
||||
* The strategy for generating the HTML attributes "value" of choices in a
|
||||
choice field has changed
|
||||
|
||||
|
||||
Instead of using the choice value, a generated integer is now stored.
|
||||
Again, take care if your Javascript reads this value. If your choice field
|
||||
is a non-expanded single-choice field, or if the choices are guaranteed not
|
||||
@ -248,3 +255,63 @@ UPGRADE FROM 2.0 to 2.1
|
||||
{
|
||||
return isset($options['widget']) && 'single_text' === $options['widget'] ? 'text' : 'choice';
|
||||
}
|
||||
|
||||
* Flash Messages now returns and array based on type (the old method are still available but deprecated)
|
||||
|
||||
Before (PHP):
|
||||
|
||||
<?php if ($view['session']->hasFlash('notice')): ?>
|
||||
<div class="flash-notice">
|
||||
<?php echo $view['session']->getFlash('notice') ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
After (PHP):
|
||||
|
||||
<?php if ($view['session']->getFlashBag()->has('notice')): ?>
|
||||
<div class="flash-notice">
|
||||
<?php echo $view['session']->getFlashBag()->get('notice') ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
If you wanted to process all flash types you could also make use of the `getFlashBag()->all()` API:
|
||||
|
||||
<?php foreach ($view['session']->getFlashBag()->all() as $type => $flash): ?>
|
||||
<div class="flash-$type">
|
||||
<?php echo $flash; ?>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
|
||||
Before (Twig):
|
||||
|
||||
{% if app.session.hasFlash('notice') %}
|
||||
<div class="flash-notice">
|
||||
{{ app.session.flash('notice') }}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
After (Twig):
|
||||
|
||||
{% if app.session.flashes.has('notice') %}
|
||||
<div class="flash-notice">
|
||||
{{ app.session.flashes.get('notice') }}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
Again you can process all flash messages in one go with
|
||||
|
||||
{% for type, flashMessage in app.session.flashes.all() %}
|
||||
<div class="flash-{{ type }}">
|
||||
{{ flashMessage }}
|
||||
</div>
|
||||
{% endforeach %}
|
||||
|
||||
* Session storage drivers
|
||||
|
||||
Session storage drivers should inherit from
|
||||
`Symfony\Component\HttpFoundation\Session\Storage\AbstractSessionStorage`
|
||||
and no longer should implement `read()`, `write()`, `remove()` which were removed from the
|
||||
`SessionStorageInterface`.
|
||||
|
||||
Any session storage driver that wants to use custom save handlers should
|
||||
implement `Symfony\Component\HttpFoundation\Session\Storage\SaveHandlerInterface`
|
||||
|
@ -3,7 +3,8 @@
|
||||
namespace Symfony\Bridge\Doctrine\HttpFoundation;
|
||||
|
||||
use Doctrine\DBAL\Platforms\MySqlPlatform;
|
||||
use Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\AbstractSessionStorage;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\SessionSaveHandlerInterface;
|
||||
use Doctrine\DBAL\Driver\Connection;
|
||||
|
||||
/**
|
||||
@ -12,39 +13,30 @@ use Doctrine\DBAL\Driver\Connection;
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
class DbalSessionStorage extends NativeSessionStorage
|
||||
class DbalSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface
|
||||
{
|
||||
/**
|
||||
* @var Connection
|
||||
*/
|
||||
private $con;
|
||||
private $tableName;
|
||||
|
||||
public function __construct(Connection $con, $tableName = 'sessions', array $options = array())
|
||||
{
|
||||
parent::__construct($options);
|
||||
|
||||
$this->con = $con;
|
||||
$this->tableName = $tableName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the session.
|
||||
*/
|
||||
public function start()
|
||||
* @var string
|
||||
*/
|
||||
private $tableName;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param Connection $con An instance of Connection.
|
||||
* @param string $tableName Table name.
|
||||
* @param array $options Session configuration options
|
||||
*/
|
||||
public function __construct(Connection $con, $tableName = 'sessions', array $options = array())
|
||||
{
|
||||
if (self::$sessionStarted) {
|
||||
return;
|
||||
}
|
||||
$this->con = $con;
|
||||
$this->tableName = $tableName;
|
||||
|
||||
// use this object as the session handler
|
||||
session_set_save_handler(
|
||||
array($this, 'sessionOpen'),
|
||||
array($this, 'sessionClose'),
|
||||
array($this, 'sessionRead'),
|
||||
array($this, 'sessionWrite'),
|
||||
array($this, 'sessionDestroy'),
|
||||
array($this, 'sessionGC')
|
||||
);
|
||||
|
||||
parent::start();
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -55,7 +47,7 @@ class DbalSessionStorage extends NativeSessionStorage
|
||||
*
|
||||
* @return Boolean true, if the session was opened, otherwise an exception is thrown
|
||||
*/
|
||||
public function sessionOpen($path = null, $name = null)
|
||||
public function openSession($path = null, $name = null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@ -65,7 +57,7 @@ class DbalSessionStorage extends NativeSessionStorage
|
||||
*
|
||||
* @return Boolean true, if the session was closed, otherwise false
|
||||
*/
|
||||
public function sessionClose()
|
||||
public function closeSession()
|
||||
{
|
||||
// do nothing
|
||||
return true;
|
||||
@ -80,7 +72,7 @@ class DbalSessionStorage extends NativeSessionStorage
|
||||
*
|
||||
* @throws \RuntimeException If the session cannot be destroyed
|
||||
*/
|
||||
public function sessionDestroy($id)
|
||||
public function destroySession($id)
|
||||
{
|
||||
try {
|
||||
$this->con->executeQuery("DELETE FROM {$this->tableName} WHERE sess_id = :id", array(
|
||||
@ -102,7 +94,7 @@ class DbalSessionStorage extends NativeSessionStorage
|
||||
*
|
||||
* @throws \RuntimeException If any old sessions cannot be cleaned
|
||||
*/
|
||||
public function sessionGC($lifetime)
|
||||
public function gcSession($lifetime)
|
||||
{
|
||||
try {
|
||||
$this->con->executeQuery("DELETE FROM {$this->tableName} WHERE sess_time < :time", array(
|
||||
@ -124,7 +116,7 @@ class DbalSessionStorage extends NativeSessionStorage
|
||||
*
|
||||
* @throws \RuntimeException If the session cannot be read
|
||||
*/
|
||||
public function sessionRead($id)
|
||||
public function readSession($id)
|
||||
{
|
||||
try {
|
||||
$data = $this->con->executeQuery("SELECT sess_data FROM {$this->tableName} WHERE sess_id = :id", array(
|
||||
@ -140,7 +132,7 @@ class DbalSessionStorage extends NativeSessionStorage
|
||||
|
||||
return '';
|
||||
} catch (\PDOException $e) {
|
||||
throw new \RuntimeException(sprintf('PDOException was thrown when trying to manipulate session data: %s', $e->getMessage()), 0, $e);
|
||||
throw new \RuntimeException(sprintf('PDOException was thrown when trying to read the session data: %s', $e->getMessage()), 0, $e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -154,7 +146,7 @@ class DbalSessionStorage extends NativeSessionStorage
|
||||
*
|
||||
* @throws \RuntimeException If the session data cannot be written
|
||||
*/
|
||||
public function sessionWrite($id, $data)
|
||||
public function writeSession($id, $data)
|
||||
{
|
||||
$platform = $this->con->getDatabasePlatform();
|
||||
|
||||
@ -181,7 +173,7 @@ class DbalSessionStorage extends NativeSessionStorage
|
||||
$this->createNewSession($id, $data);
|
||||
}
|
||||
} catch (\PDOException $e) {
|
||||
throw new \RuntimeException(sprintf('PDOException was thrown when trying to manipulate session data: %s', $e->getMessage()), 0, $e);
|
||||
throw new \RuntimeException(sprintf('PDOException was thrown when trying to write the session data: %s', $e->getMessage()), 0, $e);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -167,7 +167,7 @@ class Configuration implements ConfigurationInterface
|
||||
->canBeUnset()
|
||||
->children()
|
||||
->booleanNode('auto_start')->defaultFalse()->end()
|
||||
->scalarNode('storage_id')->defaultValue('session.storage.native')->end()
|
||||
->scalarNode('storage_id')->defaultValue('session.storage.native_file')->end()
|
||||
->scalarNode('name')->end()
|
||||
->scalarNode('lifetime')->end()
|
||||
->scalarNode('path')->end()
|
||||
|
@ -301,7 +301,7 @@ class FrameworkExtension extends Extension
|
||||
|
||||
$this->addClassesToCompile(array(
|
||||
'Symfony\\Bundle\\FrameworkBundle\\EventListener\\SessionListener',
|
||||
'Symfony\\Component\\HttpFoundation\\SessionStorage\\SessionStorageInterface',
|
||||
'Symfony\\Component\\HttpFoundation\\Session\\Storage\\SessionStorageInterface',
|
||||
$container->getDefinition('session')->getClass(),
|
||||
));
|
||||
|
||||
|
@ -69,7 +69,6 @@ class TestSessionListener implements EventSubscriberInterface
|
||||
|
||||
if ($session = $event->getRequest()->getSession()) {
|
||||
$session->save();
|
||||
$session->close();
|
||||
|
||||
$params = session_get_cookie_params();
|
||||
|
||||
|
@ -5,22 +5,31 @@
|
||||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
|
||||
|
||||
<parameters>
|
||||
<parameter key="session.class">Symfony\Component\HttpFoundation\Session</parameter>
|
||||
<parameter key="session.storage.native.class">Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage</parameter>
|
||||
<parameter key="session.storage.filesystem.class">Symfony\Component\HttpFoundation\SessionStorage\FilesystemSessionStorage</parameter>
|
||||
<parameter key="session.class">Symfony\Component\HttpFoundation\Session\Session</parameter>
|
||||
<parameter key="session.flashbag.class">Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag</parameter>
|
||||
<parameter key="session.attribute_bag.class">Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag</parameter>
|
||||
<parameter key="session.storage.native_file.class">Symfony\Component\HttpFoundation\Session\Storage\NativeFileSessionStorage</parameter>
|
||||
<parameter key="session.storage.mock_file.class">Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage</parameter>
|
||||
<parameter key="session_listener.class">Symfony\Bundle\FrameworkBundle\EventListener\SessionListener</parameter>
|
||||
</parameters>
|
||||
|
||||
<services>
|
||||
<service id="session" class="%session.class%">
|
||||
<argument type="service" id="session.storage" />
|
||||
<argument type="service" id="session.attribute_bag" />
|
||||
<argument type="service" id="session.flash_bag" />
|
||||
</service>
|
||||
|
||||
<service id="session.storage.native" class="%session.storage.native.class%" public="false">
|
||||
<service id="session.flash_bag" class="%session.flashbag.class%" public="false" />
|
||||
|
||||
<service id="session.attribute_bag" class="%session.attribute_bag.class%" public="false" />
|
||||
|
||||
<service id="session.storage.mock_file" class="%session.storage.mock_file.class%" public="false">
|
||||
<argument>%kernel.cache_dir%/sessions</argument>
|
||||
<argument>%session.storage.options%</argument>
|
||||
</service>
|
||||
|
||||
<service id="session.storage.filesystem" class="%session.storage.filesystem.class%" public="false">
|
||||
<service id="session.storage.native_file" class="%session.storage.native_file.class%" public="false">
|
||||
<argument>%kernel.cache_dir%/sessions</argument>
|
||||
<argument>%session.storage.options%</argument>
|
||||
</service>
|
||||
@ -29,5 +38,9 @@
|
||||
<tag name="kernel.event_subscriber" />
|
||||
<argument type="service" id="service_container" />
|
||||
</service>
|
||||
|
||||
<!-- for BC -->
|
||||
<service id="session.storage.native" alias="session.storage.native_file" />
|
||||
<service id="session.storage.filesystem" alias="session.storage.mock_file" />
|
||||
</services>
|
||||
</container>
|
||||
|
@ -79,7 +79,7 @@ class GlobalVariables
|
||||
/**
|
||||
* Returns the current session.
|
||||
*
|
||||
* @return Symfony\Component\HttpFoundation\Session|void The session
|
||||
* @return Symfony\Component\HttpFoundation\Session\Session|void The session
|
||||
*/
|
||||
public function getSession()
|
||||
{
|
||||
|
@ -48,17 +48,17 @@ class SessionHelper extends Helper
|
||||
|
||||
public function getFlash($name, $default = null)
|
||||
{
|
||||
return $this->session->getFlash($name, $default);
|
||||
return $this->session->getFlashBag()->get($name);
|
||||
}
|
||||
|
||||
public function getFlashes()
|
||||
{
|
||||
return $this->session->getFlashes();
|
||||
return $this->session->getFlashBag()->all();
|
||||
}
|
||||
|
||||
public function hasFlash($name)
|
||||
{
|
||||
return $this->session->hasFlash($name);
|
||||
return $this->session->getFlashBag()->has($name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -20,7 +20,7 @@ $container->loadFromExtension('framework', array(
|
||||
),
|
||||
'session' => array(
|
||||
'auto_start' => true,
|
||||
'storage_id' => 'session.storage.native',
|
||||
'storage_id' => 'session.storage.native_file',
|
||||
'name' => '_SYMFONY',
|
||||
'lifetime' => 86400,
|
||||
'path' => '/',
|
||||
|
@ -12,7 +12,7 @@
|
||||
<framework:esi enabled="true" />
|
||||
<framework:profiler only-exceptions="true" />
|
||||
<framework:router resource="%kernel.root_dir%/config/routing.xml" type="xml" />
|
||||
<framework:session auto-start="true" storage-id="session.storage.native" name="_SYMFONY" lifetime="86400" path="/" domain="example.com" secure="true" httponly="true" />
|
||||
<framework:session auto-start="true" storage-id="session.storage.native_file" name="_SYMFONY" lifetime="86400" path="/" domain="example.com" secure="true" httponly="true" />
|
||||
<framework:templating assets-version="SomeVersionScheme" cache="/path/to/cache" >
|
||||
<framework:loader>loader.foo</framework:loader>
|
||||
<framework:loader>loader.bar</framework:loader>
|
||||
|
@ -14,7 +14,7 @@ framework:
|
||||
type: xml
|
||||
session:
|
||||
auto_start: true
|
||||
storage_id: session.storage.native
|
||||
storage_id: session.storage.native_file
|
||||
name: _SYMFONY
|
||||
lifetime: 86400
|
||||
path: /
|
||||
|
@ -78,7 +78,7 @@ abstract class FrameworkExtensionTest extends TestCase
|
||||
$this->assertTrue($container->hasDefinition('session'), '->registerSessionConfiguration() loads session.xml');
|
||||
$this->assertEquals('fr', $container->getParameter('kernel.default_locale'));
|
||||
$this->assertTrue($container->getDefinition('session_listener')->getArgument(1));
|
||||
$this->assertEquals('session.storage.native', (string) $container->getAlias('session.storage'));
|
||||
$this->assertEquals('session.storage.native_file', (string) $container->getAlias('session.storage'));
|
||||
|
||||
$options = $container->getParameter('session.storage.options');
|
||||
$this->assertEquals('_SYMFONY', $options['name']);
|
||||
|
@ -94,7 +94,7 @@ class TestSessionListenerTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
private function getSession()
|
||||
{
|
||||
return $this->getMockBuilder('Symfony\Component\HttpFoundation\Session')
|
||||
return $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ class SessionController extends ContainerAware
|
||||
{
|
||||
$request = $this->container->get('request');
|
||||
$session = $request->getSession();
|
||||
$session->setFlash('notice', $message);
|
||||
$session->getFlashBag()->set('notice', $message);
|
||||
|
||||
return new RedirectResponse($this->container->get('router')->generate('session_showflash'));
|
||||
}
|
||||
@ -61,8 +61,8 @@ class SessionController extends ContainerAware
|
||||
$request = $this->container->get('request');
|
||||
$session = $request->getSession();
|
||||
|
||||
if ($session->hasFlash('notice')) {
|
||||
$output = $session->getFlash('notice');
|
||||
if ($session->getFlashBag()->has('notice')) {
|
||||
$output = $session->getFlashBag()->get('notice');
|
||||
} else {
|
||||
$output = 'No flash was set.';
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ framework:
|
||||
default_locale: en
|
||||
session:
|
||||
auto_start: true
|
||||
storage_id: session.storage.filesystem
|
||||
storage_id: session.storage.mock_file
|
||||
|
||||
services:
|
||||
logger: { class: Symfony\Component\HttpKernel\Log\NullLogger }
|
||||
|
@ -12,8 +12,8 @@
|
||||
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Session;
|
||||
use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
||||
use Symfony\Bundle\FrameworkBundle\Templating\Helper\SessionHelper;
|
||||
|
||||
class SessionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
@ -24,9 +24,9 @@ class SessionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$this->request = new Request();
|
||||
|
||||
$session = new Session(new ArraySessionStorage());
|
||||
$session = new Session(new MockArraySessionStorage());
|
||||
$session->set('foobar', 'bar');
|
||||
$session->setFlash('foo', 'bar');
|
||||
$session->getFlashBag()->set('notice', 'bar');
|
||||
|
||||
$this->request->setSession($session);
|
||||
}
|
||||
@ -40,14 +40,15 @@ class SessionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$helper = new SessionHelper($this->request);
|
||||
|
||||
$this->assertTrue($helper->hasFlash('foo'));
|
||||
$this->assertTrue($helper->hasFlash('notice'));
|
||||
|
||||
$this->assertEquals('bar', $helper->getFlash('foo'));
|
||||
$this->assertEquals('foo', $helper->getFlash('bar', 'foo'));
|
||||
$this->assertEquals('bar', $helper->getFlash('notice'));
|
||||
}
|
||||
|
||||
$this->assertNull($helper->getFlash('foobar'));
|
||||
|
||||
$this->assertEquals(array('foo' => 'bar'), $helper->getFlashes());
|
||||
public function testGetFlashes()
|
||||
{
|
||||
$helper = new SessionHelper($this->request);
|
||||
$this->assertEquals(array('notice' => 'bar'), $helper->getFlashes());
|
||||
}
|
||||
|
||||
public function testGet()
|
||||
|
@ -14,8 +14,8 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\Templating;
|
||||
use Symfony\Bundle\FrameworkBundle\Templating\PhpEngine;
|
||||
use Symfony\Component\DependencyInjection\Container;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Session;
|
||||
use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
||||
use Symfony\Component\Templating\TemplateNameParser;
|
||||
use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables;
|
||||
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
|
||||
@ -64,7 +64,7 @@ class PhpEngineTest extends TestCase
|
||||
{
|
||||
$container = new Container();
|
||||
$request = new Request();
|
||||
$session = new Session(new ArraySessionStorage());
|
||||
$session = new Session(new MockArraySessionStorage());
|
||||
|
||||
$request->setSession($session);
|
||||
$container->set('request', $request);
|
||||
|
@ -10,7 +10,7 @@ framework:
|
||||
default_locale: en
|
||||
session:
|
||||
auto_start: true
|
||||
storage_id: session.storage.filesystem
|
||||
storage_id: session.storage.mock_file
|
||||
|
||||
services:
|
||||
logger: { class: Symfony\Component\HttpKernel\Log\NullLogger }
|
||||
|
@ -14,8 +14,8 @@ namespace Symfony\Bundle\TwigBundle\Tests;
|
||||
use Symfony\Bundle\TwigBundle\TwigEngine;
|
||||
use Symfony\Component\DependencyInjection\Container;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Session;
|
||||
use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
||||
use Symfony\Component\Templating\TemplateNameParser;
|
||||
use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables;
|
||||
|
||||
@ -71,7 +71,7 @@ class TwigEngineTest extends TestCase
|
||||
{
|
||||
$container = new Container();
|
||||
$request = new Request();
|
||||
$session = new Session(new ArraySessionStorage());
|
||||
$session = new Session(new MockArraySessionStorage());
|
||||
|
||||
$request->setSession($session);
|
||||
$container->set('request', $request);
|
||||
|
@ -15,6 +15,7 @@ use Symfony\Component\DependencyInjection\ContainerAware;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag;
|
||||
|
||||
/**
|
||||
* ProfilerController.
|
||||
@ -146,9 +147,9 @@ class ProfilerController extends ContainerAware
|
||||
{
|
||||
$request = $this->container->get('request');
|
||||
|
||||
if (null !== $session = $request->getSession()) {
|
||||
// keep current flashes for one more request
|
||||
$session->setFlashes($session->getFlashes());
|
||||
if (null !== $session = $request->getSession() && $session->getFlashBag() instanceof AutoExpireFlashBag) {
|
||||
// keep current flashes for one more request if using AutoExpireFlashBag
|
||||
$session->getFlashBag()->setAll($session->getFlashBag()->peekAll());
|
||||
}
|
||||
|
||||
if (null === $token) {
|
||||
|
@ -15,6 +15,7 @@ use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
|
||||
use Symfony\Bundle\TwigBundle\TwigEngine;
|
||||
use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag;
|
||||
|
||||
/**
|
||||
* WebDebugToolbarListener injects the Web Debug Toolbar.
|
||||
@ -70,9 +71,10 @@ class WebDebugToolbarListener
|
||||
}
|
||||
|
||||
if ($response->headers->has('X-Debug-Token') && $response->isRedirect() && $this->interceptRedirects) {
|
||||
if (null !== $session = $request->getSession()) {
|
||||
// keep current flashes for one more request
|
||||
$session->setFlashes($session->getFlashes());
|
||||
$session = $request->getSession();
|
||||
if ($session->getFlashBag() instanceof AutoExpireFlashBag) {
|
||||
// keep current flashes for one more request if using AutoExpireFlashBag
|
||||
$session->getFlashBag()->setAll($session->getFlashBag()->peekAll());
|
||||
}
|
||||
|
||||
$response->setContent($this->templating->render('WebProfilerBundle:Profiler:toolbar_redirect.html.twig', array('location' => $response->headers->get('Location'))));
|
||||
|
@ -177,7 +177,7 @@ class WebDebugToolbarListenerTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
protected function getRequestMock($isXmlHttpRequest = false, $requestFormat = 'html')
|
||||
{
|
||||
$session = $this->getMock('Symfony\Component\HttpFoundation\Session', array(), array(), '', false);
|
||||
$session = $this->getMock('Symfony\Component\HttpFoundation\Session\Session', array(), array(), '', false);
|
||||
$request = $this->getMock(
|
||||
'Symfony\Component\HttpFoundation\Request',
|
||||
array('getSession', 'isXmlHttpRequest', 'getRequestFormat'),
|
||||
|
@ -11,7 +11,7 @@
|
||||
|
||||
namespace Symfony\Component\Form\Extension\Csrf\CsrfProvider;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
|
||||
/**
|
||||
* This provider uses a Symfony2 Session object to retrieve the user's
|
||||
|
@ -11,6 +11,8 @@
|
||||
|
||||
namespace Symfony\Component\HttpFoundation;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
|
||||
/**
|
||||
* Request represents an HTTP request.
|
||||
*
|
||||
@ -122,7 +124,7 @@ class Request
|
||||
protected $format;
|
||||
|
||||
/**
|
||||
* @var \Symfony\Component\HttpFoundation\Session
|
||||
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface
|
||||
*/
|
||||
protected $session;
|
||||
|
||||
@ -466,7 +468,7 @@ class Request
|
||||
/**
|
||||
* Gets the Session.
|
||||
*
|
||||
* @return Session|null The session
|
||||
* @return SessionInterface|null The session
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
@ -504,11 +506,11 @@ class Request
|
||||
/**
|
||||
* Sets the Session.
|
||||
*
|
||||
* @param Session $session The Session
|
||||
* @param SessionInterface $session The Session
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function setSession(Session $session)
|
||||
public function setSession(SessionInterface $session)
|
||||
{
|
||||
$this->session = $session;
|
||||
}
|
||||
|
@ -1,358 +0,0 @@
|
||||
<?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;
|
||||
|
||||
use Symfony\Component\HttpFoundation\SessionStorage\SessionStorageInterface;
|
||||
|
||||
/**
|
||||
* Session.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
class Session implements \Serializable
|
||||
{
|
||||
protected $storage;
|
||||
protected $started;
|
||||
protected $attributes;
|
||||
protected $flashes;
|
||||
protected $oldFlashes;
|
||||
protected $closed;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param SessionStorageInterface $storage A SessionStorageInterface instance
|
||||
*/
|
||||
public function __construct(SessionStorageInterface $storage)
|
||||
{
|
||||
$this->storage = $storage;
|
||||
$this->flashes = array();
|
||||
$this->oldFlashes = array();
|
||||
$this->attributes = array();
|
||||
$this->started = false;
|
||||
$this->closed = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the session storage.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function start()
|
||||
{
|
||||
if (true === $this->started) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->storage->start();
|
||||
|
||||
$attributes = $this->storage->read('_symfony2');
|
||||
|
||||
if (isset($attributes['attributes'])) {
|
||||
$this->attributes = $attributes['attributes'];
|
||||
$this->flashes = $attributes['flashes'];
|
||||
|
||||
// flag current flash messages to be removed at shutdown
|
||||
$this->oldFlashes = $this->flashes;
|
||||
}
|
||||
|
||||
$this->started = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if an attribute is defined.
|
||||
*
|
||||
* @param string $name The attribute name
|
||||
*
|
||||
* @return Boolean true if the attribute is defined, false otherwise
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
return array_key_exists($name, $this->attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an attribute.
|
||||
*
|
||||
* @param string $name The attribute name
|
||||
* @param mixed $default The default value
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function get($name, $default = null)
|
||||
{
|
||||
return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an attribute.
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function set($name, $value)
|
||||
{
|
||||
if (false === $this->started) {
|
||||
$this->start();
|
||||
}
|
||||
|
||||
$this->attributes[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns attributes.
|
||||
*
|
||||
* @return array Attributes
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
return $this->attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets attributes.
|
||||
*
|
||||
* @param array $attributes Attributes
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function replace(array $attributes)
|
||||
{
|
||||
if (false === $this->started) {
|
||||
$this->start();
|
||||
}
|
||||
|
||||
$this->attributes = $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes an attribute.
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function remove($name)
|
||||
{
|
||||
if (false === $this->started) {
|
||||
$this->start();
|
||||
}
|
||||
|
||||
if (array_key_exists($name, $this->attributes)) {
|
||||
unset($this->attributes[$name]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears all attributes.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
if (false === $this->started) {
|
||||
$this->start();
|
||||
}
|
||||
|
||||
$this->attributes = array();
|
||||
$this->flashes = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidates the current session.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function invalidate()
|
||||
{
|
||||
$this->clear();
|
||||
$this->storage->regenerate(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrates the current session to a new session id while maintaining all
|
||||
* session attributes.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function migrate()
|
||||
{
|
||||
$this->storage->regenerate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the session ID
|
||||
*
|
||||
* @return mixed The session ID
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
if (false === $this->started) {
|
||||
$this->start();
|
||||
}
|
||||
|
||||
return $this->storage->getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the flash messages.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getFlashes()
|
||||
{
|
||||
return $this->flashes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the flash messages.
|
||||
*
|
||||
* @param array $values
|
||||
*/
|
||||
public function setFlashes($values)
|
||||
{
|
||||
if (false === $this->started) {
|
||||
$this->start();
|
||||
}
|
||||
|
||||
$this->flashes = $values;
|
||||
$this->oldFlashes = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a flash message.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string|null $default
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFlash($name, $default = null)
|
||||
{
|
||||
return array_key_exists($name, $this->flashes) ? $this->flashes[$name] : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a flash message.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
*/
|
||||
public function setFlash($name, $value)
|
||||
{
|
||||
if (false === $this->started) {
|
||||
$this->start();
|
||||
}
|
||||
|
||||
$this->flashes[$name] = $value;
|
||||
unset($this->oldFlashes[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a flash message exists.
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return Boolean
|
||||
*/
|
||||
public function hasFlash($name)
|
||||
{
|
||||
if (false === $this->started) {
|
||||
$this->start();
|
||||
}
|
||||
|
||||
return array_key_exists($name, $this->flashes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a flash message.
|
||||
*
|
||||
* @param string $name
|
||||
*/
|
||||
public function removeFlash($name)
|
||||
{
|
||||
if (false === $this->started) {
|
||||
$this->start();
|
||||
}
|
||||
|
||||
unset($this->flashes[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the flash messages.
|
||||
*/
|
||||
public function clearFlashes()
|
||||
{
|
||||
if (false === $this->started) {
|
||||
$this->start();
|
||||
}
|
||||
|
||||
$this->flashes = array();
|
||||
$this->oldFlashes = array();
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
if (false === $this->started) {
|
||||
$this->start();
|
||||
}
|
||||
|
||||
$this->flashes = array_diff_key($this->flashes, $this->oldFlashes);
|
||||
|
||||
$this->storage->write('_symfony2', array(
|
||||
'attributes' => $this->attributes,
|
||||
'flashes' => $this->flashes,
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* This method should be called when you don't want the session to be saved
|
||||
* when the Session object is garbaged collected (useful for instance when
|
||||
* you want to simulate the interaction of several users/sessions in a single
|
||||
* PHP process).
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
$this->closed = true;
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
if (true === $this->started && !$this->closed) {
|
||||
$this->save();
|
||||
}
|
||||
}
|
||||
|
||||
public function serialize()
|
||||
{
|
||||
return serialize($this->storage);
|
||||
}
|
||||
|
||||
public function unserialize($serialized)
|
||||
{
|
||||
$this->storage = unserialize($serialized);
|
||||
$this->attributes = array();
|
||||
$this->started = false;
|
||||
}
|
||||
}
|
@ -0,0 +1,137 @@
|
||||
<?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\Attribute;
|
||||
|
||||
/**
|
||||
* This class relates to session attribute storage
|
||||
*/
|
||||
class AttributeBag implements AttributeBagInterface
|
||||
{
|
||||
private $name = 'attributes';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $storageKey;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $attributes = array();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param type $storageKey The key used to store flashes in the session.
|
||||
*/
|
||||
public function __construct($storageKey = '_sf2_attributes')
|
||||
{
|
||||
$this->storageKey = $storageKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function initialize(array &$attributes)
|
||||
{
|
||||
$this->attributes = &$attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getStorageKey()
|
||||
{
|
||||
return $this->storageKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
return array_key_exists($name, $this->attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function get($name, $default = null)
|
||||
{
|
||||
return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function set($name, $value)
|
||||
{
|
||||
$this->attributes[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
return $this->attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function replace(array $attributes)
|
||||
{
|
||||
$this->attributes = array();
|
||||
foreach ($attributes as $key => $value) {
|
||||
$this->set($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function remove($name)
|
||||
{
|
||||
$retval = null;
|
||||
if (array_key_exists($name, $this->attributes)) {
|
||||
$retval = $this->attributes[$name];
|
||||
unset($this->attributes[$name]);
|
||||
}
|
||||
|
||||
return $retval;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
$return = $this->attributes;
|
||||
$this->attributes = array();
|
||||
|
||||
return $return;
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
<?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\Attribute;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
|
||||
|
||||
/**
|
||||
* Attributes store.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
interface AttributeBagInterface extends SessionBagInterface
|
||||
{
|
||||
/**
|
||||
* Checks if an attribute is defined.
|
||||
*
|
||||
* @param string $name The attribute name
|
||||
*
|
||||
* @return Boolean true if the attribute is defined, false otherwise
|
||||
*/
|
||||
function has($name);
|
||||
|
||||
/**
|
||||
* Returns an attribute.
|
||||
*
|
||||
* @param string $name The attribute name
|
||||
* @param mixed $default The default value if not found.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
function get($name, $default = null);
|
||||
|
||||
/**
|
||||
* Sets an attribute.
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
*/
|
||||
function set($name, $value);
|
||||
|
||||
/**
|
||||
* Returns attributes.
|
||||
*
|
||||
* @return array Attributes
|
||||
*/
|
||||
function all();
|
||||
|
||||
/**
|
||||
* Sets attributes.
|
||||
*
|
||||
* @param array $attributes Attributes
|
||||
*/
|
||||
function replace(array $attributes);
|
||||
|
||||
/**
|
||||
* Removes an attribute.
|
||||
*
|
||||
* @param string $name
|
||||
*/
|
||||
function remove($name);
|
||||
}
|
@ -0,0 +1,184 @@
|
||||
<?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\Attribute;
|
||||
|
||||
/**
|
||||
* This class provides structured storage of session attributes using
|
||||
* a name spacing character in the key.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class NamespacedAttributeBag extends AttributeBag
|
||||
{
|
||||
/**
|
||||
* Namespace character.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $namespaceCharacter;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param type $storageKey Session storage key.
|
||||
* @param type $namespaceCharacter Namespace character to use in keys.
|
||||
*/
|
||||
public function __construct($storageKey = '_sf2_attributes', $namespaceCharacter = '/')
|
||||
{
|
||||
$this->namespaceCharacter = $namespaceCharacter;
|
||||
parent::__construct($storageKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
$attributes = $this->resolveAttributePath($name);
|
||||
$name = $this->resolveKey($name);
|
||||
|
||||
return array_key_exists($name, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function get($name, $default = null)
|
||||
{
|
||||
$attributes = $this->resolveAttributePath($name);
|
||||
$name = $this->resolveKey($name);
|
||||
|
||||
return array_key_exists($name, $attributes) ? $attributes[$name] : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function set($name, $value)
|
||||
{
|
||||
$attributes = & $this->resolveAttributePath($name, true);
|
||||
$name = $this->resolveKey($name);
|
||||
$attributes[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
return $this->attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function replace(array $attributes)
|
||||
{
|
||||
$this->attributes = array();
|
||||
foreach ($attributes as $key => $value) {
|
||||
$this->set($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function remove($name)
|
||||
{
|
||||
$retval = null;
|
||||
$attributes = & $this->resolveAttributePath($name);
|
||||
$name = $this->resolveKey($name);
|
||||
if (array_key_exists($name, $attributes)) {
|
||||
$retval = $attributes[$name];
|
||||
unset($attributes[$name]);
|
||||
}
|
||||
|
||||
return $retval;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
$return = $this->attributes;
|
||||
$this->attributes = array();
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a path in attributes property and returns it as a reference.
|
||||
*
|
||||
* This method allows structured namespacing of session attributes.
|
||||
*
|
||||
* @param string $name Key name
|
||||
* @param boolean $writeContext Write context, default false
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function &resolveAttributePath($name, $writeContext = false)
|
||||
{
|
||||
$array = & $this->attributes;
|
||||
$name = (strpos($name, $this->namespaceCharacter) === 0) ? substr($name, 1) : $name;
|
||||
|
||||
// Check if there is anything to do, else return
|
||||
if (!$name) {
|
||||
return $array;
|
||||
}
|
||||
|
||||
$parts = explode($this->namespaceCharacter, $name);
|
||||
if (count($parts) < 2) {
|
||||
if (!$writeContext) {
|
||||
return $array;
|
||||
}
|
||||
|
||||
$array[$parts[0]] = array();
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
unset($parts[count($parts)-1]);
|
||||
|
||||
foreach ($parts as $part) {
|
||||
if (!array_key_exists($part, $array)) {
|
||||
if (!$writeContext) {
|
||||
return $array;
|
||||
}
|
||||
|
||||
$array[$part] = array();
|
||||
}
|
||||
|
||||
$array = & $array[$part];
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the key from the name.
|
||||
*
|
||||
* This is the last part in a dot separated string.
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function resolveKey($name)
|
||||
{
|
||||
if (strpos($name, $this->namespaceCharacter) !== false) {
|
||||
$name = substr($name, strrpos($name, $this->namespaceCharacter)+1, strlen($name));
|
||||
}
|
||||
|
||||
return $name;
|
||||
}
|
||||
}
|
@ -0,0 +1,174 @@
|
||||
<?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\Flash;
|
||||
|
||||
/**
|
||||
* AutoExpireFlashBag flash message container.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class AutoExpireFlashBag implements FlashBagInterface
|
||||
{
|
||||
private $name = 'flashes';
|
||||
|
||||
/**
|
||||
* Flash messages.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $flashes = array();
|
||||
|
||||
/**
|
||||
* The storage key for flashes in the session
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $storageKey;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param type $storageKey The key used to store flashes in the session.
|
||||
*/
|
||||
public function __construct($storageKey = '_sf2_flashes')
|
||||
{
|
||||
$this->storageKey = $storageKey;
|
||||
$this->flashes = array('display' => array(), 'new' => array());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function initialize(array &$flashes)
|
||||
{
|
||||
$this->flashes = &$flashes;
|
||||
|
||||
// The logic: messages from the last request will be stored in new, so we move them to previous
|
||||
// This request we will show what is in 'display'. What is placed into 'new' this time round will
|
||||
// be moved to display next time round.
|
||||
$this->flashes['display'] = array_key_exists('new', $this->flashes) ? $this->flashes['new'] : array();
|
||||
$this->flashes['new'] = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function peek($type, $default = null)
|
||||
{
|
||||
return $this->has($type) ? $this->flashes['display'][$type] : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function peekAll()
|
||||
{
|
||||
return array_key_exists('display', $this->flashes) ? (array)$this->flashes['display'] : array();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function get($type, $default = null)
|
||||
{
|
||||
if (!$this->has($type)) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
$return = null;
|
||||
if (isset($this->flashes['new'][$type])) {
|
||||
unset($this->flashes['new'][$type]);
|
||||
}
|
||||
|
||||
if (isset($this->flashes['display'][$type])) {
|
||||
$return = $this->flashes['display'][$type];
|
||||
unset($this->flashes['display'][$type]);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
$return = $this->flashes['display'];
|
||||
$this->flashes = array('new' => array(), 'display' => array());
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setAll(array $messages)
|
||||
{
|
||||
$this->flashes['new'] = $messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function set($type, $message)
|
||||
{
|
||||
$this->flashes['new'][$type] = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function has($type)
|
||||
{
|
||||
return array_key_exists($type, $this->flashes['display']);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function keys()
|
||||
{
|
||||
return array_keys($this->flashes['display']);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getStorageKey()
|
||||
{
|
||||
return $this->storageKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
$return = $this->all();
|
||||
$this->flashes = array('display' => array(), 'new' => array());
|
||||
|
||||
return $return;
|
||||
}
|
||||
}
|
158
src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php
Normal file
158
src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php
Normal file
@ -0,0 +1,158 @@
|
||||
<?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\Flash;
|
||||
|
||||
/**
|
||||
* FlashBag flash message container.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class FlashBag implements FlashBagInterface
|
||||
{
|
||||
private $name = 'flashes';
|
||||
|
||||
/**
|
||||
* Flash messages.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $flashes = array();
|
||||
|
||||
/**
|
||||
* The storage key for flashes in the session
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $storageKey;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param type $storageKey The key used to store flashes in the session.
|
||||
*/
|
||||
public function __construct($storageKey = '_sf2_flashes')
|
||||
{
|
||||
$this->storageKey = $storageKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function initialize(array &$flashes)
|
||||
{
|
||||
$this->flashes = &$flashes;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function peek($type, $default = null)
|
||||
{
|
||||
return $this->has($type) ? $this->flashes[$type] : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function peekAll()
|
||||
{
|
||||
return $this->flashes;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function get($type, $default = null)
|
||||
{
|
||||
if (!$this->has($type)) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
$return = $this->flashes[$type];
|
||||
|
||||
unset($this->flashes[$type]);
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
$return = $this->peekAll();
|
||||
$this->flashes = array();
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function set($type, $message)
|
||||
{
|
||||
$this->flashes[$type] = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setAll(array $messages)
|
||||
{
|
||||
$this->flashes = $messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function has($type)
|
||||
{
|
||||
return array_key_exists($type, $this->flashes);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function keys()
|
||||
{
|
||||
return array_keys($this->flashes);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getStorageKey()
|
||||
{
|
||||
return $this->storageKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
return $this->all();
|
||||
}
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
<?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\Flash;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
|
||||
|
||||
/**
|
||||
* FlashBagInterface.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
interface FlashBagInterface extends SessionBagInterface
|
||||
{
|
||||
/**
|
||||
* Registers a message for a given type.
|
||||
*
|
||||
* @param string $type
|
||||
* @param string $message
|
||||
*/
|
||||
function set($type, $message);
|
||||
|
||||
/**
|
||||
* Gets flash message for a given type.
|
||||
*
|
||||
* @param string $type Message category type.
|
||||
* @param string $default Default value if $type doee not exist.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function peek($type, $default = null);
|
||||
|
||||
/**
|
||||
* Gets all flash messages.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function peekAll();
|
||||
|
||||
/**
|
||||
* Gets and clears flash from the stack.
|
||||
*
|
||||
* @param string $type
|
||||
* @param string $default Default value if $type doee not exist.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function get($type, $default = null);
|
||||
|
||||
/**
|
||||
* Gets and clears flashes from the stack.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function all();
|
||||
|
||||
/**
|
||||
* Sets all flash messages.
|
||||
*/
|
||||
function setAll(array $messages);
|
||||
|
||||
/**
|
||||
* Has flash messages for a given type?
|
||||
*
|
||||
* @param string $type
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
function has($type);
|
||||
|
||||
/**
|
||||
* Returns a list of all defined types.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function keys();
|
||||
}
|
318
src/Symfony/Component/HttpFoundation/Session/Session.php
Normal file
318
src/Symfony/Component/HttpFoundation/Session/Session.php
Normal file
@ -0,0 +1,318 @@
|
||||
<?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;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
|
||||
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
|
||||
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
|
||||
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
|
||||
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
|
||||
|
||||
/**
|
||||
* Session.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Drak <drak@zikula.org>
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
class Session implements SessionInterface
|
||||
{
|
||||
/**
|
||||
* Storage driver.
|
||||
*
|
||||
* @var SessionStorageInterface
|
||||
*/
|
||||
protected $storage;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param SessionStorageInterface $storage A SessionStorageInterface instance.
|
||||
* @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
|
||||
* @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for default FlashBag)
|
||||
*/
|
||||
public function __construct(SessionStorageInterface $storage, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
|
||||
{
|
||||
$this->storage = $storage;
|
||||
$this->registerBag($attributes ?: new AttributeBag());
|
||||
$this->registerBag($flashes ?: new FlashBag());
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the session storage.
|
||||
*
|
||||
* @return boolean True if session started.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function start()
|
||||
{
|
||||
return $this->storage->start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if an attribute is defined.
|
||||
*
|
||||
* @param string $name The attribute name
|
||||
*
|
||||
* @return Boolean true if the attribute is defined, false otherwise
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
return $this->storage->getBag('attributes')->has($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an attribute.
|
||||
*
|
||||
* @param string $name The attribute name
|
||||
* @param mixed $default The default value
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function get($name, $default = null)
|
||||
{
|
||||
return $this->storage->getBag('attributes')->get($name, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an attribute.
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function set($name, $value)
|
||||
{
|
||||
$this->storage->getBag('attributes')->set($name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns attributes.
|
||||
*
|
||||
* @return array Attributes
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
return $this->storage->getBag('attributes')->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets attributes.
|
||||
*
|
||||
* @param array $attributes Attributes
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function replace(array $attributes)
|
||||
{
|
||||
$this->storage->getBag('attributes')->replace($attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes an attribute.
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function remove($name)
|
||||
{
|
||||
return $this->storage->getBag('attributes')->remove($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears all attributes.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
$this->storage->getBag('attributes')->clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidates the current session.
|
||||
*
|
||||
* Clears all session attributes and flashes and regenerates the
|
||||
* session and deletes the old session from persistence.
|
||||
*
|
||||
* @return boolean True if session invalidated, false if error.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function invalidate()
|
||||
{
|
||||
$this->storage->clear();
|
||||
|
||||
return $this->storage->regenerate(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrates the current session to a new session id while maintaining all
|
||||
* session attributes.
|
||||
*
|
||||
* @param boolean $destroy Whether to delete the old session or leave it to garbage collection.
|
||||
*
|
||||
* @return boolean True if session migrated, false if error
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function migrate($destroy = false)
|
||||
{
|
||||
return $this->storage->regenerate($destroy);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
$this->storage->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the session ID
|
||||
*
|
||||
* @return mixed The session ID
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->storage->getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements the \Serialize interface.
|
||||
*
|
||||
* @return SessionStorageInterface
|
||||
*/
|
||||
public function serialize()
|
||||
{
|
||||
return serialize($this->storage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements the \Serialize interface.
|
||||
*
|
||||
* @throws \InvalidArgumentException If the passed string does not unserialize to an instance of SessionStorageInterface
|
||||
*/
|
||||
public function unserialize($serialized)
|
||||
{
|
||||
$storage = unserialize($serialized);
|
||||
if (!$storage instanceof SessionStorageInterface) {
|
||||
throw new \InvalidArgumentException('Serialized data did not return a valid instance of SessionStorageInterface');
|
||||
}
|
||||
|
||||
$this->storage = $storage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a SessionBagInterface with the sessio.
|
||||
*
|
||||
* @param SessionBagInterface $bag
|
||||
*/
|
||||
public function registerBag(SessionBagInterface $bag)
|
||||
{
|
||||
$this->storage->registerBag($bag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get's a bag instance.
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return SessionBagInterface
|
||||
*/
|
||||
public function getBag($name)
|
||||
{
|
||||
return $this->storage->getBag($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the flashbag interface.
|
||||
*
|
||||
* @return FlashBagInterface
|
||||
*/
|
||||
public function getFlashBag()
|
||||
{
|
||||
return $this->getBag('flashes');
|
||||
}
|
||||
|
||||
// the following methods are kept for compatibility with Symfony 2.0 (they will be removed for Symfony 2.3)
|
||||
|
||||
/**
|
||||
* @deprecated since 2.1, will be removed from 2.3
|
||||
*/
|
||||
public function getFlashes()
|
||||
{
|
||||
return $this->getBag('flashes')->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated since 2.1, will be removed from 2.3
|
||||
*/
|
||||
public function setFlashes($values)
|
||||
{
|
||||
$this->getBag('flashes')->setAll($values);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated since 2.1, will be removed from 2.3
|
||||
*/
|
||||
public function getFlash($name, $default = null)
|
||||
{
|
||||
return $this->getBag('flashes')->get($name, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated since 2.1, will be removed from 2.3
|
||||
*/
|
||||
public function setFlash($name, $value)
|
||||
{
|
||||
$this->getBag('flashes')->set($name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated since 2.1, will be removed from 2.3
|
||||
*/
|
||||
public function hasFlash($name)
|
||||
{
|
||||
return $this->getBag('flashes')->has($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated since 2.1, will be removed from 2.3
|
||||
*/
|
||||
public function removeFlash($name)
|
||||
{
|
||||
$this->getBag('flashes')->get($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated since 2.1, will be removed from 2.3
|
||||
*/
|
||||
public function clearFlashes()
|
||||
{
|
||||
return $this->getBag('flashes')->clear();
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* Session Bag store.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
interface SessionBagInterface
|
||||
{
|
||||
/**
|
||||
* Gets this bag's name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function getName();
|
||||
|
||||
/**
|
||||
* Initializes the Bag
|
||||
*
|
||||
* @param array $array
|
||||
*/
|
||||
function initialize(array &$array);
|
||||
|
||||
/**
|
||||
* Gets the storage key for this bag.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function getStorageKey();
|
||||
|
||||
/**
|
||||
* Clears out data from bag.
|
||||
*
|
||||
* @return mixed Whatever data was contained.
|
||||
*/
|
||||
function clear();
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* Interface for the session.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
interface SessionInterface extends \Serializable
|
||||
{
|
||||
/**
|
||||
* Starts the session storage.
|
||||
*
|
||||
* @throws \RuntimeException If session fails to start.
|
||||
*/
|
||||
function start();
|
||||
|
||||
/**
|
||||
* Invalidates the current session.
|
||||
*
|
||||
* @return boolean True if session invalidated, false if error.
|
||||
*/
|
||||
function invalidate();
|
||||
|
||||
/**
|
||||
* Migrates the current session to a new session id while maintaining all
|
||||
* session attributes.
|
||||
*
|
||||
* @param boolean $destroy Whether to delete the old session or leave it to garbage collection.
|
||||
*
|
||||
* @return boolean True if session migrated, false if error.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
function migrate($destroy = false);
|
||||
|
||||
/**
|
||||
* Force the session to be saved and closed.
|
||||
*
|
||||
* This method is generally not required for real sessions as
|
||||
* the session will be automatically saved at the end of
|
||||
* code execution.
|
||||
*/
|
||||
function save();
|
||||
|
||||
/**
|
||||
* Checks if an attribute is defined.
|
||||
*
|
||||
* @param string $name The attribute name
|
||||
*
|
||||
* @return Boolean true if the attribute is defined, false otherwise
|
||||
*/
|
||||
function has($name);
|
||||
|
||||
/**
|
||||
* Returns an attribute.
|
||||
*
|
||||
* @param string $name The attribute name
|
||||
* @param mixed $default The default value if not found.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
function get($name, $default = null);
|
||||
|
||||
/**
|
||||
* Sets an attribute.
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
*/
|
||||
function set($name, $value);
|
||||
|
||||
/**
|
||||
* Returns attributes.
|
||||
*
|
||||
* @return array Attributes
|
||||
*/
|
||||
function all();
|
||||
|
||||
/**
|
||||
* Sets attributes.
|
||||
*
|
||||
* @param array $attributes Attributes
|
||||
*/
|
||||
function replace(array $attributes);
|
||||
|
||||
/**
|
||||
* Removes an attribute.
|
||||
*
|
||||
* @param string $name
|
||||
*/
|
||||
function remove($name);
|
||||
|
||||
/**
|
||||
* Clears all attributes.
|
||||
*/
|
||||
function clear();
|
||||
}
|
@ -0,0 +1,339 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* This provides a base class for session attribute storage.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
abstract class AbstractSessionStorage implements SessionStorageInterface
|
||||
{
|
||||
/**
|
||||
* Array of SessionBagInterface
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $bags;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $options = array();
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $started = false;
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $closed = false;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* Depending on how you want the storage driver to behave you probably
|
||||
* want top override this constructor entirely.
|
||||
*
|
||||
* List of options for $options array with their defaults.
|
||||
* @see http://www.php.net/manual/en/session.configuration.php for options
|
||||
* but we omit 'session.' from the beginning of the keys.
|
||||
*
|
||||
* auto_start, "0"
|
||||
* cookie_domain, ""
|
||||
* cookie_httponly, ""
|
||||
* cookie_lifetime, "0"
|
||||
* cookie_path, "/"
|
||||
* cookie_secure, ""
|
||||
* entropy_file, ""
|
||||
* entropy_length, "0"
|
||||
* gc_divisor, "100"
|
||||
* gc_maxlifetime, "1440"
|
||||
* gc_probability, "1"
|
||||
* hash_bits_per_character, "4"
|
||||
* hash_function, "0"
|
||||
* name, "PHPSESSID"
|
||||
* referer_check, ""
|
||||
* save_path, ""
|
||||
* serialize_handler, "php"
|
||||
* use_cookies, "1"
|
||||
* use_only_cookies, "1"
|
||||
* use_trans_sid, "0"
|
||||
* upload_progress.enabled, "1"
|
||||
* upload_progress.cleanup, "1"
|
||||
* upload_progress.prefix, "upload_progress_"
|
||||
* upload_progress.name, "PHP_SESSION_UPLOAD_PROGRESS"
|
||||
* upload_progress.freq, "1%"
|
||||
* upload_progress.min-freq, "1"
|
||||
* url_rewriter.tags, "a=href,area=href,frame=src,form=,fieldset="
|
||||
*
|
||||
* @param array $options Session configuration options.
|
||||
*/
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
$this->setOptions($options);
|
||||
$this->registerSaveHandlers();
|
||||
$this->registerShutdownFunction();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function start()
|
||||
{
|
||||
if ($this->started && !$this->closed) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->options['use_cookies'] && headers_sent()) {
|
||||
throw new \RuntimeException('Failed to start the session because header have already been sent.');
|
||||
}
|
||||
|
||||
// start the session
|
||||
if (!session_start()) {
|
||||
throw new \RuntimeException('Failed to start the session');
|
||||
}
|
||||
|
||||
$this->loadSession();
|
||||
|
||||
$this->started = true;
|
||||
$this->closed = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
if (!$this->started) {
|
||||
return ''; // returning empty is consistent with session_id() behaviour
|
||||
}
|
||||
|
||||
return session_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* Regenerates the session.
|
||||
*
|
||||
* This method will regenerate the session ID and optionally
|
||||
* destroy the old ID. Session regeneration should be done
|
||||
* periodically and for example, should be done when converting
|
||||
* an anonymous session to a logged in user session.
|
||||
*
|
||||
* @param boolean $destroy
|
||||
*
|
||||
* @return boolean Returns true on success or false on failure.
|
||||
*/
|
||||
public function regenerate($destroy = false)
|
||||
{
|
||||
return session_regenerate_id($destroy);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
session_write_close();
|
||||
$this->closed = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
// clear out the bags
|
||||
foreach ($this->bags as $bag) {
|
||||
$bag->clear();
|
||||
}
|
||||
|
||||
// clear out the session
|
||||
$_SESSION = array();
|
||||
|
||||
// reconnect the bags to the session
|
||||
$this->loadSession();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a SessionBagInterface for use.
|
||||
*
|
||||
* @param SessionBagInterface $bag
|
||||
*/
|
||||
public function registerBag(SessionBagInterface $bag)
|
||||
{
|
||||
$this->bags[$bag->getName()] = $bag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a bag by name.
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return SessionBagInterface
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function getBag($name)
|
||||
{
|
||||
if (!isset($this->bags[$name])) {
|
||||
throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name));
|
||||
}
|
||||
|
||||
if ($this->options['auto_start'] && !$this->started) {
|
||||
$this->start();
|
||||
}
|
||||
|
||||
return $this->bags[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets session.* ini variables.
|
||||
*
|
||||
* For convenience we omit 'session.' from the beginning of the keys.
|
||||
* Explicitly ignores other ini keys.
|
||||
*
|
||||
* session_get_cookie_params() overrides values.
|
||||
*
|
||||
* @param array $options
|
||||
*
|
||||
* @see http://www.php.net/manual/en/session.configuration.php
|
||||
*/
|
||||
protected function setOptions(array $options)
|
||||
{
|
||||
$cookieDefaults = session_get_cookie_params();
|
||||
$this->options = array_merge(array(
|
||||
'cookie_lifetime' => $cookieDefaults['lifetime'],
|
||||
'cookie_path' => $cookieDefaults['path'],
|
||||
'cookie_domain' => $cookieDefaults['domain'],
|
||||
'cookie_secure' => $cookieDefaults['secure'],
|
||||
'cookie_httponly' => isset($cookieDefaults['httponly']) ? $cookieDefaults['httponly'] : false,
|
||||
), $options);
|
||||
|
||||
// Unless session.cache_limiter has been set explicitly, disable it
|
||||
// because this is managed by HeaderBag directly (if used).
|
||||
if (!isset($this->options['cache_limiter'])) {
|
||||
$this->options['cache_limiter'] = 0;
|
||||
}
|
||||
|
||||
if (!isset($this->options['auto_start'])) {
|
||||
$this->options['auto_start'] = 0;
|
||||
}
|
||||
|
||||
if (!isset($this->options['use_cookies'])) {
|
||||
$this->options['use_cookies'] = 1;
|
||||
}
|
||||
|
||||
foreach ($this->options as $key => $value) {
|
||||
if (in_array($key, array(
|
||||
'auto_start', 'cookie_domain', 'cookie_httponly',
|
||||
'cookie_lifetime', 'cookie_path', 'cookie_secure',
|
||||
'entropy_file', 'entropy_length', 'gc_divisor',
|
||||
'gc_maxlifetime', 'gc_probability', 'hash_bits_per_character',
|
||||
'hash_function', 'name', 'referer_check',
|
||||
'save_path', 'serialize_handler', 'use_cookies',
|
||||
'use_only_cookies', 'use_trans_sid', 'upload_progress.enabled',
|
||||
'upload_progress.cleanup', 'upload_progress.prefix', 'upload_progress.name',
|
||||
'upload_progress.freq', 'upload_progress.min-freq', 'url_rewriter.tags'))) {
|
||||
ini_set('session.'.$key, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers this storage device for PHP session handling.
|
||||
*
|
||||
* PHP requires session save handlers to be set, either it's own, or custom ones.
|
||||
* There are some defaults set automatically when PHP starts, but these can be overriden
|
||||
* using this command if you need anything other than PHP's default handling.
|
||||
*
|
||||
* When the session starts, PHP will call the sessionRead() handler which should return an array
|
||||
* of any session attributes. PHP will then populate these into $_SESSION.
|
||||
*
|
||||
* When PHP shuts down, the sessionWrite() handler is called and will pass the $_SESSION contents
|
||||
* to be stored.
|
||||
*
|
||||
* When a session is specifically destroyed, PHP will call the sessionDestroy() handler with the
|
||||
* session ID. This happens when the session is regenerated for example and th handler
|
||||
* MUST delete the session by ID from the persistent storage immediately.
|
||||
*
|
||||
* PHP will call sessionGc() from time to time to expire any session records according to the
|
||||
* set max lifetime of a session. This routine should delete all records from persistent
|
||||
* storage which were last accessed longer than the $lifetime.
|
||||
*
|
||||
* PHP sessionOpen() and sessionClose() are pretty much redundant and can just return true.
|
||||
*
|
||||
* NOTE:
|
||||
*
|
||||
* To use PHP native save handlers, override this method using ini_set with
|
||||
* session.save_handlers and session.save_path e.g.
|
||||
*
|
||||
* ini_set('session.save_handlers', 'files');
|
||||
* ini_set('session.save_path', /tmp');
|
||||
*
|
||||
* @see http://php.net/manual/en/function.session-set-save-handler.php
|
||||
* @see SessionSaveHandlerInterface
|
||||
*/
|
||||
protected function registerSaveHandlers()
|
||||
{
|
||||
// note this can be reset to PHP's control using ini_set('session.save_handler', 'files');
|
||||
// so long as ini_set() is called before the session is started.
|
||||
if ($this instanceof SessionSaveHandlerInterface) {
|
||||
session_set_save_handler(
|
||||
array($this, 'openSession'),
|
||||
array($this, 'closeSession'),
|
||||
array($this, 'readSession'),
|
||||
array($this, 'writeSession'),
|
||||
array($this, 'destroySession'),
|
||||
array($this, 'gcSession')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers PHP shutdown function.
|
||||
*
|
||||
* This method is required to avoid strange issues when using PHP objects as
|
||||
* session save handlers.
|
||||
*/
|
||||
protected function registerShutdownFunction()
|
||||
{
|
||||
register_shutdown_function('session_write_close');
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the session with attributes.
|
||||
*
|
||||
* After starting the session, PHP retrieves the session from whatever handlers
|
||||
* are set to (either PHP's internal, custom set with session_set_save_handler()).
|
||||
* PHP takes the return value from the sessionRead() handler, unserializes it
|
||||
* and populates $_SESSION with the result automatically.
|
||||
*/
|
||||
protected function loadSession(array &$session = null)
|
||||
{
|
||||
if (null === $session) {
|
||||
$session = &$_SESSION;
|
||||
}
|
||||
|
||||
foreach ($this->bags as $bag) {
|
||||
$key = $bag->getStorageKey();
|
||||
$session[$key] = isset($session[$key]) ? $session[$key] : array();
|
||||
$bag->initialize($session[$key]);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,136 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* MemcacheSessionStorage.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface
|
||||
{
|
||||
/**
|
||||
* Memcache driver.
|
||||
*
|
||||
* @var Memcache
|
||||
*/
|
||||
private $memcache;
|
||||
|
||||
/**
|
||||
* Configuration options.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $memcacheOptions;
|
||||
|
||||
/**
|
||||
* Key prefix for shared environments.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $prefix;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param \Memcache $memcache A \Memcache instance
|
||||
* @param array $memcacheOptions An associative array of Memcachge options
|
||||
* @param array $options Session configuration options.
|
||||
*
|
||||
* @see AbstractSessionStorage::__construct()
|
||||
*/
|
||||
public function __construct(\Memcache $memcache, array $memcacheOptions = array(), array $options = array())
|
||||
{
|
||||
$this->memcache = $memcache;
|
||||
|
||||
// defaults
|
||||
if (!isset($memcacheOptions['serverpool'])) {
|
||||
$memcacheOptions['serverpool'] = array(
|
||||
'host' => '127.0.0.1',
|
||||
'port' => 11211,
|
||||
'timeout' => 1,
|
||||
'persistent' => false,
|
||||
'weight' => 1);
|
||||
}
|
||||
|
||||
$memcacheOptions['expiretime'] = isset($memcacheOptions['expiretime']) ? (int)$memcacheOptions['expiretime'] : 86400;
|
||||
$this->prefix = isset($memcachedOptions['prefix']) ? $memcachedOptions['prefix'] : 'sf2s';
|
||||
|
||||
$this->memcacheOptions = $memcacheOptions;
|
||||
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
protected function addServer(array $server)
|
||||
{
|
||||
if (array_key_exists('host', $server)) {
|
||||
throw new \InvalidArgumentException('host key must be set');
|
||||
}
|
||||
$server['port'] = isset($server['port']) ? (int)$server['port'] : 11211;
|
||||
$server['timeout'] = isset($server['timeout']) ? (int)$server['timeout'] : 1;
|
||||
$server['presistent'] = isset($server['presistent']) ? (bool)$server['presistent'] : false;
|
||||
$server['weight'] = isset($server['weight']) ? (bool)$server['weight'] : 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function openSession($savePath, $sessionName)
|
||||
{
|
||||
foreach ($this->memcacheOptions['serverpool'] as $server) {
|
||||
$this->addServer($server);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function closeSession()
|
||||
{
|
||||
return $this->memcache->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function readSession($sessionId)
|
||||
{
|
||||
return $this->memcache->get($this->prefix.$sessionId) ?: '';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function writeSession($sessionId, $data)
|
||||
{
|
||||
return $this->memcache->set($this->prefix.$sessionId, $data, $this->memcacheOptions['expiretime']);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function destroySession($sessionId)
|
||||
{
|
||||
return $this->memcache->delete($this->prefix.$sessionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function gcSession($lifetime)
|
||||
{
|
||||
// not required here because memcache will auto expire the records anyhow.
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,137 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* MemcachedSessionStorage.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class MemcachedSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface
|
||||
{
|
||||
/**
|
||||
* Memcached driver.
|
||||
*
|
||||
* @var Memcached
|
||||
*/
|
||||
private $memcached;
|
||||
|
||||
/**
|
||||
* Configuration options.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $memcachedOptions;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param \Memcached $memcached A \Memcached instance
|
||||
* @param array $memcachedOptions An associative array of Memcached options
|
||||
* @param array $options Session configuration options.
|
||||
*
|
||||
* @see AbstractSessionStorage::__construct()
|
||||
*/
|
||||
public function __construct(\Memcached $memcache, array $memcachedOptions = array(), array $options = array())
|
||||
{
|
||||
$this->memcached = $memcached;
|
||||
|
||||
// defaults
|
||||
if (!isset($memcachedOptions['serverpool'])) {
|
||||
$memcachedOptions['serverpool'] = array(
|
||||
'host' => '127.0.0.1',
|
||||
'port' => 11211,
|
||||
'timeout' => 1,
|
||||
'persistent' => false,
|
||||
'weight' => 1);
|
||||
}
|
||||
|
||||
$memcachedOptions['expiretime'] = isset($memcachedOptions['expiretime']) ? (int)$memcachedOptions['expiretime'] : 86400;
|
||||
|
||||
$this->memcached->setOption(\Memcached::OPT_PREFIX_KEY, isset($memcachedOptions['prefix']) ? $memcachedOption['prefix'] : 'sf2s');
|
||||
|
||||
$this->memcacheOptions = $memcachedOptions;
|
||||
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function openSession($savePath, $sessionName)
|
||||
{
|
||||
foreach ($this->memcachedOptions['serverpool'] as $server) {
|
||||
$this->addServer($server);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close session.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function closeSession()
|
||||
{
|
||||
return $this->memcached->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function readSession($sessionId)
|
||||
{
|
||||
return $this->memcached->get($this->prefix.$sessionId) ?: '';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function writeSession($sessionId, $data)
|
||||
{
|
||||
return $this->memcached->set($this->prefix.$sessionId, $data, false, $this->memcachedOptions['expiretime']);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function destroySession($sessionId)
|
||||
{
|
||||
return $this->memcached->delete($this->prefix.$sessionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function gcSession($lifetime)
|
||||
{
|
||||
// not required here because memcached will auto expire the records anyhow.
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a server to the memcached handler.
|
||||
*
|
||||
* @param array $server
|
||||
*/
|
||||
protected function addServer(array $server)
|
||||
{
|
||||
if (array_key_exists('host', $server)) {
|
||||
throw new \InvalidArgumentException('host key must be set');
|
||||
}
|
||||
$server['port'] = isset($server['port']) ? (int)$server['port'] : 11211;
|
||||
$server['timeout'] = isset($server['timeout']) ? (int)$server['timeout'] : 1;
|
||||
$server['presistent'] = isset($server['presistent']) ? (bool)$server['presistent'] : false;
|
||||
$server['weight'] = isset($server['weight']) ? (bool)$server['weight'] : 1;
|
||||
}
|
||||
}
|
@ -0,0 +1,124 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* MockArraySessionStorage mocks the session for unit tests.
|
||||
*
|
||||
* No PHP session is actually started since a session can be initialized
|
||||
* and shutdown only once per PHP execution cycle.
|
||||
*
|
||||
* When doing functional testing, you should use MockFileSessionStorage instead.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class MockArraySessionStorage extends AbstractSessionStorage
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $sessionId;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $sessionData = array();
|
||||
|
||||
public function setSessionData(array $array)
|
||||
{
|
||||
$this->sessionData = $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function start()
|
||||
{
|
||||
if ($this->started && !$this->closed) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->started = true;
|
||||
$this->loadSession($this->sessionData);
|
||||
|
||||
$this->sessionId = $this->generateSessionId();
|
||||
session_id($this->sessionId);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function regenerate($destroy = false)
|
||||
{
|
||||
if ($this->options['auto_start'] && !$this->started) {
|
||||
$this->start();
|
||||
}
|
||||
|
||||
$this->sessionId = $this->generateSessionId();
|
||||
session_id($this->sessionId);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
if (!$this->started) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $this->sessionId;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
// nothing to do since we don't persist the session data
|
||||
$this->closed = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
// clear out the bags
|
||||
foreach ($this->bags as $bag) {
|
||||
$bag->clear();
|
||||
}
|
||||
|
||||
// clear out the session
|
||||
$this->sessionData = array();
|
||||
|
||||
// reconnect the bags to the session
|
||||
$this->loadSession($this->sessionData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a session ID.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function generateSessionId()
|
||||
{
|
||||
return sha1(uniqid(mt_rand(), true));
|
||||
}
|
||||
}
|
@ -0,0 +1,136 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* MockFileSessionStorage is used to mock sessions for
|
||||
* functional testing when done in a single PHP process.
|
||||
*
|
||||
* No PHP session is actually started since a session can be initialized
|
||||
* and shutdown only once per PHP execution cycle.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class MockFileSessionStorage extends MockArraySessionStorage
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $savePath;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $savePath Path of directory to save session files.
|
||||
* @param array $options Session options.
|
||||
*
|
||||
* @see AbstractSessionStorage::__construct()
|
||||
*/
|
||||
public function __construct($savePath = null, array $options = array())
|
||||
{
|
||||
if (null === $savePath) {
|
||||
$savePath = sys_get_temp_dir();
|
||||
}
|
||||
|
||||
if (!is_dir($savePath)) {
|
||||
mkdir($savePath, 0777, true);
|
||||
}
|
||||
|
||||
$this->savePath = $savePath;
|
||||
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function start()
|
||||
{
|
||||
if ($this->started) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!session_id()) {
|
||||
session_id($this->generateSessionId());
|
||||
}
|
||||
|
||||
$this->sessionId = session_id();
|
||||
|
||||
$this->read();
|
||||
|
||||
$this->started = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function regenerate($destroy = false)
|
||||
{
|
||||
if ($destroy) {
|
||||
$this->destroy();
|
||||
}
|
||||
|
||||
session_id($this->generateSessionId());
|
||||
$this->sessionId = session_id();
|
||||
|
||||
$this->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
if (!$this->started) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $this->sessionId;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
file_put_contents($this->getFilePath(), serialize($this->sessionData));
|
||||
}
|
||||
|
||||
private function destroy()
|
||||
{
|
||||
if (is_file($this->getFilePath())) {
|
||||
unlink($this->getFilePath());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate path to file.
|
||||
*
|
||||
* @return string File path
|
||||
*/
|
||||
public function getFilePath()
|
||||
{
|
||||
return $this->savePath.'/'.$this->sessionId.'.sess';
|
||||
}
|
||||
|
||||
private function read()
|
||||
{
|
||||
$filePath = $this->getFilePath();
|
||||
$this->sessionData = is_readable($filePath) && is_file($filePath) ? unserialize(file_get_contents($filePath)) : array();
|
||||
|
||||
$this->loadSession($this->sessionData);
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* NativeFileSessionStorage.
|
||||
*
|
||||
* Native session handler using PHP's built in file storage.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class NativeFileSessionStorage extends AbstractSessionStorage
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $savePath;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $savePath Path of directory to save session files.
|
||||
* @param array $options Session configuration options.
|
||||
*
|
||||
* @see AbstractSessionStorage::__construct()
|
||||
*/
|
||||
public function __construct($savePath = null, array $options = array())
|
||||
{
|
||||
if (null === $savePath) {
|
||||
$savePath = sys_get_temp_dir();
|
||||
}
|
||||
|
||||
if (!is_dir($savePath)) {
|
||||
mkdir($savePath, 0777, true);
|
||||
}
|
||||
|
||||
$this->savePath = $savePath;
|
||||
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function registerSaveHandlers()
|
||||
{
|
||||
ini_set('session.save_handler', 'files');
|
||||
ini_set('session.save_path', $this->savePath);
|
||||
}
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* NativeMemcacheSessionStorage.
|
||||
*
|
||||
* Session based on native PHP memcache database handler.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class NativeMemcacheSessionStorage extends AbstractSessionStorage
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $savePath;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $savePath Path of memcache server.
|
||||
* @param array $options Session configuration options.
|
||||
*
|
||||
* @see AbstractSessionStorage::__construct()
|
||||
*/
|
||||
public function __construct($savePath = 'tcp://127.0.0.1:11211?persistent=0', array $options = array())
|
||||
{
|
||||
if (!extension_loaded('memcache')) {
|
||||
throw new \RuntimeException('PHP does not have "memcache" session module registered');
|
||||
}
|
||||
|
||||
$this->savePath = $savePath;
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function registerSaveHandlers()
|
||||
{
|
||||
ini_set('session.save_handler', 'memcache');
|
||||
ini_set('session.save_path', $this->savePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Sets any values memcached ini values.
|
||||
*
|
||||
* @see http://www.php.net/manual/en/memcache.ini.php
|
||||
*/
|
||||
protected function setOptions(array $options)
|
||||
{
|
||||
foreach ($options as $key => $value) {
|
||||
if (in_array($key, array(
|
||||
'memcache.allow_failover', 'memcache.max_failover_attempts',
|
||||
'memcache.chunk_size', 'memcache.default_port', 'memcache.hash_strategy',
|
||||
'memcache.hash_function', 'memcache.protocol', 'memcache.redundancy',
|
||||
'memcache.session_redundancy', 'memcache.compress_threshold',
|
||||
'memcache.lock_timeout'))) {
|
||||
ini_set($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
parent::setOptions($options);
|
||||
}
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* NativeMemcachedSessionStorage.
|
||||
*
|
||||
* Session based on native PHP memcached database handler.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class NativeMemcachedSessionStorage extends AbstractSessionStorage
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $savePath;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $savePath Comma separated list of servers: e.g. memcache1.example.com:11211,memcache2.example.com:11211
|
||||
* @param array $options Session configuration options.
|
||||
*
|
||||
* @see AbstractSessionStorage::__construct()
|
||||
*/
|
||||
public function __construct($savePath = '127.0.0.1:11211', array $options = array())
|
||||
{
|
||||
if (!extension_loaded('memcached')) {
|
||||
throw new \RuntimeException('PHP does not have "memcached" session module registered');
|
||||
}
|
||||
|
||||
$this->savePath = $savePath;
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function registerSaveHandlers()
|
||||
{
|
||||
ini_set('session.save_handler', 'memcached');
|
||||
ini_set('session.save_path', $this->savePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Sets any values memcached ini values.
|
||||
*
|
||||
* @see https://github.com/php-memcached-dev/php-memcached/blob/master/memcached.ini
|
||||
*/
|
||||
protected function setOptions(array $options)
|
||||
{
|
||||
foreach ($options as $key => $value) {
|
||||
if (in_array($key, array(
|
||||
'memcached.sess_locking', 'memcached.sess_lock_wait',
|
||||
'memcached.sess_prefix', 'memcached.compression_type',
|
||||
'memcached.compression_factor', 'memcached.compression_threshold',
|
||||
'memcached.serializer'))) {
|
||||
ini_set($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
parent::setOptions($options);
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* NativeSqliteSessionStorage.
|
||||
*
|
||||
* Session based on native PHP sqlite database handler.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class NativeSqliteSessionStorage extends AbstractSessionStorage
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $dbPath;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $dbPath Path to SQLite database file.
|
||||
* @param array $options Session configuration options.
|
||||
*
|
||||
* @see AbstractSessionStorage::__construct()
|
||||
*/
|
||||
public function __construct($dbPath, array $options = array())
|
||||
{
|
||||
if (!extension_loaded('sqlite')) {
|
||||
throw new \RuntimeException('PHP does not have "sqlite" session module registered');
|
||||
}
|
||||
|
||||
$this->dbPath = $dbPath;
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function registerSaveHandlers()
|
||||
{
|
||||
ini_set('session.save_handler', 'sqlite');
|
||||
ini_set('session.save_path', $this->dbPath);
|
||||
}
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* NullSessionStorage.
|
||||
*
|
||||
* Can be used in unit testing or in a sitation where persisted sessions are not desired.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
class NullSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function openSession($savePath, $sessionName)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close session.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function closeSession()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function readSession($sessionId)
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function writeSession($sessionId, $data)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function destroySession($sessionId)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function gcSession($lifetime)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\HttpFoundation\SessionStorage;
|
||||
namespace Symfony\Component\HttpFoundation\Session\Storage;
|
||||
|
||||
/**
|
||||
* PdoSessionStorage.
|
||||
@ -17,18 +17,19 @@ namespace Symfony\Component\HttpFoundation\SessionStorage;
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Michael Williams <michael.williams@funsational.com>
|
||||
*/
|
||||
class PdoSessionStorage extends NativeSessionStorage
|
||||
class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface
|
||||
{
|
||||
/**
|
||||
* PDO instance.
|
||||
*
|
||||
* @var \PDO
|
||||
*/
|
||||
private $db;
|
||||
private $pdo;
|
||||
|
||||
/**
|
||||
* Database options.
|
||||
*
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $dbOptions;
|
||||
@ -36,21 +37,22 @@ class PdoSessionStorage extends NativeSessionStorage
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param \PDO $db A PDO instance
|
||||
* @param array $options An associative array of session options
|
||||
* @param array $dbOptions An associative array of DB options
|
||||
*
|
||||
* @param \PDO $pdo A \PDO instance
|
||||
* @param array $dbOptions An associative array of DB options
|
||||
* @param array $options Session configuration options
|
||||
*
|
||||
* @throws \InvalidArgumentException When "db_table" option is not provided
|
||||
*
|
||||
* @see NativeSessionStorage::__construct()
|
||||
* @see AbstractSessionStorage::__construct()
|
||||
*/
|
||||
public function __construct(\PDO $db, array $options = array(), array $dbOptions = array())
|
||||
public function __construct(\PDO $pdo, array $dbOptions = array(), array $options = array())
|
||||
{
|
||||
if (!array_key_exists('db_table', $dbOptions)) {
|
||||
throw new \InvalidArgumentException('You must provide the "db_table" option for a PdoSessionStorage.');
|
||||
}
|
||||
|
||||
$this->db = $db;
|
||||
$this->pdo = $pdo;
|
||||
$this->dbOptions = array_merge(array(
|
||||
'db_id_col' => 'sess_id',
|
||||
'db_data_col' => 'sess_data',
|
||||
@ -61,61 +63,27 @@ class PdoSessionStorage extends NativeSessionStorage
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the session.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function start()
|
||||
{
|
||||
if (self::$sessionStarted) {
|
||||
return;
|
||||
}
|
||||
|
||||
// use this object as the session handler
|
||||
session_set_save_handler(
|
||||
array($this, 'sessionOpen'),
|
||||
array($this, 'sessionClose'),
|
||||
array($this, 'sessionRead'),
|
||||
array($this, 'sessionWrite'),
|
||||
array($this, 'sessionDestroy'),
|
||||
array($this, 'sessionGC')
|
||||
);
|
||||
|
||||
parent::start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a session.
|
||||
*
|
||||
* @param string $path (ignored)
|
||||
* @param string $name (ignored)
|
||||
*
|
||||
* @return Boolean true, if the session was opened, otherwise an exception is thrown
|
||||
*/
|
||||
public function sessionOpen($path = null, $name = null)
|
||||
public function openSession($path = null, $name = null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes a session.
|
||||
*
|
||||
* @return Boolean true, if the session was closed, otherwise false
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function sessionClose()
|
||||
public function closeSession()
|
||||
{
|
||||
// do nothing
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys a session.
|
||||
*
|
||||
* @param string $id A session ID
|
||||
*
|
||||
* @return Boolean true, if the session was destroyed, otherwise an exception is thrown
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws \RuntimeException If the session cannot be destroyed
|
||||
*/
|
||||
public function sessionDestroy($id)
|
||||
public function destroySession($id)
|
||||
{
|
||||
// get table/column
|
||||
$dbTable = $this->dbOptions['db_table'];
|
||||
@ -125,7 +93,7 @@ class PdoSessionStorage extends NativeSessionStorage
|
||||
$sql = "DELETE FROM $dbTable WHERE $dbIdCol = :id";
|
||||
|
||||
try {
|
||||
$stmt = $this->db->prepare($sql);
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
$stmt->bindParam(':id', $id, \PDO::PARAM_STR);
|
||||
$stmt->execute();
|
||||
} catch (\PDOException $e) {
|
||||
@ -136,15 +104,11 @@ class PdoSessionStorage extends NativeSessionStorage
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleans up old sessions.
|
||||
*
|
||||
* @param int $lifetime The lifetime of a session
|
||||
*
|
||||
* @return Boolean true, if old sessions have been cleaned, otherwise an exception is thrown
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws \RuntimeException If any old sessions cannot be cleaned
|
||||
*/
|
||||
public function sessionGC($lifetime)
|
||||
public function gcSession($lifetime)
|
||||
{
|
||||
// get table/column
|
||||
$dbTable = $this->dbOptions['db_table'];
|
||||
@ -154,7 +118,7 @@ class PdoSessionStorage extends NativeSessionStorage
|
||||
$sql = "DELETE FROM $dbTable WHERE $dbTimeCol < (:time - $lifetime)";
|
||||
|
||||
try {
|
||||
$stmt = $this->db->prepare($sql);
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
$stmt->bindValue(':time', time(), \PDO::PARAM_INT);
|
||||
$stmt->execute();
|
||||
} catch (\PDOException $e) {
|
||||
@ -165,15 +129,11 @@ class PdoSessionStorage extends NativeSessionStorage
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a session.
|
||||
*
|
||||
* @param string $id A session ID
|
||||
*
|
||||
* @return string The session data if the session was read or created, otherwise an exception is thrown
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws \RuntimeException If the session cannot be read
|
||||
*/
|
||||
public function sessionRead($id)
|
||||
public function readSession($id)
|
||||
{
|
||||
// get table/columns
|
||||
$dbTable = $this->dbOptions['db_table'];
|
||||
@ -183,7 +143,7 @@ class PdoSessionStorage extends NativeSessionStorage
|
||||
try {
|
||||
$sql = "SELECT $dbDataCol FROM $dbTable WHERE $dbIdCol = :id";
|
||||
|
||||
$stmt = $this->db->prepare($sql);
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
$stmt->bindParam(':id', $id, \PDO::PARAM_STR, 255);
|
||||
|
||||
$stmt->execute();
|
||||
@ -200,21 +160,16 @@ class PdoSessionStorage extends NativeSessionStorage
|
||||
|
||||
return '';
|
||||
} catch (\PDOException $e) {
|
||||
throw new \RuntimeException(sprintf('PDOException was thrown when trying to manipulate session data: %s', $e->getMessage()), 0, $e);
|
||||
throw new \RuntimeException(sprintf('PDOException was thrown when trying to read the session data: %s', $e->getMessage()), 0, $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes session data.
|
||||
*
|
||||
* @param string $id A session ID
|
||||
* @param string $data A serialized chunk of session data
|
||||
*
|
||||
* @return Boolean true, if the session was written, otherwise an exception is thrown
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws \RuntimeException If the session data cannot be written
|
||||
*/
|
||||
public function sessionWrite($id, $data)
|
||||
public function writeSession($id, $data)
|
||||
{
|
||||
// get table/column
|
||||
$dbTable = $this->dbOptions['db_table'];
|
||||
@ -222,7 +177,7 @@ class PdoSessionStorage extends NativeSessionStorage
|
||||
$dbIdCol = $this->dbOptions['db_id_col'];
|
||||
$dbTimeCol = $this->dbOptions['db_time_col'];
|
||||
|
||||
$sql = ('mysql' === $this->db->getAttribute(\PDO::ATTR_DRIVER_NAME))
|
||||
$sql = ('mysql' === $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME))
|
||||
? "INSERT INTO $dbTable ($dbIdCol, $dbDataCol, $dbTimeCol) VALUES (:id, :data, :time) "
|
||||
."ON DUPLICATE KEY UPDATE $dbDataCol = VALUES($dbDataCol), $dbTimeCol = CASE WHEN $dbTimeCol = :time THEN (VALUES($dbTimeCol) + 1) ELSE VALUES($dbTimeCol) END"
|
||||
: "UPDATE $dbTable SET $dbDataCol = :data, $dbTimeCol = :time WHERE $dbIdCol = :id";
|
||||
@ -230,7 +185,7 @@ class PdoSessionStorage extends NativeSessionStorage
|
||||
try {
|
||||
//session data can contain non binary safe characters so we need to encode it
|
||||
$encoded = base64_encode($data);
|
||||
$stmt = $this->db->prepare($sql);
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
$stmt->bindParam(':id', $id, \PDO::PARAM_STR);
|
||||
$stmt->bindParam(':data', $encoded, \PDO::PARAM_STR);
|
||||
$stmt->bindValue(':time', time(), \PDO::PARAM_INT);
|
||||
@ -242,7 +197,7 @@ class PdoSessionStorage extends NativeSessionStorage
|
||||
$this->createNewSession($id, $data);
|
||||
}
|
||||
} catch (\PDOException $e) {
|
||||
throw new \RuntimeException(sprintf('PDOException was thrown when trying to manipulate session data: %s', $e->getMessage()), 0, $e);
|
||||
throw new \RuntimeException(sprintf('PDOException was thrown when trying to write the session data: %s', $e->getMessage()), 0, $e);
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -268,7 +223,7 @@ class PdoSessionStorage extends NativeSessionStorage
|
||||
|
||||
//session data can contain non binary safe characters so we need to encode it
|
||||
$encoded = base64_encode($data);
|
||||
$stmt = $this->db->prepare($sql);
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
$stmt->bindParam(':id', $id, \PDO::PARAM_STR);
|
||||
$stmt->bindParam(':data', $encoded, \PDO::PARAM_STR);
|
||||
$stmt->bindValue(':time', time(), \PDO::PARAM_INT);
|
@ -0,0 +1,157 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* Session Savehandler Interface.
|
||||
*
|
||||
* This interface is for implementing methods required for the
|
||||
* session_set_save_handler() function.
|
||||
*
|
||||
* @see http://php.net/session_set_save_handler
|
||||
*
|
||||
* These are methods called by PHP when the session is started
|
||||
* and closed and for various house-keeping tasks required
|
||||
* by session management.
|
||||
*
|
||||
* PHP requires session save handlers. There are some defaults set
|
||||
* automatically when PHP starts, but these can be overriden using
|
||||
* this command if you need anything other than PHP's default handling.
|
||||
*
|
||||
* When the session starts, PHP will call the readSession() handler
|
||||
* which should return a string extactly as stored (which will have
|
||||
* been encoded by PHP using a special session serializer session_decode()
|
||||
* which is different to the serialize() function. PHP will then populate
|
||||
* these into $_SESSION.
|
||||
*
|
||||
* When PHP shuts down, the sessionWrite() handler is called and will pass
|
||||
* the $_SESSION contents already serialized (using session_encode()) to
|
||||
* be stored.
|
||||
*
|
||||
* When a session is specifically destroyed, PHP will call the
|
||||
* sessionSession() handler with the session ID. This happens when the
|
||||
* session is regenerated for example and th handler MUST delete the
|
||||
* session by ID from the persistent storage immediately.
|
||||
*
|
||||
* PHP will call sessionSession() from time to time to expire any session
|
||||
* records according to the set max lifetime of a session. This routine
|
||||
* should delete all records from persistent storage which were last
|
||||
* accessed longer than the $lifetime.
|
||||
*
|
||||
* PHP openSession() and closeSession() are pretty much redundant and
|
||||
* can return true.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
interface SessionSaveHandlerInterface
|
||||
{
|
||||
/**
|
||||
* Open session.
|
||||
*
|
||||
* This method is for internal use by PHP and must not be called manually.
|
||||
*
|
||||
* @param string $savePath Save path.
|
||||
* @param string $sessionName Session Name.
|
||||
*
|
||||
* @throws \RuntimeException If something goes wrong starting the session.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
function openSession($savePath, $sessionName);
|
||||
|
||||
/**
|
||||
* Close session.
|
||||
*
|
||||
* This method is for internal use by PHP and must not be called manually.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
function closeSession();
|
||||
|
||||
/**
|
||||
* Read session.
|
||||
*
|
||||
* This method is for internal use by PHP and must not be called manually.
|
||||
*
|
||||
* This method is called by PHP itself when the session is started.
|
||||
* This method should retrieve the session data from storage by the
|
||||
* ID provided by PHP. Return the string directly as is from storage.
|
||||
* If the record was not found you must return an empty string.
|
||||
*
|
||||
* The returned data will be unserialized automatically by PHP using a
|
||||
* special unserializer method session_decode() and the result will be used
|
||||
* to populate the $_SESSION superglobal. This is done automatically and
|
||||
* is not configurable.
|
||||
*
|
||||
* @param string $sessionId Session ID.
|
||||
*
|
||||
* @throws \RuntimeException On fatal error but not "record not found".
|
||||
*
|
||||
* @return string String as stored in persistent storage or empty string in all other cases.
|
||||
*/
|
||||
function readSession($sessionId);
|
||||
|
||||
/**
|
||||
* Commit session to storage.
|
||||
*
|
||||
* This method is for internal use by PHP and must not be called manually.
|
||||
*
|
||||
* PHP will call this method when the session is closed. It sends
|
||||
* the session ID and the contents of $_SESSION to be saved in a lightweight
|
||||
* serialized format (which PHP does automatically using session_encode()
|
||||
* which should be stored exactly as is given in $data.
|
||||
*
|
||||
* Note this method is normally called by PHP after the output buffers
|
||||
* have been closed.
|
||||
*
|
||||
* @param string $sessionId Session ID.
|
||||
* @param string $data Session serialized data to save.
|
||||
*
|
||||
* @throws \RuntimeException On fatal error.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
function writeSession($sessionId, $data);
|
||||
|
||||
/**
|
||||
* Destroys this session.
|
||||
*
|
||||
* This method is for internal use by PHP and must not be called manually.
|
||||
*
|
||||
* PHP will call this method when the session data associated
|
||||
* with the session ID provided needs to be immediately
|
||||
* deleted from the permanent storage.
|
||||
*
|
||||
* @param string $sessionId Session ID.
|
||||
*
|
||||
* @throws \RuntimeException On fatal error.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
function destroySession($sessionId);
|
||||
|
||||
/**
|
||||
* Garbage collection for storage.
|
||||
*
|
||||
* This method is for internal use by PHP and must not be called manually.
|
||||
*
|
||||
* This method is called by PHP periodically and passes the maximum
|
||||
* time a session can exist for before being deleted from permanent storage.
|
||||
*
|
||||
* @param integer $lifetime Max lifetime in seconds to keep sessions stored.
|
||||
*
|
||||
* @throws \RuntimeException On fatal error.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
function gcSession($lifetime);
|
||||
}
|
@ -0,0 +1,92 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* StorageInterface.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Drak <drak@zikula.org>
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
interface SessionStorageInterface
|
||||
{
|
||||
/**
|
||||
* Starts the session.
|
||||
*
|
||||
* @throws \RuntimeException If something goes wrong starting the session.
|
||||
*
|
||||
* @return boolean True if started.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
function start();
|
||||
|
||||
/**
|
||||
* Returns the session ID
|
||||
*
|
||||
* @return mixed The session ID or false if the session has not started.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
function getId();
|
||||
|
||||
/**
|
||||
* Regenerates id that represents this storage.
|
||||
*
|
||||
* This method must invoke session_regenerate_id($destroy) unless
|
||||
* this interface is used for a storage object designed for unit
|
||||
* or functional testing where a real PHP session would interfere
|
||||
* with testing.
|
||||
*
|
||||
* @param Boolean $destroy Destroy session when regenerating?
|
||||
*
|
||||
* @return Boolean True if session regenerated, false if error
|
||||
*
|
||||
* @throws \RuntimeException If an error occurs while regenerating this storage
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
function regenerate($destroy = false);
|
||||
|
||||
/**
|
||||
* Force the session to be saved and closed.
|
||||
*
|
||||
* This method must invoke session_write_close() unless this interface is
|
||||
* used for a storage object design for unit or functional testing where
|
||||
* a real PHP session would interfere with testing, in which case it
|
||||
* it should actually persist the session data if required.
|
||||
*/
|
||||
function save();
|
||||
|
||||
/**
|
||||
* Clear all session data in memory.
|
||||
*/
|
||||
function clear();
|
||||
|
||||
/**
|
||||
* Gets a SessionBagInterface by name.
|
||||
*
|
||||
* @return SessionBagInterface
|
||||
*/
|
||||
function getBag($name);
|
||||
|
||||
/**
|
||||
* Registers a SessionBagInterface for use.
|
||||
*
|
||||
* @param SessionBagInterface $bag
|
||||
*/
|
||||
function registerBag(SessionBagInterface $bag);
|
||||
}
|
@ -1,81 +0,0 @@
|
||||
<?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\SessionStorage;
|
||||
|
||||
/**
|
||||
* ArraySessionStorage mocks the session for unit tests.
|
||||
*
|
||||
* When doing functional testing, you should use FilesystemSessionStorage instead.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
|
||||
*/
|
||||
|
||||
class ArraySessionStorage implements SessionStorageInterface
|
||||
{
|
||||
/**
|
||||
* Storage data.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $data = array();
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function read($key, $default = null)
|
||||
{
|
||||
return array_key_exists($key, $this->data) ? $this->data[$key] : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function regenerate($destroy = false)
|
||||
{
|
||||
if ($destroy) {
|
||||
$this->data = array();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function remove($key)
|
||||
{
|
||||
unset($this->data[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function start()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function write($key, $data)
|
||||
{
|
||||
$this->data[$key] = $data;
|
||||
}
|
||||
}
|
@ -1,192 +0,0 @@
|
||||
<?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\SessionStorage;
|
||||
|
||||
/**
|
||||
* FilesystemSessionStorage simulates sessions for functional tests.
|
||||
*
|
||||
* This storage does not start the session (session_start())
|
||||
* as it is not "available" when running tests on the command line.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
class FilesystemSessionStorage extends NativeSessionStorage
|
||||
{
|
||||
/**
|
||||
* File path.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $path;
|
||||
|
||||
/**
|
||||
* Data.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $data;
|
||||
|
||||
/**
|
||||
* Session started flag.
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
private $started;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct($path, array $options = array())
|
||||
{
|
||||
$this->path = $path;
|
||||
$this->started = false;
|
||||
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the session.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function start()
|
||||
{
|
||||
if ($this->started) {
|
||||
return;
|
||||
}
|
||||
|
||||
session_set_cookie_params(
|
||||
$this->options['lifetime'],
|
||||
$this->options['path'],
|
||||
$this->options['domain'],
|
||||
$this->options['secure'],
|
||||
$this->options['httponly']
|
||||
);
|
||||
|
||||
if (!ini_get('session.use_cookies') && isset($this->options['id']) && $this->options['id'] && $this->options['id'] != session_id()) {
|
||||
session_id($this->options['id']);
|
||||
}
|
||||
|
||||
if (!session_id()) {
|
||||
session_id(hash('md5', uniqid(mt_rand(), true)));
|
||||
}
|
||||
|
||||
$file = $this->path.'/'.session_id().'.session';
|
||||
|
||||
$this->data = is_file($file) ? unserialize(file_get_contents($file)) : array();
|
||||
$this->started = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the session ID
|
||||
*
|
||||
* @return mixed The session ID
|
||||
*
|
||||
* @throws \RuntimeException If the session was not started yet
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
if (!$this->started) {
|
||||
throw new \RuntimeException('The session must be started before reading its ID');
|
||||
}
|
||||
|
||||
return session_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads data from this storage.
|
||||
*
|
||||
* The preferred format for a key is directory style so naming conflicts can be avoided.
|
||||
*
|
||||
* @param string $key A unique key identifying your data
|
||||
* @param string $default The default value
|
||||
*
|
||||
* @return mixed Data associated with the key
|
||||
*
|
||||
* @throws \RuntimeException If an error occurs while reading data from this storage
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function read($key, $default = null)
|
||||
{
|
||||
return array_key_exists($key, $this->data) ? $this->data[$key] : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes data from this storage.
|
||||
*
|
||||
* The preferred format for a key is directory style so naming conflicts can be avoided.
|
||||
*
|
||||
* @param string $key A unique key identifying your data
|
||||
*
|
||||
* @return mixed Data associated with the key
|
||||
*
|
||||
* @throws \RuntimeException If an error occurs while removing data from this storage
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function remove($key)
|
||||
{
|
||||
$retval = $this->data[$key];
|
||||
|
||||
unset($this->data[$key]);
|
||||
|
||||
return $retval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes data to this storage.
|
||||
*
|
||||
* The preferred format for a key is directory style so naming conflicts can be avoided.
|
||||
*
|
||||
* @param string $key A unique key identifying your data
|
||||
* @param mixed $data Data associated with your key
|
||||
*
|
||||
* @throws \RuntimeException If an error occurs while writing to this storage
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function write($key, $data)
|
||||
{
|
||||
$this->data[$key] = $data;
|
||||
|
||||
if (!is_dir($this->path)) {
|
||||
mkdir($this->path, 0777, true);
|
||||
}
|
||||
|
||||
file_put_contents($this->path.'/'.session_id().'.session', serialize($this->data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Regenerates id that represents this storage.
|
||||
*
|
||||
* @param Boolean $destroy Destroy session when regenerating?
|
||||
*
|
||||
* @return Boolean True if session regenerated, false if error
|
||||
*
|
||||
* @throws \RuntimeException If an error occurs while regenerating this storage
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function regenerate($destroy = false)
|
||||
{
|
||||
if ($destroy) {
|
||||
$this->data = array();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -1,180 +0,0 @@
|
||||
<?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\SessionStorage;
|
||||
|
||||
/**
|
||||
* NativeSessionStorage.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
class NativeSessionStorage implements SessionStorageInterface
|
||||
{
|
||||
static protected $sessionIdRegenerated = false;
|
||||
static protected $sessionStarted = false;
|
||||
|
||||
protected $options;
|
||||
|
||||
/**
|
||||
* Available options:
|
||||
*
|
||||
* * name: The cookie name (null [omitted] by default)
|
||||
* * id: The session id (null [omitted] by default)
|
||||
* * lifetime: Cookie lifetime
|
||||
* * path: Cookie path
|
||||
* * domain: Cookie domain
|
||||
* * secure: Cookie secure
|
||||
* * httponly: Cookie http only
|
||||
*
|
||||
* The default values for most options are those returned by the session_get_cookie_params() function
|
||||
*
|
||||
* @param array $options An associative array of session options
|
||||
*/
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
$cookieDefaults = session_get_cookie_params();
|
||||
|
||||
$this->options = array_merge(array(
|
||||
'lifetime' => $cookieDefaults['lifetime'],
|
||||
'path' => $cookieDefaults['path'],
|
||||
'domain' => $cookieDefaults['domain'],
|
||||
'secure' => $cookieDefaults['secure'],
|
||||
'httponly' => isset($cookieDefaults['httponly']) ? $cookieDefaults['httponly'] : false,
|
||||
), $options);
|
||||
|
||||
// Skip setting new session name if user don't want it
|
||||
if (isset($this->options['name'])) {
|
||||
session_name($this->options['name']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the session.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function start()
|
||||
{
|
||||
if (self::$sessionStarted) {
|
||||
return;
|
||||
}
|
||||
|
||||
session_set_cookie_params(
|
||||
$this->options['lifetime'],
|
||||
$this->options['path'],
|
||||
$this->options['domain'],
|
||||
$this->options['secure'],
|
||||
$this->options['httponly']
|
||||
);
|
||||
|
||||
// disable native cache limiter as this is managed by HeaderBag directly
|
||||
session_cache_limiter(false);
|
||||
|
||||
if (!ini_get('session.use_cookies') && isset($this->options['id']) && $this->options['id'] && $this->options['id'] != session_id()) {
|
||||
session_id($this->options['id']);
|
||||
}
|
||||
|
||||
session_start();
|
||||
|
||||
self::$sessionStarted = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
if (!self::$sessionStarted) {
|
||||
throw new \RuntimeException('The session must be started before reading its ID');
|
||||
}
|
||||
|
||||
return session_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads data from this storage.
|
||||
*
|
||||
* The preferred format for a key is directory style so naming conflicts can be avoided.
|
||||
*
|
||||
* @param string $key A unique key identifying your data
|
||||
* @param string $default Default value
|
||||
*
|
||||
* @return mixed Data associated with the key
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function read($key, $default = null)
|
||||
{
|
||||
return array_key_exists($key, $_SESSION) ? $_SESSION[$key] : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes data from this storage.
|
||||
*
|
||||
* The preferred format for a key is directory style so naming conflicts can be avoided.
|
||||
*
|
||||
* @param string $key A unique key identifying your data
|
||||
*
|
||||
* @return mixed Data associated with the key
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function remove($key)
|
||||
{
|
||||
$retval = null;
|
||||
|
||||
if (isset($_SESSION[$key])) {
|
||||
$retval = $_SESSION[$key];
|
||||
unset($_SESSION[$key]);
|
||||
}
|
||||
|
||||
return $retval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes data to this storage.
|
||||
*
|
||||
* The preferred format for a key is directory style so naming conflicts can be avoided.
|
||||
*
|
||||
* @param string $key A unique key identifying your data
|
||||
* @param mixed $data Data associated with your key
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function write($key, $data)
|
||||
{
|
||||
$_SESSION[$key] = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Regenerates id that represents this storage.
|
||||
*
|
||||
* @param Boolean $destroy Destroy session when regenerating?
|
||||
*
|
||||
* @return Boolean True if session regenerated, false if error
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function regenerate($destroy = false)
|
||||
{
|
||||
if (self::$sessionIdRegenerated) {
|
||||
return;
|
||||
}
|
||||
|
||||
session_regenerate_id($destroy);
|
||||
|
||||
self::$sessionIdRegenerated = true;
|
||||
}
|
||||
}
|
@ -1,97 +0,0 @@
|
||||
<?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\SessionStorage;
|
||||
|
||||
/**
|
||||
* SessionStorageInterface.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
interface SessionStorageInterface
|
||||
{
|
||||
/**
|
||||
* Starts the session.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
function start();
|
||||
|
||||
/**
|
||||
* Returns the session ID
|
||||
*
|
||||
* @return mixed The session ID
|
||||
*
|
||||
* @throws \RuntimeException If the session was not started yet
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
function getId();
|
||||
|
||||
/**
|
||||
* Reads data from this storage.
|
||||
*
|
||||
* The preferred format for a key is directory style so naming conflicts can be avoided.
|
||||
*
|
||||
* @param string $key A unique key identifying your data
|
||||
*
|
||||
* @return mixed Data associated with the key
|
||||
*
|
||||
* @throws \RuntimeException If an error occurs while reading data from this storage
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
function read($key);
|
||||
|
||||
/**
|
||||
* Removes data from this storage.
|
||||
*
|
||||
* The preferred format for a key is directory style so naming conflicts can be avoided.
|
||||
*
|
||||
* @param string $key A unique key identifying your data
|
||||
*
|
||||
* @return mixed Data associated with the key
|
||||
*
|
||||
* @throws \RuntimeException If an error occurs while removing data from this storage
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
function remove($key);
|
||||
|
||||
/**
|
||||
* Writes data to this storage.
|
||||
*
|
||||
* The preferred format for a key is directory style so naming conflicts can be avoided.
|
||||
*
|
||||
* @param string $key A unique key identifying your data
|
||||
* @param mixed $data Data associated with your key
|
||||
*
|
||||
* @throws \RuntimeException If an error occurs while writing to this storage
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
function write($key, $data);
|
||||
|
||||
/**
|
||||
* Regenerates id that represents this storage.
|
||||
*
|
||||
* @param Boolean $destroy Destroy session when regenerating?
|
||||
*
|
||||
* @return Boolean True if session regenerated, false if error
|
||||
*
|
||||
* @throws \RuntimeException If an error occurs while regenerating this storage
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
function regenerate($destroy = false);
|
||||
}
|
@ -21,7 +21,7 @@ class SessionCsrfProviderTest extends \PHPUnit_Framework_TestCase
|
||||
protected function setUp()
|
||||
{
|
||||
$this->session = $this->getMock(
|
||||
'Symfony\Component\HttpFoundation\Session',
|
||||
'Symfony\Component\HttpFoundation\Session\Session',
|
||||
array(),
|
||||
array(),
|
||||
'',
|
||||
|
@ -12,10 +12,8 @@
|
||||
namespace Symfony\Tests\Component\HttpFoundation;
|
||||
|
||||
|
||||
use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
class RequestTest extends \PHPUnit_Framework_TestCase
|
||||
@ -848,7 +846,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
|
||||
$request = new Request;
|
||||
|
||||
$this->assertFalse($request->hasSession());
|
||||
$request->setSession(new Session(new ArraySessionStorage()));
|
||||
$request->setSession(new Session(new MockArraySessionStorage()));
|
||||
$this->assertTrue($request->hasSession());
|
||||
}
|
||||
|
||||
@ -859,7 +857,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($request->hasPreviousSession());
|
||||
$request->cookies->set(session_name(), 'foo');
|
||||
$this->assertFalse($request->hasPreviousSession());
|
||||
$request->setSession(new Session(new ArraySessionStorage()));
|
||||
$request->setSession(new Session(new MockArraySessionStorage()));
|
||||
$this->assertTrue($request->hasPreviousSession());
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Tests\Component\HttpFoundation\Session\Attribute;
|
||||
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
|
||||
|
||||
|
||||
/**
|
||||
* Tests AttributeBag
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class AttributeBagTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $array;
|
||||
|
||||
/**
|
||||
* @var AttributeBag
|
||||
*/
|
||||
private $bag;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->array = array(
|
||||
'hello' => 'world',
|
||||
'always' => 'be happy',
|
||||
'user.login' => 'drak',
|
||||
'csrf.token' => array(
|
||||
'a' => '1234',
|
||||
'b' => '4321',
|
||||
),
|
||||
'category' => array(
|
||||
'fishing' => array(
|
||||
'first' => 'cod',
|
||||
'second' => 'sole')
|
||||
),
|
||||
);
|
||||
$this->bag = new AttributeBag('_sf2');
|
||||
$this->bag->initialize($this->array);
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
$this->bag = null;
|
||||
$this->array = array();
|
||||
}
|
||||
|
||||
public function testInitialize()
|
||||
{
|
||||
$bag = new AttributeBag();
|
||||
$bag->initialize($this->array);
|
||||
$this->assertEquals($this->array, $bag->all());
|
||||
$array = array('should' => 'change');
|
||||
$bag->initialize($array);
|
||||
$this->assertEquals($array, $bag->all());
|
||||
}
|
||||
|
||||
public function testGetStorageKey()
|
||||
{
|
||||
$this->assertEquals('_sf2', $this->bag->getStorageKey());
|
||||
$attributeBag = new AttributeBag('test');
|
||||
$this->assertEquals('test', $attributeBag->getStorageKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider attributesProvider
|
||||
*/
|
||||
public function testHas($key, $value, $exists)
|
||||
{
|
||||
$this->assertEquals($exists, $this->bag->has($key));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider attributesProvider
|
||||
*/
|
||||
public function testGet($key, $value, $expected)
|
||||
{
|
||||
$this->assertEquals($value, $this->bag->get($key));
|
||||
}
|
||||
|
||||
public function testGetDefaults()
|
||||
{
|
||||
$this->assertNull($this->bag->get('user2.login'));
|
||||
$this->assertEquals('default', $this->bag->get('user2.login', 'default'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider attributesProvider
|
||||
*/
|
||||
public function testSet($key, $value, $expected)
|
||||
{
|
||||
$this->bag->set($key, $value);
|
||||
$this->assertEquals($value, $this->bag->get($key));
|
||||
}
|
||||
|
||||
public function testAll()
|
||||
{
|
||||
$this->assertEquals($this->array, $this->bag->all());
|
||||
|
||||
$this->bag->set('hello', 'fabien');
|
||||
$array = $this->array;
|
||||
$array['hello'] = 'fabien';
|
||||
$this->assertEquals($array, $this->bag->all());
|
||||
}
|
||||
|
||||
public function testReplace()
|
||||
{
|
||||
$array = array();
|
||||
$array['name'] = 'jack';
|
||||
$array['foo.bar'] = 'beep';
|
||||
$this->bag->replace($array);
|
||||
$this->assertEquals($array, $this->bag->all());
|
||||
$this->assertNull($this->bag->get('hello'));
|
||||
$this->assertNull($this->bag->get('always'));
|
||||
$this->assertNull($this->bag->get('user.login'));
|
||||
}
|
||||
|
||||
public function testRemove()
|
||||
{
|
||||
$this->assertEquals('world', $this->bag->get('hello'));
|
||||
$this->bag->remove('hello');
|
||||
$this->assertNull($this->bag->get('hello'));
|
||||
|
||||
$this->assertEquals('be happy', $this->bag->get('always'));
|
||||
$this->bag->remove('always');
|
||||
$this->assertNull($this->bag->get('always'));
|
||||
|
||||
$this->assertEquals('drak', $this->bag->get('user.login'));
|
||||
$this->bag->remove('user.login');
|
||||
$this->assertNull($this->bag->get('user.login'));
|
||||
}
|
||||
|
||||
public function testClear()
|
||||
{
|
||||
$this->bag->clear();
|
||||
$this->assertEquals(array(), $this->bag->all());
|
||||
}
|
||||
|
||||
public function attributesProvider()
|
||||
{
|
||||
return array(
|
||||
array('hello', 'world', true),
|
||||
array('always', 'be happy', true),
|
||||
array('user.login', 'drak', true),
|
||||
array('csrf.token', array('a' => '1234', 'b' => '4321'), true),
|
||||
array('category', array('fishing' => array('first' => 'cod', 'second' => 'sole')), true),
|
||||
array('user2.login', null, false),
|
||||
array('never', null, false),
|
||||
array('bye', null, false),
|
||||
array('bye/for/now', null, false),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Tests\Component\HttpFoundation;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;
|
||||
|
||||
/**
|
||||
* Tests NamespacedAttributeBag
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class NamespacedAttributeBagTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $array;
|
||||
|
||||
/**
|
||||
* @var NamespacedAttributeBag
|
||||
*/
|
||||
private $bag;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->array = array(
|
||||
'hello' => 'world',
|
||||
'always' => 'be happy',
|
||||
'user.login' => 'drak',
|
||||
'csrf.token' => array(
|
||||
'a' => '1234',
|
||||
'b' => '4321',
|
||||
),
|
||||
'category' => array(
|
||||
'fishing' => array(
|
||||
'first' => 'cod',
|
||||
'second' => 'sole')
|
||||
),
|
||||
);
|
||||
$this->bag = new NamespacedAttributeBag('_sf2', '/');
|
||||
$this->bag->initialize($this->array);
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
$this->bag = null;
|
||||
$this->array = array();
|
||||
}
|
||||
|
||||
public function testInitialize()
|
||||
{
|
||||
$bag = new NamespacedAttributeBag();
|
||||
$bag->initialize($this->array);
|
||||
$this->assertEquals($this->array, $this->bag->all());
|
||||
$array = array('should' => 'not stick');
|
||||
$bag->initialize($array);
|
||||
|
||||
// should have remained the same
|
||||
$this->assertEquals($this->array, $this->bag->all());
|
||||
}
|
||||
|
||||
public function testGetStorageKey()
|
||||
{
|
||||
$this->assertEquals('_sf2', $this->bag->getStorageKey());
|
||||
$attributeBag = new NamespacedAttributeBag('test');
|
||||
$this->assertEquals('test', $attributeBag->getStorageKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider attributesProvider
|
||||
*/
|
||||
public function testHas($key, $value, $exists)
|
||||
{
|
||||
$this->assertEquals($exists, $this->bag->has($key));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider attributesProvider
|
||||
*/
|
||||
public function testGet($key, $value, $expected)
|
||||
{
|
||||
$this->assertEquals($value, $this->bag->get($key));
|
||||
}
|
||||
|
||||
public function testGetDefaults()
|
||||
{
|
||||
$this->assertNull($this->bag->get('user2.login'));
|
||||
$this->assertEquals('default', $this->bag->get('user2.login', 'default'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider attributesProvider
|
||||
*/
|
||||
public function testSet($key, $value, $expected)
|
||||
{
|
||||
$this->bag->set($key, $value);
|
||||
$this->assertEquals($value, $this->bag->get($key));
|
||||
}
|
||||
|
||||
public function testAll()
|
||||
{
|
||||
$this->assertEquals($this->array, $this->bag->all());
|
||||
|
||||
$this->bag->set('hello', 'fabien');
|
||||
$array = $this->array;
|
||||
$array['hello'] = 'fabien';
|
||||
$this->assertEquals($array, $this->bag->all());
|
||||
}
|
||||
|
||||
public function testReplace()
|
||||
{
|
||||
$array = array();
|
||||
$array['name'] = 'jack';
|
||||
$array['foo.bar'] = 'beep';
|
||||
$this->bag->replace($array);
|
||||
$this->assertEquals($array, $this->bag->all());
|
||||
$this->assertNull($this->bag->get('hello'));
|
||||
$this->assertNull($this->bag->get('always'));
|
||||
$this->assertNull($this->bag->get('user.login'));
|
||||
}
|
||||
|
||||
public function testRemove()
|
||||
{
|
||||
$this->assertEquals('world', $this->bag->get('hello'));
|
||||
$this->bag->remove('hello');
|
||||
$this->assertNull($this->bag->get('hello'));
|
||||
|
||||
$this->assertEquals('be happy', $this->bag->get('always'));
|
||||
$this->bag->remove('always');
|
||||
$this->assertNull($this->bag->get('always'));
|
||||
|
||||
$this->assertEquals('drak', $this->bag->get('user.login'));
|
||||
$this->bag->remove('user.login');
|
||||
$this->assertNull($this->bag->get('user.login'));
|
||||
}
|
||||
|
||||
public function testClear()
|
||||
{
|
||||
$this->bag->clear();
|
||||
$this->assertEquals(array(), $this->bag->all());
|
||||
}
|
||||
|
||||
public function attributesProvider()
|
||||
{
|
||||
return array(
|
||||
array('hello', 'world', true),
|
||||
array('always', 'be happy', true),
|
||||
array('user.login', 'drak', true),
|
||||
array('csrf.token', array('a' => '1234', 'b' => '4321'), true),
|
||||
array('csrf.token/a', '1234', true),
|
||||
array('csrf.token/b', '4321', true),
|
||||
array('category', array('fishing' => array('first' => 'cod', 'second' => 'sole')), true),
|
||||
array('category/fishing', array('first' => 'cod', 'second' => 'sole'), true),
|
||||
array('category/fishing/first', 'cod', true),
|
||||
array('category/fishing/second', 'sole', true),
|
||||
array('user2.login', null, false),
|
||||
array('never', null, false),
|
||||
array('bye', null, false),
|
||||
array('bye/for/now', null, false),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,130 @@
|
||||
<?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\Tests\Component\HttpFoundation\Session\Flash;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag as FlashBag;
|
||||
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
|
||||
|
||||
/**
|
||||
* AutoExpireFlashBagTest
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class AutoExpireFlashBagTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface
|
||||
*/
|
||||
private $bag;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $array = array();
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->bag = new FlashBag();
|
||||
$this->array = array('new' => array('notice' => 'A previous flash message'));
|
||||
$this->bag->initialize($this->array);
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
$this->bag = null;
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testInitialize()
|
||||
{
|
||||
$bag = new FlashBag();
|
||||
$array = array('new' => array('notice' => 'A previous flash message'));
|
||||
$bag->initialize($array);
|
||||
$this->assertEquals('A previous flash message', $bag->peek('notice'));
|
||||
$array = array('new' => array(
|
||||
'notice' => 'Something else',
|
||||
'error' => 'a',
|
||||
));
|
||||
$bag->initialize($array);
|
||||
$this->assertEquals('Something else', $bag->peek('notice'));
|
||||
$this->assertEquals('a', $bag->peek('error'));
|
||||
}
|
||||
|
||||
public function testPeek()
|
||||
{
|
||||
$this->assertNull($this->bag->peek('non_existing'));
|
||||
$this->assertEquals('default', $this->bag->peek('non_existing', 'default'));
|
||||
$this->assertEquals('A previous flash message', $this->bag->peek('notice'));
|
||||
$this->assertEquals('A previous flash message', $this->bag->peek('notice'));
|
||||
}
|
||||
|
||||
public function testSet()
|
||||
{
|
||||
$this->bag->set('notice', 'Foo');
|
||||
$this->assertNotEquals('Foo', $this->bag->peek('notice'));
|
||||
}
|
||||
|
||||
public function testHas()
|
||||
{
|
||||
$this->assertFalse($this->bag->has('nothing'));
|
||||
$this->assertTrue($this->bag->has('notice'));
|
||||
}
|
||||
|
||||
public function testKeys()
|
||||
{
|
||||
$this->assertEquals(array('notice'), $this->bag->keys());
|
||||
}
|
||||
|
||||
public function testPeekAll()
|
||||
{
|
||||
$array = array(
|
||||
'new' => array(
|
||||
'notice' => 'Foo',
|
||||
'error' => 'Bar',
|
||||
),
|
||||
);
|
||||
|
||||
$this->bag->initialize($array);
|
||||
$this->assertEquals(array(
|
||||
'notice' => 'Foo',
|
||||
'error' => 'Bar',
|
||||
), $this->bag->peekAll()
|
||||
);
|
||||
|
||||
$this->assertEquals(array(
|
||||
'notice' => 'Foo',
|
||||
'error' => 'Bar',
|
||||
), $this->bag->peekAll()
|
||||
);
|
||||
}
|
||||
|
||||
public function testGet()
|
||||
{
|
||||
$this->assertNull($this->bag->get('non_existing'));
|
||||
$this->assertEquals('default', $this->bag->get('non_existing', 'default'));
|
||||
$this->assertEquals('A previous flash message', $this->bag->get('notice'));
|
||||
$this->assertNull($this->bag->get('notice'));
|
||||
}
|
||||
|
||||
public function testAll()
|
||||
{
|
||||
$this->bag->set('notice', 'Foo');
|
||||
$this->bag->set('error', 'Bar');
|
||||
$this->assertEquals(array(
|
||||
'notice' => 'A previous flash message',
|
||||
), $this->bag->all()
|
||||
);
|
||||
|
||||
$this->assertEquals(array(), $this->bag->all());
|
||||
}
|
||||
}
|
@ -0,0 +1,121 @@
|
||||
<?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\Tests\Component\HttpFoundation\Session\Flash;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
|
||||
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
|
||||
|
||||
/**
|
||||
* FlashBagTest
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class FlashBagTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Symfony\Component\HttpFoundation\SessionFlash\FlashBagInterface
|
||||
*/
|
||||
private $bag;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $array = array();
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->bag = new FlashBag();
|
||||
$this->array = array('notice' => 'A previous flash message');
|
||||
$this->bag->initialize($this->array);
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
$this->bag = null;
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testInitialize()
|
||||
{
|
||||
$bag = new FlashBag();
|
||||
$bag->initialize($this->array);
|
||||
$this->assertEquals($this->array, $bag->peekAll());
|
||||
$array = array('should' => array('change'));
|
||||
$bag->initialize($array);
|
||||
$this->assertEquals($array, $bag->peekAll());
|
||||
}
|
||||
|
||||
public function testPeek()
|
||||
{
|
||||
$this->assertNull($this->bag->peek('non_existing'));
|
||||
$this->assertEquals('default', $this->bag->peek('not_existing', 'default'));
|
||||
$this->assertEquals('A previous flash message', $this->bag->peek('notice'));
|
||||
$this->assertEquals('A previous flash message', $this->bag->peek('notice'));
|
||||
}
|
||||
|
||||
public function testGet()
|
||||
{
|
||||
$this->assertNull($this->bag->get('non_existing'));
|
||||
$this->assertEquals('default', $this->bag->get('not_existing', 'default'));
|
||||
$this->assertEquals('A previous flash message', $this->bag->get('notice'));
|
||||
$this->assertNull($this->bag->get('notice'));
|
||||
}
|
||||
|
||||
public function testAll()
|
||||
{
|
||||
$this->bag->set('notice', 'Foo');
|
||||
$this->bag->set('error', 'Bar');
|
||||
$this->assertEquals(array(
|
||||
'notice' => 'Foo',
|
||||
'error' => 'Bar'), $this->bag->all()
|
||||
);
|
||||
|
||||
$this->assertEquals(array(), $this->bag->all());
|
||||
}
|
||||
|
||||
public function testSet()
|
||||
{
|
||||
$this->bag->set('notice', 'Foo');
|
||||
$this->bag->set('notice', 'Bar');
|
||||
$this->assertEquals('Bar', $this->bag->peek('notice'));
|
||||
}
|
||||
|
||||
public function testHas()
|
||||
{
|
||||
$this->assertFalse($this->bag->has('nothing'));
|
||||
$this->assertTrue($this->bag->has('notice'));
|
||||
}
|
||||
|
||||
public function testKeys()
|
||||
{
|
||||
$this->assertEquals(array('notice'), $this->bag->keys());
|
||||
}
|
||||
|
||||
public function testPeekAll()
|
||||
{
|
||||
$this->bag->set('notice', 'Foo');
|
||||
$this->bag->set('error', 'Bar');
|
||||
$this->assertEquals(array(
|
||||
'notice' => 'Foo',
|
||||
'error' => 'Bar',
|
||||
), $this->bag->peekAll()
|
||||
);
|
||||
$this->assertTrue($this->bag->has('notice'));
|
||||
$this->assertTrue($this->bag->has('error'));
|
||||
$this->assertEquals(array(
|
||||
'notice' => 'Foo',
|
||||
'error' => 'Bar',
|
||||
), $this->bag->peekAll()
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,171 @@
|
||||
<?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\Tests\Component\HttpFoundation\Session;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
|
||||
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
||||
|
||||
/**
|
||||
* SessionTest
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Robert Schönthal <seroscho@googlemail.com>
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class SessionTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface
|
||||
*/
|
||||
protected $storage;
|
||||
|
||||
/**
|
||||
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface
|
||||
*/
|
||||
protected $session;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->storage = new MockArraySessionStorage();
|
||||
$this->session = new Session($this->storage, new AttributeBag(), new FlashBag());
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
$this->storage = null;
|
||||
$this->session = null;
|
||||
}
|
||||
|
||||
public function testStart()
|
||||
{
|
||||
$this->assertEquals('', $this->session->getId());
|
||||
$this->assertTrue($this->session->start());
|
||||
$this->assertNotEquals('', $this->session->getId());
|
||||
}
|
||||
|
||||
public function testGet()
|
||||
{
|
||||
// tests defaults
|
||||
$this->assertNull($this->session->get('foo'));
|
||||
$this->assertEquals(1, $this->session->get('foo', 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider setProvider
|
||||
*/
|
||||
public function testSet($key, $value)
|
||||
{
|
||||
$this->session->set($key, $value);
|
||||
$this->assertEquals($value, $this->session->get($key));
|
||||
}
|
||||
|
||||
public function testReplace()
|
||||
{
|
||||
$this->session->replace(array('happiness' => 'be good', 'symfony' => 'awesome'));
|
||||
$this->assertEquals(array('happiness' => 'be good', 'symfony' => 'awesome'), $this->session->all());
|
||||
$this->session->replace(array());
|
||||
$this->assertEquals(array(), $this->session->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider setProvider
|
||||
*/
|
||||
public function testAll($key, $value, $result)
|
||||
{
|
||||
$this->session->set($key, $value);
|
||||
$this->assertEquals($result, $this->session->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider setProvider
|
||||
*/
|
||||
public function testClear($key, $value)
|
||||
{
|
||||
$this->session->set('hi', 'fabien');
|
||||
$this->session->set($key, $value);
|
||||
$this->session->clear();
|
||||
$this->assertEquals(array(), $this->session->all());
|
||||
}
|
||||
|
||||
public function setProvider()
|
||||
{
|
||||
return array(
|
||||
array('foo', 'bar', array('foo' => 'bar')),
|
||||
array('foo.bar', 'too much beer', array('foo.bar' => 'too much beer')),
|
||||
array('great', 'symfony2 is great', array('great' => 'symfony2 is great')),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider setProvider
|
||||
*/
|
||||
public function testRemove($key, $value)
|
||||
{
|
||||
$this->session->set('hi.world', 'have a nice day');
|
||||
$this->session->set($key, $value);
|
||||
$this->session->remove($key);
|
||||
$this->assertEquals(array('hi.world' => 'have a nice day'), $this->session->all());
|
||||
}
|
||||
|
||||
public function testInvalidate()
|
||||
{
|
||||
$this->session->set('invalidate', 123);
|
||||
$this->session->invalidate();
|
||||
$this->assertEquals(array(), $this->session->all());
|
||||
}
|
||||
|
||||
public function testMigrate()
|
||||
{
|
||||
$this->session->set('migrate', 321);
|
||||
$this->session->migrate();
|
||||
$this->assertEquals(321, $this->session->get('migrate'));
|
||||
}
|
||||
|
||||
public function testMigrateDestroy()
|
||||
{
|
||||
$this->session->set('migrate', 333);
|
||||
$this->session->migrate(true);
|
||||
$this->assertEquals(333, $this->session->get('migrate'));
|
||||
}
|
||||
|
||||
public function testSerialize()
|
||||
{
|
||||
$compare = serialize($this->storage);
|
||||
|
||||
$this->assertSame($compare, $this->session->serialize());
|
||||
|
||||
$this->session->unserialize($compare);
|
||||
|
||||
$_storage = new \ReflectionProperty(get_class($this->session), 'storage');
|
||||
$_storage->setAccessible(true);
|
||||
|
||||
$this->assertEquals($_storage->getValue($this->session), $this->storage, 'storage match');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testUnserializeException()
|
||||
{
|
||||
$serialized = serialize(new \ArrayObject());
|
||||
$this->session->unserialize($serialized);
|
||||
}
|
||||
|
||||
public function testGetId()
|
||||
{
|
||||
$this->assertEquals('', $this->session->getId());
|
||||
$this->session->start();
|
||||
$this->assertNotEquals('', $this->session->getId());
|
||||
}
|
||||
}
|
@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Tests\Component\HttpFoundation\Session\Storage;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\AbstractSessionStorage;
|
||||
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
|
||||
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\SessionSaveHandlerInterface;
|
||||
|
||||
/**
|
||||
* Turn AbstractSessionStorage into something concrete because
|
||||
* certain mocking features are broken in PHPUnit-Mock-Objects < 1.1.2
|
||||
* @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/73
|
||||
*/
|
||||
class ConcreteSessionStorage extends AbstractSessionStorage
|
||||
{
|
||||
}
|
||||
|
||||
class CustomHandlerSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface
|
||||
{
|
||||
public function openSession($path, $id)
|
||||
{
|
||||
}
|
||||
|
||||
public function closeSession()
|
||||
{
|
||||
}
|
||||
|
||||
public function readSession($id)
|
||||
{
|
||||
}
|
||||
|
||||
public function writeSession($id, $data)
|
||||
{
|
||||
}
|
||||
|
||||
public function destroySession($id)
|
||||
{
|
||||
}
|
||||
|
||||
public function gcSession($lifetime)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test class for AbstractSessionStorage.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*
|
||||
* These tests require separate processes.
|
||||
*
|
||||
* @runTestsInSeparateProcesses
|
||||
*/
|
||||
class AbstractSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @return AbstractSessionStorage
|
||||
*/
|
||||
protected function getStorage()
|
||||
{
|
||||
$storage = new CustomHandlerSessionStorage();
|
||||
$storage->registerBag(new AttributeBag);
|
||||
|
||||
return $storage;
|
||||
}
|
||||
|
||||
public function testBag()
|
||||
{
|
||||
$storage = $this->getStorage();
|
||||
$bag = new FlashBag();
|
||||
$storage->registerBag($bag);
|
||||
$this->assertSame($bag, $storage->getBag($bag->getName()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testRegisterBagException()
|
||||
{
|
||||
$storage = $this->getStorage();
|
||||
$storage->getBag('non_existing');
|
||||
}
|
||||
|
||||
public function testGetId()
|
||||
{
|
||||
$storage = $this->getStorage();
|
||||
$this->assertEquals('', $storage->getId());
|
||||
$storage->start();
|
||||
$this->assertNotEquals('', $storage->getId());
|
||||
}
|
||||
|
||||
public function testRegenerate()
|
||||
{
|
||||
$storage = $this->getStorage();
|
||||
$storage->start();
|
||||
$id = $storage->getId();
|
||||
$storage->getBag('attributes')->set('lucky', 7);
|
||||
$storage->regenerate();
|
||||
$this->assertNotEquals($id, $storage->getId());
|
||||
$this->assertEquals(7, $storage->getBag('attributes')->get('lucky'));
|
||||
|
||||
}
|
||||
|
||||
public function testRegenerateDestroy()
|
||||
{
|
||||
$storage = $this->getStorage();
|
||||
$storage->start();
|
||||
$id = $storage->getId();
|
||||
$storage->getBag('attributes')->set('legs', 11);
|
||||
$storage->regenerate(true);
|
||||
$this->assertNotEquals($id, $storage->getId());
|
||||
$this->assertEquals(11, $storage->getBag('attributes')->get('legs'));
|
||||
}
|
||||
|
||||
public function testCustomSaveHandlers()
|
||||
{
|
||||
$storage = new CustomHandlerSessionStorage();
|
||||
$this->assertEquals('user', ini_get('session.save_handler'));
|
||||
}
|
||||
|
||||
public function testNativeSaveHandlers()
|
||||
{
|
||||
$storage = new ConcreteSessionStorage();
|
||||
$this->assertNotEquals('user', ini_get('session.save_handler'));
|
||||
}
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Tests\Component\HttpFoundation\Session\Storage;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
||||
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
|
||||
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
|
||||
|
||||
|
||||
/**
|
||||
* Test class for MockArraySessionStorage.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class MockArraySessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var MockArraySessionStorage
|
||||
*/
|
||||
private $storage;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $attributes;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $flashes;
|
||||
|
||||
private $data;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->attributes = new AttributeBag();
|
||||
$this->flashes = new FlashBag();
|
||||
|
||||
$this->data = array(
|
||||
$this->attributes->getStorageKey() => array('foo' => 'bar'),
|
||||
$this->flashes->getStorageKey() => array('notice' => 'hello'),
|
||||
);
|
||||
|
||||
$this->storage = new MockArraySessionStorage();
|
||||
$this->storage->registerBag($this->flashes);
|
||||
$this->storage->registerBag($this->attributes);
|
||||
$this->storage->setSessionData($this->data);
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
$this->data = null;
|
||||
$this->flashes = null;
|
||||
$this->attributes = null;
|
||||
$this->storage = null;
|
||||
}
|
||||
|
||||
public function testStart()
|
||||
{
|
||||
$this->assertEquals('', $this->storage->getId());
|
||||
$this->storage->start();
|
||||
$id = $this->storage->getId();
|
||||
$this->assertNotEquals('', $id);
|
||||
$this->storage->start();
|
||||
$this->assertEquals($id, $this->storage->getId());
|
||||
}
|
||||
|
||||
public function testRegenerate()
|
||||
{
|
||||
$this->storage->start();
|
||||
$id = $this->storage->getId();
|
||||
$this->storage->regenerate();
|
||||
$this->assertNotEquals($id, $this->storage->getId());
|
||||
$this->assertEquals(array('foo' => 'bar'), $this->storage->getBag('attributes')->all());
|
||||
$this->assertEquals(array('notice' => 'hello'), $this->storage->getBag('flashes')->peekAll());
|
||||
|
||||
$id = $this->storage->getId();
|
||||
$this->storage->regenerate(true);
|
||||
$this->assertNotEquals($id, $this->storage->getId());
|
||||
$this->assertEquals(array('foo' => 'bar'), $this->storage->getBag('attributes')->all());
|
||||
$this->assertEquals(array('notice' => 'hello'), $this->storage->getBag('flashes')->peekAll());
|
||||
}
|
||||
|
||||
public function testGetId()
|
||||
{
|
||||
$this->assertEquals('', $this->storage->getId());
|
||||
$this->storage->start();
|
||||
$this->assertNotEquals('', $this->storage->getId());
|
||||
}
|
||||
}
|
@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Test\Component\HttpFoundation\Session\Storage;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
|
||||
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
|
||||
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
|
||||
|
||||
/**
|
||||
* Test class for MockFileSessionStorage.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $sessionDir;
|
||||
|
||||
/**
|
||||
* @var FileMockSessionStorage
|
||||
*/
|
||||
protected $storage;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->sessionDir = sys_get_temp_dir().'/sf2test';
|
||||
$this->storage = $this->getStorage();
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
$this->sessionDir = null;
|
||||
$this->storage = null;
|
||||
array_map('unlink', glob($this->sessionDir.'/*.session'));
|
||||
if (is_dir($this->sessionDir)) {
|
||||
rmdir($this->sessionDir);
|
||||
}
|
||||
}
|
||||
|
||||
public function testStart()
|
||||
{
|
||||
$this->assertEquals('', $this->storage->getId());
|
||||
$this->assertTrue($this->storage->start());
|
||||
$id = $this->storage->getId();
|
||||
$this->assertNotEquals('', $this->storage->getId());
|
||||
$this->assertTrue($this->storage->start());
|
||||
$this->assertEquals($id, $this->storage->getId());
|
||||
}
|
||||
|
||||
public function testRegenerate()
|
||||
{
|
||||
$this->storage->start();
|
||||
$this->storage->getBag('attributes')->set('regenerate', 1234);
|
||||
$this->storage->regenerate();
|
||||
$this->assertEquals(1234, $this->storage->getBag('attributes')->get('regenerate'));
|
||||
$this->storage->regenerate(true);
|
||||
$this->assertEquals(1234, $this->storage->getBag('attributes')->get('regenerate'));
|
||||
}
|
||||
|
||||
public function testGetId()
|
||||
{
|
||||
$this->assertEquals('', $this->storage->getId());
|
||||
$this->storage->start();
|
||||
$this->assertNotEquals('', $this->storage->getId());
|
||||
}
|
||||
|
||||
public function testSave()
|
||||
{
|
||||
$this->storage->start();
|
||||
$this->assertNotEquals('108', $this->storage->getBag('attributes')->get('new'));
|
||||
$this->assertFalse($this->storage->getBag('flashes')->has('newkey'));
|
||||
$this->storage->getBag('attributes')->set('new', '108');
|
||||
$this->storage->getBag('flashes')->set('newkey', 'test');
|
||||
$this->storage->save();
|
||||
|
||||
$storage = $this->getStorage();
|
||||
$storage->start();
|
||||
$this->assertEquals('108', $storage->getBag('attributes')->get('new'));
|
||||
$this->assertTrue($storage->getBag('flashes')->has('newkey'));
|
||||
$this->assertEquals('test', $storage->getBag('flashes')->peek('newkey'));
|
||||
}
|
||||
|
||||
public function testMultipleInstances()
|
||||
{
|
||||
$storage1 = $this->getStorage();
|
||||
$storage1->start();
|
||||
$storage1->getBag('attributes')->set('foo', 'bar');
|
||||
$storage1->save();
|
||||
|
||||
$storage2 = $this->getStorage();
|
||||
$storage2->start();
|
||||
$this->assertEquals('bar', $storage2->getBag('attributes')->get('foo'), 'values persist between instances');
|
||||
}
|
||||
|
||||
private function getStorage(array $options = array())
|
||||
{
|
||||
$storage = new MockFileSessionStorage($this->sessionDir, $options);
|
||||
$storage->registerBag(new FlashBag);
|
||||
$storage->registerBag(new AttributeBag);
|
||||
|
||||
return $storage;
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Tests\Component\HttpFoundation\Session\Storage;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\NativeFileSessionStorage;
|
||||
|
||||
/**
|
||||
* Test class for NativeFileSessionStorage.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*
|
||||
* @runTestsInSeparateProcesses
|
||||
*/
|
||||
class NativeFileSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testSaveHandlers()
|
||||
{
|
||||
$storage = new NativeFileSessionStorage(sys_get_temp_dir(), array('name' => 'TESTING'));
|
||||
$this->assertEquals('files', ini_get('session.save_handler'));
|
||||
$this->assertEquals(sys_get_temp_dir(), ini_get('session.save_path'));
|
||||
$this->assertEquals('TESTING', ini_get('session.name'));
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Tests\Component\HttpFoundation\Session\Storage;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\NativeMemcacheSessionStorage;
|
||||
|
||||
/**
|
||||
* Test class for NativeMemcacheSessionStorage.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*
|
||||
* @runTestsInSeparateProcesses
|
||||
*/
|
||||
class NativeMemcacheSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testSaveHandlers()
|
||||
{
|
||||
if (!extension_loaded('memcache')) {
|
||||
$this->markTestSkipped('Skipped tests SQLite extension is not present');
|
||||
}
|
||||
|
||||
$storage = new NativeMemcacheSessionStorage('tcp://127.0.0.1:11211?persistent=0', array('name' => 'TESTING'));
|
||||
$this->assertEquals('memcache', ini_get('session.save_handler'));
|
||||
$this->assertEquals('tcp://127.0.0.1:11211?persistent=0', ini_get('session.save_path'));
|
||||
$this->assertEquals('TESTING', ini_get('session.name'));
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Tests\Component\HttpFoundation\Session\Storage;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\NativeMemcachedSessionStorage;
|
||||
|
||||
/**
|
||||
* Test class for NativeMemcachedSessionStorage.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*
|
||||
* @runTestsInSeparateProcesses
|
||||
*/
|
||||
class NativeMemcachedSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testSaveHandlers()
|
||||
{
|
||||
if (!extension_loaded('memcached')) {
|
||||
$this->markTestSkipped('Skipped tests SQLite extension is not present');
|
||||
}
|
||||
|
||||
// test takes too long if memcached server is not running
|
||||
ini_set('memcached.sess_locking', '0');
|
||||
|
||||
$storage = new NativeMemcachedSessionStorage('127.0.0.1:11211', array('name' => 'TESTING'));
|
||||
|
||||
$this->assertEquals('memcached', ini_get('session.save_handler'));
|
||||
$this->assertEquals('127.0.0.1:11211', ini_get('session.save_path'));
|
||||
$this->assertEquals('TESTING', ini_get('session.name'));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Tests\Component\HttpFoundation\Session\Storage;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\NativeSqliteSessionStorage;
|
||||
|
||||
/**
|
||||
* Test class for NativeSqliteSessionStorage.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*
|
||||
* @runTestsInSeparateProcesses
|
||||
*/
|
||||
class NativeSqliteSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testSaveHandlers()
|
||||
{
|
||||
if (!extension_loaded('sqlite')) {
|
||||
$this->markTestSkipped('Skipped tests SQLite extension is not present');
|
||||
}
|
||||
|
||||
$storage = new NativeSqliteSessionStorage(sys_get_temp_dir().'/sqlite.db', array('name' => 'TESTING'));
|
||||
$this->assertEquals('sqlite', ini_get('session.save_handler'));
|
||||
$this->assertEquals(sys_get_temp_dir().'/sqlite.db', ini_get('session.save_path'));
|
||||
$this->assertEquals('TESTING', ini_get('session.name'));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Tests\Component\HttpFoundation\Session\Storage;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\NullSessionStorage;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
|
||||
/**
|
||||
* Test class for NullSessionStorage.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*
|
||||
* @runTestsInSeparateProcesses
|
||||
*/
|
||||
class NullSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testSaveHandlers()
|
||||
{
|
||||
$storage = new NullSessionStorage();
|
||||
$this->assertEquals('user', ini_get('session.save_handler'));
|
||||
}
|
||||
|
||||
public function testSession()
|
||||
{
|
||||
session_id('nullsessionstorage');
|
||||
$storage = new NullSessionStorage();
|
||||
$session = new Session($storage);
|
||||
$this->assertNull($session->get('something'));
|
||||
$session->set('something', 'unique');
|
||||
$this->assertEquals('unique', $session->get('something'));
|
||||
}
|
||||
|
||||
public function testNothingIsPersisted()
|
||||
{
|
||||
session_id('nullsessionstorage');
|
||||
$storage = new NullSessionStorage();
|
||||
$session = new Session($storage);
|
||||
$session->start();
|
||||
$this->assertEquals('nullsessionstorage', $session->getId());
|
||||
$this->assertNull($session->get('something'));
|
||||
}
|
||||
}
|
||||
|
@ -1,104 +0,0 @@
|
||||
<?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\Tests\Component\HttpFoundation\SessionStorage;
|
||||
|
||||
use Symfony\Component\HttpFoundation\SessionStorage\FilesystemSessionStorage;
|
||||
|
||||
class FilesystemSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
private $path;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->path = sys_get_temp_dir().'/sf2/session_test';
|
||||
if (!file_exists($this->path)) {
|
||||
mkdir($this->path, 0777, true);
|
||||
}
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
array_map('unlink', glob($this->path.'/*.session'));
|
||||
rmdir($this->path);
|
||||
|
||||
$this->path = null;
|
||||
}
|
||||
|
||||
public function testMultipleInstances()
|
||||
{
|
||||
$storage1 = new FilesystemSessionStorage($this->path);
|
||||
$storage1->start();
|
||||
$storage1->write('foo', 'bar');
|
||||
|
||||
$storage2 = new FilesystemSessionStorage($this->path);
|
||||
$storage2->start();
|
||||
$this->assertEquals('bar', $storage2->read('foo'), 'values persist between instances');
|
||||
}
|
||||
|
||||
public function testGetIdThrowsErrorBeforeStart()
|
||||
{
|
||||
$this->setExpectedException('RuntimeException');
|
||||
|
||||
$storage = new FilesystemSessionStorage($this->path);
|
||||
$storage->getId();
|
||||
}
|
||||
|
||||
public function testGetIdWorksAfterStart()
|
||||
{
|
||||
$storage = new FilesystemSessionStorage($this->path);
|
||||
$storage->start();
|
||||
$storage->getId();
|
||||
}
|
||||
|
||||
public function testGetIdSetByOptions()
|
||||
{
|
||||
$previous = ini_get('session.use_cookies');
|
||||
|
||||
ini_set('session.use_cookies', false);
|
||||
|
||||
$storage = new FilesystemSessionStorage($this->path, array('id' => 'symfony2-sessionId'));
|
||||
$storage->start();
|
||||
|
||||
$this->assertEquals('symfony2-sessionId', $storage->getId());
|
||||
|
||||
ini_set('session.use_cookies', $previous);
|
||||
}
|
||||
|
||||
public function testRemoveVariable()
|
||||
{
|
||||
$storage = new FilesystemSessionStorage($this->path);
|
||||
$storage->start();
|
||||
|
||||
$storage->write('foo', 'bar');
|
||||
|
||||
$this->assertEquals('bar', $storage->read('foo'));
|
||||
|
||||
$storage->remove('foo', 'bar');
|
||||
|
||||
$this->assertNull($storage->read('foo'));
|
||||
}
|
||||
|
||||
public function testRegenerate()
|
||||
{
|
||||
$storage = new FilesystemSessionStorage($this->path);
|
||||
$storage->start();
|
||||
$storage->write('foo', 'bar');
|
||||
|
||||
$storage->regenerate();
|
||||
|
||||
$this->assertEquals('bar', $storage->read('foo'));
|
||||
|
||||
$storage->regenerate(true);
|
||||
|
||||
$this->assertNull($storage->read('foo'));
|
||||
}
|
||||
}
|
@ -1,232 +0,0 @@
|
||||
<?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\Tests\Component\HttpFoundation;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session;
|
||||
use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
|
||||
|
||||
/**
|
||||
* SessionTest
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Robert Schönthal <seroscho@googlemail.com>
|
||||
*/
|
||||
class SessionTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $storage;
|
||||
protected $session;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->storage = new ArraySessionStorage();
|
||||
$this->session = $this->getSession();
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
$this->storage = null;
|
||||
$this->session = null;
|
||||
}
|
||||
|
||||
public function testFlash()
|
||||
{
|
||||
$this->session->clearFlashes();
|
||||
|
||||
$this->assertSame(array(), $this->session->getFlashes());
|
||||
|
||||
$this->assertFalse($this->session->hasFlash('foo'));
|
||||
|
||||
$this->session->setFlash('foo', 'bar');
|
||||
|
||||
$this->assertTrue($this->session->hasFlash('foo'));
|
||||
$this->assertSame('bar', $this->session->getFlash('foo'));
|
||||
|
||||
$this->session->removeFlash('foo');
|
||||
|
||||
$this->assertFalse($this->session->hasFlash('foo'));
|
||||
|
||||
$flashes = array('foo' => 'bar', 'bar' => 'foo');
|
||||
|
||||
$this->session->setFlashes($flashes);
|
||||
|
||||
$this->assertSame($flashes, $this->session->getFlashes());
|
||||
}
|
||||
|
||||
public function testFlashesAreFlushedWhenNeeded()
|
||||
{
|
||||
$this->session->setFlash('foo', 'bar');
|
||||
$this->session->save();
|
||||
|
||||
$this->session = $this->getSession();
|
||||
$this->assertTrue($this->session->hasFlash('foo'));
|
||||
$this->session->save();
|
||||
|
||||
$this->session = $this->getSession();
|
||||
$this->assertFalse($this->session->hasFlash('foo'));
|
||||
}
|
||||
|
||||
public function testAll()
|
||||
{
|
||||
$this->assertFalse($this->session->has('foo'));
|
||||
$this->assertNull($this->session->get('foo'));
|
||||
|
||||
$this->session->set('foo', 'bar');
|
||||
|
||||
$this->assertTrue($this->session->has('foo'));
|
||||
$this->assertSame('bar', $this->session->get('foo'));
|
||||
|
||||
$this->session = $this->getSession();
|
||||
|
||||
$this->session->remove('foo');
|
||||
$this->session->set('foo', 'bar');
|
||||
|
||||
$this->session->remove('foo');
|
||||
|
||||
$this->assertFalse($this->session->has('foo'));
|
||||
|
||||
$attrs = array('foo' => 'bar', 'bar' => 'foo');
|
||||
|
||||
$this->session = $this->getSession();
|
||||
|
||||
$this->session->replace($attrs);
|
||||
|
||||
$this->assertSame($attrs, $this->session->all());
|
||||
|
||||
$this->session->clear();
|
||||
|
||||
$this->assertSame(array(), $this->session->all());
|
||||
}
|
||||
|
||||
public function testMigrateAndInvalidate()
|
||||
{
|
||||
$this->session->set('foo', 'bar');
|
||||
$this->session->setFlash('foo', 'bar');
|
||||
|
||||
$this->assertSame('bar', $this->session->get('foo'));
|
||||
$this->assertSame('bar', $this->session->getFlash('foo'));
|
||||
|
||||
$this->session->migrate();
|
||||
|
||||
$this->assertSame('bar', $this->session->get('foo'));
|
||||
$this->assertSame('bar', $this->session->getFlash('foo'));
|
||||
|
||||
$this->session = $this->getSession();
|
||||
$this->session->invalidate();
|
||||
|
||||
$this->assertSame(array(), $this->session->all());
|
||||
$this->assertSame(array(), $this->session->getFlashes());
|
||||
}
|
||||
|
||||
public function testSerialize()
|
||||
{
|
||||
$this->session = new Session($this->storage);
|
||||
|
||||
$compare = serialize($this->storage);
|
||||
|
||||
$this->assertSame($compare, $this->session->serialize());
|
||||
|
||||
$this->session->unserialize($compare);
|
||||
|
||||
$_storage = new \ReflectionProperty(get_class($this->session), 'storage');
|
||||
$_storage->setAccessible(true);
|
||||
|
||||
$this->assertEquals($_storage->getValue($this->session), $this->storage, 'storage match');
|
||||
}
|
||||
|
||||
public function testSave()
|
||||
{
|
||||
$this->storage = new ArraySessionStorage();
|
||||
$this->session = new Session($this->storage);
|
||||
$this->session->set('foo', 'bar');
|
||||
|
||||
$this->session->save();
|
||||
$compare = array('_symfony2' => array('attributes' => array('foo' => 'bar'), 'flashes' => array()));
|
||||
|
||||
$r = new \ReflectionObject($this->storage);
|
||||
$p = $r->getProperty('data');
|
||||
$p->setAccessible(true);
|
||||
|
||||
$this->assertSame($p->getValue($this->storage), $compare);
|
||||
}
|
||||
|
||||
public function testGetId()
|
||||
{
|
||||
$this->assertNull($this->session->getId());
|
||||
}
|
||||
|
||||
public function testStart()
|
||||
{
|
||||
$this->session->start();
|
||||
|
||||
$this->assertSame(array(), $this->session->getFlashes());
|
||||
$this->assertSame(array(), $this->session->all());
|
||||
}
|
||||
|
||||
public function testSavedOnDestruct()
|
||||
{
|
||||
$this->session->set('foo', 'bar');
|
||||
|
||||
$this->session->__destruct();
|
||||
|
||||
$expected = array(
|
||||
'attributes'=>array('foo'=>'bar'),
|
||||
'flashes'=>array(),
|
||||
);
|
||||
$saved = $this->storage->read('_symfony2');
|
||||
$this->assertSame($expected, $saved);
|
||||
}
|
||||
|
||||
public function testSavedOnDestructAfterManualSave()
|
||||
{
|
||||
$this->session->set('foo', 'nothing');
|
||||
$this->session->save();
|
||||
$this->session->set('foo', 'bar');
|
||||
|
||||
$this->session->__destruct();
|
||||
|
||||
$expected = array(
|
||||
'attributes'=>array('foo'=>'bar'),
|
||||
'flashes'=>array(),
|
||||
);
|
||||
$saved = $this->storage->read('_symfony2');
|
||||
$this->assertSame($expected, $saved);
|
||||
}
|
||||
|
||||
public function testStorageRegenerate()
|
||||
{
|
||||
$this->storage->write('foo', 'bar');
|
||||
|
||||
$this->assertTrue($this->storage->regenerate());
|
||||
|
||||
$this->assertEquals('bar', $this->storage->read('foo'));
|
||||
|
||||
$this->assertTrue($this->storage->regenerate(true));
|
||||
|
||||
$this->assertNull($this->storage->read('foo'));
|
||||
}
|
||||
|
||||
public function testStorageRemove()
|
||||
{
|
||||
$this->storage->write('foo', 'bar');
|
||||
|
||||
$this->assertEquals('bar', $this->storage->read('foo'));
|
||||
|
||||
$this->storage->remove('foo');
|
||||
|
||||
$this->assertNull($this->storage->read('foo'));
|
||||
}
|
||||
|
||||
protected function getSession()
|
||||
{
|
||||
return new Session($this->storage);
|
||||
}
|
||||
}
|
@ -33,7 +33,7 @@ class LocaleListenerTest extends \PHPUnit_Framework_TestCase
|
||||
session_name('foo');
|
||||
$request->cookies->set('foo', 'value');
|
||||
|
||||
$session = $this->getMock('Symfony\Component\HttpFoundation\Session', array('get'), array(), '', false);
|
||||
$session = $this->getMock('Symfony\Component\HttpFoundation\Session\Session', array('get'), array(), '', false);
|
||||
$session->expects($this->once())->method('get')->will($this->returnValue('es'));
|
||||
$request->setSession($session);
|
||||
|
||||
@ -55,7 +55,7 @@ class LocaleListenerTest extends \PHPUnit_Framework_TestCase
|
||||
$event = $this->getEvent($request);
|
||||
|
||||
// also updates the session _locale value
|
||||
$session = $this->getMock('Symfony\Component\HttpFoundation\Session', array('set', 'get'), array(), '', false);
|
||||
$session = $this->getMock('Symfony\Component\HttpFoundation\Session\Session', array('set', 'get'), array(), '', false);
|
||||
$session->expects($this->once())->method('set')->with('_locale', 'es');
|
||||
$session->expects($this->once())->method('get')->with('_locale')->will($this->returnValue('es'));
|
||||
$request->setSession($session);
|
||||
|
@ -4,8 +4,8 @@ namespace Symfony\Test\Component\Security\Http\Firewall;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\Session;
|
||||
use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
||||
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
||||
@ -63,7 +63,7 @@ class ContextListenerTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
protected function runSessionOnKernelResponse($newToken, $original = null)
|
||||
{
|
||||
$session = new Session(new ArraySessionStorage());
|
||||
$session = new Session(new MockArraySessionStorage());
|
||||
|
||||
if ($original !== null) {
|
||||
$session->set('_security_session', $original);
|
||||
|
@ -22,7 +22,7 @@ class SessionLogoutHandlerTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
|
||||
$response = new Response();
|
||||
$session = $this->getMock('Symfony\Component\HttpFoundation\Session', array(), array(), '', false);
|
||||
$session = $this->getMock('Symfony\Component\HttpFoundation\Session\Session', array(), array(), '', false);
|
||||
|
||||
$request
|
||||
->expects($this->once())
|
||||
|
Reference in New Issue
Block a user