create a listener for adding the cart cookie in the response object

This commit is contained in:
Manuel Raynaud
2014-04-17 16:47:47 +02:00
parent 6ae0d02142
commit ec318376f5
4 changed files with 94 additions and 4 deletions

View File

@@ -97,7 +97,7 @@ trait CartTrait
protected function createCart(Session $session)
{
$cart = new CartModel();
$cart->setToken($this->generateCookie());
$cart->setToken($this->generateCookie($session));
if (null !== $customer = $session->getCustomerUser()) {
$cart->setCustomer($customer);
@@ -120,7 +120,7 @@ trait CartTrait
*/
protected function duplicateCart(EventDispatcherInterface $dispatcher, CartModel $cart, Session $session, Customer $customer = null)
{
$newCart = $cart->duplicate($this->generateCookie(), $customer, $dispatcher);
$newCart = $cart->duplicate($this->generateCookie($session), $customer, $dispatcher);
$session->setCart($newCart->getId());
$cartEvent = new CartEvent($newCart);
@@ -130,11 +130,13 @@ trait CartTrait
return $cartEvent->getCart();
}
protected function generateCookie()
protected function generateCookie(Session $session)
{
$id = null;
if (ConfigQuery::read("cart.session_only", 0) == 0) {
$id = uniqid('', true);
$session->set('cart_use_cookie', $id);
setcookie(
"thelia_cart",
$id,

View File

@@ -96,6 +96,10 @@
<service id="mailer" class="Thelia\Mailer\MailerFactory">
<argument type="service" id="event_dispatcher"/>
</service>
<service id="response.listener" class="Thelia\Core\EventListener\ResponseListener">
<tag name="kernel.event_subscriber"/>
</service>
</services>
</config>

View File

@@ -0,0 +1,84 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Core\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Thelia\Model\ConfigQuery;
/**
* Class ResponseListener
* @package Thelia\Core\EventListener
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class ResponseListener implements EventSubscriberInterface
{
public function beforeResponse(FilterResponseEvent $event)
{
$session = $event->getRequest()->getSession();
if (null !== $id = $session->get("cart_use_cookie")) {
$response = $event->getResponse();
$response->headers->setCookie(new Cookie(
"thelia_cart",
$id,
time()+ConfigQuery::read("cart.cookie_lifetime", 60*60*24*365),
'/'
));
$session->set("cart_use_cookie", null);
}
}
/**
* 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 [
KernelEvents::RESPONSE => ['beforeResponse', 128]
];
}
}