session is now initialize using dispatcher

This commit is contained in:
Manuel Raynaud
2014-05-20 09:44:04 +02:00
parent ec50b30ff6
commit 2ace0796e8
5 changed files with 194 additions and 19 deletions

View File

@@ -100,6 +100,10 @@
<service id="response.listener" class="Thelia\Core\EventListener\ResponseListener">
<tag name="kernel.event_subscriber"/>
</service>
<service id="session.listener" class="Thelia\Core\EventListener\SessionListener">
<tag name="kernel.event_subscriber"/>
</service>
</services>
</config>

View File

@@ -0,0 +1,75 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Core\Event;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
/**
* Class SessionEvent
* @package Thelia\Core\Event
* @author manuel raynaud <mraynaud@openstudio.fr>
*/
class SessionEvent extends ActionEvent
{
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;
protected $session;
/**
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
/**
* @return ContainerInterface
*/
public function getContainer()
{
return $this->container;
}
public function getEnv()
{
return $this->container->getParameter('kernel.environment');
}
public function getDebug()
{
return $this->container->getParameter('kernel.debug');
}
/**
* @param mixed $session
*/
public function setSession(SessionInterface $session)
{
$this->session = $session;
}
/**
* @return mixed
*/
public function getSession()
{
return $this->session;
}
}

View File

@@ -0,0 +1,82 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Core\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler;
use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
use Thelia\Core\Event\SessionEvent;
use Thelia\Core\HttpFoundation\Session\Session;
use Thelia\Core\TheliaKernelEvents;
use Thelia\Model\ConfigQuery;
/**
* Class SessionListener
* @package Thelia\Core\EventListener
* @author manuel raynaud <mraynaud@openstudio.fr>
*/
class SessionListener implements EventSubscriberInterface
{
public function prodSession(SessionEvent $event)
{
$storage = new NativeSessionStorage();
$storage->setSaveHandler(new NativeFileSessionHandler(ConfigQuery::read("session_config.save_path", THELIA_ROOT . '/local/session/')));
$event->setSession($this->getSession($storage));
}
public function testSession(SessionEvent $event)
{
if ($event->getEnv() == 'test') {
$storage = new MockFileSessionStorage($event->getContainer()->getParameter('kernel.cache_dir') . DS . 'sessions');
$event->setSession($this->getSession($storage));
$event->stopPropagation();
}
}
public function getSession(SessionStorageInterface $storage)
{
return new Session($storage);
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * array('eventName' => 'methodName')
* * array('eventName' => array('methodName', $priority))
* * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
*
* @return array The event names to listen to
*
* @api
*/
public static function getSubscribedEvents()
{
return [
TheliaKernelEvents::SESSION =>[
['prodSession', 0],
['testSession', 128]
]
];
}
}

View File

@@ -23,6 +23,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Session;
use Thelia\Core\Event\Currency\CurrencyChangeEvent;
use Thelia\Core\Event\SessionEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Model;
@@ -203,26 +204,13 @@ class TheliaHttpKernel extends HttpKernel
public function initSession(Request $request)
{
if (null === self::$session) {
$storage = new Session\Storage\NativeSessionStorage();
if (null === $session = self::$session) {
$container = $this->getContainer();
$event = new SessionEvent($this->container);
$dispatcher = $container->get('event_dispatcher');
$dispatcher->dispatch(TheliaKernelEvents::SESSION, $event);
if (Model\ConfigQuery::read("session_config.default")) {
$storage->setSaveHandler(new Session\Storage\Handler\NativeFileSessionHandler(Model\ConfigQuery::read("session_config.save_path", THELIA_ROOT . '/local/session/')));
} else {
$handlerString = Model\ConfigQuery::read("session_config.handlers", 'Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler');
$handler = new $handlerString;
$storage->setSaveHandler($handler);
}
if (Model\ConfigQuery::read("session_config.config", null)) {
$storage->setOptions(json_decode(Model\ConfigQuery::read("session_config.config")));
}
self::$session = $session = new \Thelia\Core\HttpFoundation\Session\Session($storage);
} else {
$session = self::$session;
self::$session = $session = $event->getSession();
}
$session->start();

View File

@@ -0,0 +1,26 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Core;
/**
* Class TheliaKernelEvents
* @package Thelia\Core
* @author manuel raynaud <mraynaud@openstudio.fr>
*/
final class TheliaKernelEvents
{
const SESSION = "thelia_kernel.session";
}