diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index 614364294..5e4e30140 100644 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -100,6 +100,10 @@ + + + + diff --git a/core/lib/Thelia/Core/Event/SessionEvent.php b/core/lib/Thelia/Core/Event/SessionEvent.php new file mode 100644 index 000000000..6def3ac01 --- /dev/null +++ b/core/lib/Thelia/Core/Event/SessionEvent.php @@ -0,0 +1,75 @@ + + */ +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; + } + + +} \ No newline at end of file diff --git a/core/lib/Thelia/Core/EventListener/SessionListener.php b/core/lib/Thelia/Core/EventListener/SessionListener.php new file mode 100644 index 000000000..f8d1f809d --- /dev/null +++ b/core/lib/Thelia/Core/EventListener/SessionListener.php @@ -0,0 +1,82 @@ + + */ +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] + ] + ]; + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Core/TheliaHttpKernel.php b/core/lib/Thelia/Core/TheliaHttpKernel.php index 060871a22..2bf5a0004 100644 --- a/core/lib/Thelia/Core/TheliaHttpKernel.php +++ b/core/lib/Thelia/Core/TheliaHttpKernel.php @@ -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(); diff --git a/core/lib/Thelia/Core/TheliaKernelEvents.php b/core/lib/Thelia/Core/TheliaKernelEvents.php new file mode 100644 index 000000000..d1d3fe276 --- /dev/null +++ b/core/lib/Thelia/Core/TheliaKernelEvents.php @@ -0,0 +1,26 @@ + + */ +final class TheliaKernelEvents +{ + + const SESSION = "thelia_kernel.session"; + +} \ No newline at end of file