Commit
- Loading branch information
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,8 @@ | |
<parameter key="session.storage.native.options" type="collection" /> | ||
<parameter key="session.storage.pdo.class">Symfony\Component\HttpFoundation\SessionStorage\PdoSessionStorage</parameter> | ||
<parameter key="session.storage.pdo.options" type="collection" /> | ||
<parameter key="session.storage.array.class">Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage</parameter> | ||
<parameter key="session.storage.array.options" type="collection" /> | ||
<parameter key="session.storage.filesystem.class">Symfony\Component\HttpFoundation\SessionStorage\FilesystemSessionStorage</parameter> | ||
<parameter key="session.storage.filesystem.options" type="collection" /> | ||
</parameters> | ||
|
||
<services> | ||
|
@@ -29,8 +29,9 @@ | |
<argument>%session.storage.pdo.options%</argument> | ||
</service> | ||
|
||
<service id="session.storage.array" class="%session.storage.array.class%" public="false"> | ||
<argument>%session.storage.array.options%</argument> | ||
<service id="session.storage.filesystem" class="%session.storage.filesystem.class%" public="false"> | ||
<argument>%kernel.cache_dir%/sessions</argument> | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
sstok
Contributor
|
||
<argument>%session.storage.filesystem.options%</argument> | ||
</service> | ||
|
||
<service id="session.storage" alias="session.storage.native" public="false" /> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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\Bundle\FrameworkBundle\Test; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\Cookie; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; | ||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* SessionListener. | ||
* | ||
* Saves session in test environment. | ||
* | ||
* @author Bulat Shakirzyanov <mallluhuct@gmail.com> | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class SessionListener | ||
{ | ||
protected $container; | ||
|
||
public function __construct(ContainerInterface $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
public function onCoreRequest(GetResponseEvent $event) | ||
{ | ||
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { | ||
return; | ||
} | ||
|
||
// bootstrap the session | ||
if ($this->container->has('session')) { | ||
$this->container->get('session'); | ||
} | ||
|
||
$cookies = $event->getRequest()->cookies; | ||
if ($cookies->has(session_name())) { | ||
session_id($cookies->get(session_name())); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if session was initialized and saves if current request is master | ||
* Runs on 'onCoreResponse' in test environment | ||
* | ||
* @param FilterResponseEvent $event | ||
*/ | ||
public function onCoreResponse(FilterResponseEvent $event) | ||
{ | ||
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { | ||
return; | ||
} | ||
|
||
if ($session = $event->getRequest()->getSession()) { | ||
$session->save(); | ||
|
||
$params = session_get_cookie_params(); | ||
|
||
$event->getResponse()->headers->setCookie(new Cookie(session_name(), session_id(), time() + $params['lifetime'], $params['path'], $params['domain'], $params['secure'], $params['httponly'])); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?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. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class FilesystemSessionStorage extends NativeSessionStorage | ||
{ | ||
private $path; | ||
private $data; | ||
|
||
public function __construct($path, array $options = array()) | ||
{ | ||
$this->path = $path; | ||
|
||
parent::__construct($options); | ||
} | ||
|
||
public function start() | ||
{ | ||
if (self::$sessionStarted) { | ||
This comment has been minimized.
Sorry, something went wrong.
kriswallsmith
Contributor
|
||
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') && $this->options['id'] && $this->options['id'] != session_id()) { | ||
session_id($this->options['id']); | ||
} | ||
|
||
if (!session_id()) { | ||
session_id(hash('md5', uniqid(mt_rand(), true))); | ||
} | ||
|
||
if (!is_dir($this->path)) { | ||
mkdir($this->path, 0777, true); | ||
} | ||
|
||
$file = $this->path.'/'.session_id().'.session'; | ||
|
||
$this->data = file_exists($file) ? unserialize(file_get_contents($file)) : array(); | ||
} | ||
|
||
public function read($key, $default = null) | ||
{ | ||
return array_key_exists($key, $this->data) ? $this->data[$key] : $default; | ||
} | ||
|
||
public function remove($key) | ||
{ | ||
$retval = $this->data[$key]; | ||
|
||
unset($this->data[$key]); | ||
|
||
return $retval; | ||
} | ||
|
||
public function write($key, $data) | ||
{ | ||
$this->data[$key] = $data; | ||
|
||
file_put_contents($this->path.'/'.session_id().'.session', serialize($this->data)); | ||
} | ||
|
||
public function regenerate($destroy = false) | ||
{ | ||
if ($destroy) { | ||
$this->data = array(); | ||
} | ||
return true; | ||
} | ||
} |
1 comment
on commit ea84bb0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is huge, thank you for that
@fabpot : what is the rationale behind this choice ? As a result, every user is logged out on every delivery, that's why I'm asking. Maybe there should be a dedicated directory outside the cache (
app/data/sessions
?var/sessions
? )?