mirror of
https://github.com/Sylius/Sylius.git
synced 2026-07-05 20:57:12 +00:00
major refactoring.
This commit is contained in:
parent
8fd349e3c6
commit
b212c8afb3
23 changed files with 138 additions and 166 deletions
|
|
@ -92,9 +92,10 @@ class CartController extends ContainerAware
|
|||
$cartOperator->removeItem($cart, $item);
|
||||
$cartOperator->refreshCart($cart);
|
||||
|
||||
$this->container->get('sylius_cart.manager.cart')->persistCart($cart);
|
||||
$itemManager->removeItem($item);
|
||||
|
||||
$this->container->get('sylius_cart.manager.cart')->persistCart($cart);
|
||||
|
||||
return new RedirectResponse($this->container->get('router')->generate('sylius_cart_show'));
|
||||
}
|
||||
|
||||
|
|
@ -104,6 +105,7 @@ class CartController extends ContainerAware
|
|||
public function updateAction()
|
||||
{
|
||||
$cart = $this->container->get('sylius_cart.provider')->getCart();
|
||||
|
||||
$form = $this->container->get('form.factory')->create($this->container->get('sylius_cart.form.type.cart'));
|
||||
$form->setData($cart);
|
||||
$form->bindRequest($this->container->get('request'));
|
||||
|
|
@ -111,15 +113,21 @@ class CartController extends ContainerAware
|
|||
if ($form->isValid()) {
|
||||
$cartOperator = $this->container->get('sylius_cart.operator');
|
||||
|
||||
$items = $cart->getItems();
|
||||
$existingItems = array();
|
||||
|
||||
foreach ($cart->getItems() as $item) {
|
||||
$existingItems[] = $item;
|
||||
}
|
||||
|
||||
$cart->clearItems();
|
||||
|
||||
foreach($items as $item) {
|
||||
foreach($existingItems as $item) {
|
||||
$cartOperator->addItem($cart, $item);
|
||||
}
|
||||
|
||||
$this->container->get('event_dispatcher')->dispatch(SyliusCartEvents::CART_UPDATE, new FilterCartEvent($cart));
|
||||
$cartOperator->refreshCart($cart);
|
||||
|
||||
$this->container->get('sylius_cart.manager.cart')->persistCart($cart);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -96,7 +96,6 @@ class Configuration implements ConfigurationInterface
|
|||
->addDefaultsIfNotSet()
|
||||
->children()
|
||||
->scalarNode('request')->defaultValue('Sylius\Bundle\\CartBundle\\EventDispatcher\\Listener\\RequestListener')->end()
|
||||
->scalarNode('response')->defaultValue('Sylius\Bundle\\CartBundle\\EventDispatcher\\Listener\\ResponseListener')->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
|
|
|
|||
|
|
@ -11,6 +11,8 @@
|
|||
|
||||
namespace Sylius\Bundle\CartBundle\Entity;
|
||||
|
||||
use Sylius\Bundle\CartBundle\Model\ItemInterface;
|
||||
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Sylius\Bundle\CartBundle\Model\Cart as BaseCart;
|
||||
|
||||
|
|
@ -21,4 +23,36 @@ use Sylius\Bundle\CartBundle\Model\Cart as BaseCart;
|
|||
*/
|
||||
class Cart extends BaseCart
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->items = new ArrayCollection;
|
||||
}
|
||||
|
||||
public function addItem(ItemInterface $item)
|
||||
{
|
||||
if (!$this->hasItem($item)) {
|
||||
$this->items->add($item);
|
||||
$item->setCart($this);
|
||||
}
|
||||
}
|
||||
|
||||
public function removeItem(ItemInterface $item)
|
||||
{
|
||||
if ($this->hasItem($item)) {
|
||||
$this->items->removeElement($item);
|
||||
$item->setCart(null);
|
||||
}
|
||||
}
|
||||
|
||||
public function hasItem(ItemInterface $item)
|
||||
{
|
||||
return $this->items->contains($item);
|
||||
}
|
||||
|
||||
public function clearItems()
|
||||
{
|
||||
$this->items->clear();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ class CartManager extends BaseCartManager
|
|||
*
|
||||
* @var EntityManager
|
||||
*/
|
||||
protected $em;
|
||||
protected $entityManager;
|
||||
|
||||
/**
|
||||
* Carts repository.
|
||||
|
|
@ -38,14 +38,14 @@ class CartManager extends BaseCartManager
|
|||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param EntityManager $em
|
||||
* @param EntityManager $entityManager
|
||||
*/
|
||||
public function __construct(EntityManager $em, $class)
|
||||
public function __construct(EntityManager $entityManager, $class)
|
||||
{
|
||||
parent::__construct($class);
|
||||
|
||||
$this->em = $em;
|
||||
$this->repository = $this->em->getRepository($class);
|
||||
$this->entityManager = $entityManager;
|
||||
$this->repository = $this->entityManager->getRepository($class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -62,8 +62,8 @@ class CartManager extends BaseCartManager
|
|||
*/
|
||||
public function persistCart(CartInterface $cart)
|
||||
{
|
||||
$this->em->persist($cart);
|
||||
$this->em->flush();
|
||||
$this->entityManager->persist($cart);
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -71,8 +71,8 @@ class CartManager extends BaseCartManager
|
|||
*/
|
||||
public function removeCart(CartInterface $cart)
|
||||
{
|
||||
$this->em->remove($cart);
|
||||
$this->em->flush();
|
||||
$this->entityManager->remove($cart);
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1,5 +1,14 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Sylius package.
|
||||
*
|
||||
* (c) Paweł Jędrzejewski
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Sylius\Bundle\CartBundle\Entity;
|
||||
|
||||
use Doctrine\ORM\EntityManager;
|
||||
|
|
@ -13,7 +22,7 @@ class ItemManager extends BaseItemManager
|
|||
*
|
||||
* @var EntityManager
|
||||
*/
|
||||
protected $em;
|
||||
protected $entityManager;
|
||||
|
||||
/**
|
||||
* Items repository.
|
||||
|
|
@ -23,14 +32,14 @@ class ItemManager extends BaseItemManager
|
|||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param EntityManager $em
|
||||
* @param EntityManager $entityManager
|
||||
*/
|
||||
public function __construct(EntityManager $em, $class)
|
||||
public function __construct(EntityManager $entityManager, $class)
|
||||
{
|
||||
parent::__construct($class);
|
||||
|
||||
$this->em = $em;
|
||||
$this->repository = $this->em->getRepository($class);
|
||||
$this->entityManager = $entityManager;
|
||||
$this->repository = $this->entityManager->getRepository($class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -47,8 +56,8 @@ class ItemManager extends BaseItemManager
|
|||
*/
|
||||
public function persistItem(ItemInterface $cart)
|
||||
{
|
||||
$this->em->persist($cart);
|
||||
$this->em->flush();
|
||||
$this->entityManager->persist($cart);
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -56,8 +65,8 @@ class ItemManager extends BaseItemManager
|
|||
*/
|
||||
public function removeItem(ItemInterface $cart)
|
||||
{
|
||||
$this->em->remove($cart);
|
||||
$this->em->flush();
|
||||
$this->entityManager->remove($cart);
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -12,12 +12,16 @@
|
|||
namespace Sylius\Bundle\CartBundle\EventDispatcher\Event;
|
||||
|
||||
use Sylius\Bundle\CartBundle\Model\ItemInterface;
|
||||
|
||||
use Sylius\Bundle\CartBundle\Model\CartInterface;
|
||||
|
||||
/**
|
||||
* Cart operation event.
|
||||
*
|
||||
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
|
||||
*/
|
||||
final class CartOperationEvent extends FilterCartEvent
|
||||
{
|
||||
protected $item;
|
||||
private $item;
|
||||
|
||||
public function __construct(ItemInterface $item, CartInterface $cart)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -14,6 +14,11 @@ namespace Sylius\Bundle\CartBundle\EventDispatcher\Event;
|
|||
use Symfony\Component\EventDispatcher\Event;
|
||||
use Sylius\Bundle\CartBundle\Model\CartInterface;
|
||||
|
||||
/**
|
||||
* Filter cart event.
|
||||
*
|
||||
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
|
||||
*/
|
||||
class FilterCartEvent extends Event
|
||||
{
|
||||
protected $cart;
|
||||
|
|
|
|||
|
|
@ -27,29 +27,24 @@ class RequestListener extends ContainerAware
|
|||
public function onCoreRequest(GetResponseEvent $event)
|
||||
{
|
||||
if(HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
|
||||
if(!$event->getRequest()->isXmlHttpRequest()) {
|
||||
$cartManager = $this->container->get('sylius_cart.manager.cart');
|
||||
|
||||
$cookie = $this->container->get('request')->cookies->get('SYLIUS_CART_HASH');
|
||||
|
||||
if (null === $cookie) {
|
||||
$cart = $cartManager->createCart();
|
||||
$this->container->get('sylius_cart.provider')->setCart($cart);
|
||||
$cartManager->persistCart($cart);
|
||||
return;
|
||||
}
|
||||
|
||||
$cart = $cartManager->findCartBy(array('hash' => $cookie));
|
||||
|
||||
$cartManager = $this->container->get('sylius_cart.manager.cart');
|
||||
$session = $this->container->get('request')->getSession();
|
||||
|
||||
$cartId = $session->get('sylius_cart.id', false);
|
||||
|
||||
if ($cartId) {
|
||||
$cart = $cartManager->findCart($cartId);
|
||||
|
||||
if ($cart) {
|
||||
$this->container->get('sylius_cart.provider')->setCart($cart);
|
||||
return;
|
||||
}
|
||||
|
||||
$cart = $cartManager->createCart();
|
||||
$this->container->get('sylius_cart.provider')->setCart($cart);
|
||||
$cartManager->persistCart($cart);
|
||||
}
|
||||
|
||||
$cart = $cartManager->createCart();
|
||||
$this->container->get('sylius_cart.provider')->setCart($cart);
|
||||
$cartManager->persistCart($cart);
|
||||
$session->set('sylius_cart.id', $cart->getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,13 +32,6 @@ abstract class Cart implements CartInterface
|
|||
*/
|
||||
protected $items;
|
||||
|
||||
/**
|
||||
* Hash.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $hash;
|
||||
|
||||
/**
|
||||
* Total items count.
|
||||
*
|
||||
|
|
@ -46,6 +39,13 @@ abstract class Cart implements CartInterface
|
|||
*/
|
||||
protected $totalItems;
|
||||
|
||||
/**
|
||||
* Locked.
|
||||
*
|
||||
* @var Boolean
|
||||
*/
|
||||
protected $locked;
|
||||
|
||||
/**
|
||||
* Expiration time.
|
||||
*
|
||||
|
|
@ -59,7 +59,7 @@ abstract class Cart implements CartInterface
|
|||
public function __construct()
|
||||
{
|
||||
$this->totalItems = 0;
|
||||
$this->generateHash();
|
||||
$this->locked = false;
|
||||
$this->incrementExpiresAt();
|
||||
}
|
||||
|
||||
|
|
@ -71,43 +71,14 @@ abstract class Cart implements CartInterface
|
|||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getHash()
|
||||
public function isLocked()
|
||||
{
|
||||
return $this->hash;
|
||||
return $this->locked;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setHash($hash)
|
||||
public function setLocked($locked)
|
||||
{
|
||||
$this->hash = $hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function generateHash()
|
||||
{
|
||||
if (null === $this->hash) {
|
||||
$bytes = false;
|
||||
if (function_exists('openssl_random_pseudo_bytes') && 0 !== stripos(PHP_OS, 'win')) {
|
||||
$bytes = openssl_random_pseudo_bytes(32, $strong);
|
||||
|
||||
if (true !== $strong) {
|
||||
$bytes = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (false === $bytes) {
|
||||
$bytes = hash('sha256', uniqid(mt_rand(), true), true);
|
||||
}
|
||||
|
||||
$this->hash = base_convert(bin2hex($bytes), 16, 36);
|
||||
}
|
||||
$this->locked = $locked;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -141,6 +112,11 @@ abstract class Cart implements CartInterface
|
|||
{
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
public function setItems($items)
|
||||
{
|
||||
$this->items = $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
|
@ -169,6 +145,9 @@ abstract class Cart implements CartInterface
|
|||
public function removeItem(ItemInterface $item)
|
||||
{
|
||||
if ($this->hasItem($item)) {
|
||||
$key = array_search($item, $this->items);
|
||||
unset($this->items[$key]);
|
||||
$item->setCart(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -177,11 +156,7 @@ abstract class Cart implements CartInterface
|
|||
*/
|
||||
public function hasItem(ItemInterface $item)
|
||||
{
|
||||
foreach ($this->items as $i) {
|
||||
if ($item === $i) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return $this === $item->getCart();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -203,7 +178,7 @@ abstract class Cart implements CartInterface
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setExpiresAt(\DateTime $expiresAt)
|
||||
public function setExpiresAt(\DateTime $expiresAt = null)
|
||||
{
|
||||
$this->expiresAt = $expiresAt;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,27 +24,6 @@ interface CartInterface
|
|||
* @return integer
|
||||
*/
|
||||
function getId();
|
||||
|
||||
/**
|
||||
* Get cart hash id.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function getHash();
|
||||
|
||||
/**
|
||||
* Sets cart hash id.
|
||||
*
|
||||
* @param string $hash
|
||||
*/
|
||||
function setHash($hash);
|
||||
|
||||
/**
|
||||
* Generates hash.
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
function generateHash();
|
||||
|
||||
/**
|
||||
* Returns total item count.
|
||||
|
|
@ -60,11 +39,16 @@ interface CartInterface
|
|||
*/
|
||||
function setTotalItems($totalItems);
|
||||
|
||||
function isLocked();
|
||||
function setLocked($locked);
|
||||
|
||||
/**
|
||||
* Checks whether the cart is empty.
|
||||
*/
|
||||
function isEmpty();
|
||||
|
||||
function setItems($items);
|
||||
|
||||
/**
|
||||
* Returns all items from cart.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -42,12 +42,4 @@ abstract class CartManager implements CartManagerInterface
|
|||
{
|
||||
return $this->class;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setClass($class)
|
||||
{
|
||||
$this->class = $class;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -69,13 +69,6 @@ interface CartManagerInterface
|
|||
*/
|
||||
function findCartsBy(array $criteria);
|
||||
|
||||
/**
|
||||
* Sets FQCN of cart model.
|
||||
*
|
||||
* @param string $class
|
||||
*/
|
||||
function setClass($class);
|
||||
|
||||
/**
|
||||
* Returns FQCN of cart model.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -28,10 +28,7 @@ abstract class Item implements ItemInterface
|
|||
protected $cart;
|
||||
|
||||
protected $quantity;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->quantity = 0;
|
||||
|
|
@ -62,7 +59,7 @@ abstract class Item implements ItemInterface
|
|||
*
|
||||
* @param CartInterface $cart
|
||||
*/
|
||||
public function setCart(CartInterface $cart)
|
||||
public function setCart(CartInterface $cart = null)
|
||||
{
|
||||
$this->cart = $cart;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ interface ItemInterface
|
|||
{
|
||||
function getId();
|
||||
function getCart();
|
||||
function setCart(CartInterface $cart);
|
||||
function setCart(CartInterface $cart = null);
|
||||
function getQuantity();
|
||||
function setQuantity($quantity);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,12 +42,4 @@ abstract class ItemManager implements ItemManagerInterface
|
|||
{
|
||||
return $this->class;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setClass($class)
|
||||
{
|
||||
$this->class = $class;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,13 +25,6 @@ interface ItemManagerInterface
|
|||
*/
|
||||
function getClass();
|
||||
|
||||
/**
|
||||
* Sets FQCN of cart item model.
|
||||
*
|
||||
* @param $class
|
||||
*/
|
||||
function setClass($class);
|
||||
|
||||
/**
|
||||
* Creates item model object.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ namespace Sylius\Bundle\CartBundle\Provider;
|
|||
use Sylius\Bundle\CartBundle\Model\CartInterface;
|
||||
|
||||
/**
|
||||
* Container for carts.
|
||||
* Container for cart.
|
||||
*
|
||||
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -1,20 +1,18 @@
|
|||
About SyliusCartBundle...
|
||||
=================================
|
||||
|
||||
This bundle is part of **Sylius e-commerce system**.
|
||||
Multiple or single cart engine.
|
||||
Simple cart engine.
|
||||
|
||||
About project "Sylius".
|
||||
-----------------------
|
||||
Sylius.
|
||||
-------
|
||||
|
||||
Sylius is meant to be a e-commerce system build on Symfony2 and by its community.
|
||||
The goal is to create a end-user application that will be developer friendly.
|
||||
Most of parts are fully functional and are powering 2 online shops that run in production.
|
||||
The work is focused on the bundles like PluginsBundle and ThemingBundle.
|
||||
More bundles are one their way to github. Stay tuned.
|
||||
**Sylius** is simple but **end-user and developer friendly** webshop engine built on top of Symfony2. Visit [sylius.org](http://sylius.org).
|
||||
|
||||
**Please note!** I know that many of this bundles are far from being perfect.
|
||||
I just want to share my work with others and also learn something new.
|
||||
Examples.
|
||||
---------
|
||||
|
||||
If you want to see this and other bundles in action, try out the [Sylius sandbox application](http://github.com/Sylius/Sylius-Sandbox).
|
||||
It's open sourced github project.
|
||||
|
||||
Documentation.
|
||||
--------------
|
||||
|
|
|
|||
|
|
@ -9,12 +9,6 @@
|
|||
</call>
|
||||
<tag name="kernel.event_listener" event="kernel.request" method="onCoreRequest" />
|
||||
</service>
|
||||
<service id="sylius_cart.listener.response" class="%sylius_cart.listener.response.class%">
|
||||
<call method="setContainer">
|
||||
<argument type="service" id="service_container" />
|
||||
</call>
|
||||
<tag name="kernel.event_listener" event="kernel.response" method="onCoreResponse" />
|
||||
</service>
|
||||
</services>
|
||||
|
||||
</container>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
|
||||
|
||||
<mapped-superclass name="Sylius\Bundle\CartBundle\Entity\Cart" table="sylius_cart">
|
||||
<field name="hash" column="hash" type="string" length="255" />
|
||||
<field name="locked" column="locked" type="boolean" />
|
||||
<field name="totalItems" column="total_items" type="integer" />
|
||||
<field name="expiresAt" column="expires_at" type="datetime" />
|
||||
</mapped-superclass>
|
||||
|
|
|
|||
0
src/Sylius/Bundle/CartBundle/Resources/doc/index.md
Normal file
0
src/Sylius/Bundle/CartBundle/Resources/doc/index.md
Normal file
|
|
@ -38,7 +38,7 @@ class SyliusCartExtension extends Twig_Extension
|
|||
public function getFunctions()
|
||||
{
|
||||
return array(
|
||||
'sylius_cart_getCart' => new Twig_Function_Method($this, 'getCart', array('is_safe' => array('html'))),
|
||||
'sylius_cart_get' => new Twig_Function_Method($this, 'getCart', array('is_safe' => array('html'))),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue