New version using the resource bundle

This commit is contained in:
Paweł Jędrzejewski 2012-11-13 01:36:00 +01:00
parent 58b545a546
commit dace097e76
32 changed files with 60 additions and 1330 deletions

View file

@ -1,7 +1,5 @@
phpunit.xml
Tests/autoload.php
vendor/ vendor/
bin/
composer.phar composer.phar
composer.lock composer.lock

View file

@ -4,10 +4,10 @@ php:
- 5.3 - 5.3
- 5.4 - 5.4
before_script: before_script: composer install --dev
- wget http://getcomposer.org/composer.phar
- php composer.phar install --dev script: php bin/phpspec run
notifications: notifications:
email: irc: "irc.freenode.org#sylius-dev"
- travis-ci@sylius.org email: travis-ci@sylius.org

View file

@ -1,177 +0,0 @@
<?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\AddressingBundle\Controller\Backend;
use Sylius\Bundle\AddressingBundle\EventDispatcher\Event\FilterAddressEvent;
use Sylius\Bundle\AddressingBundle\EventDispatcher\SyliusAddressingEvents;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Backend address controller.
* All administrator related actions are here.
*
* @author Paweł Jędrzejewski <pjedrzejewski@sylius.pl>
*/
class AddressController extends ContainerAware
{
/**
* Show all paginated addresses.
*
* @param Request $request
*
* @return Response
*/
public function listAction(Request $request)
{
$addressManager = $this->container->get('sylius_addressing.manager.address');
$addressSorter = $this->container->get('sylius_addressing.sorter.address');
$paginator = $addressManager->createPaginator($addressSorter);
$paginator->setCurrentPage($request->query->get('page', 1), true, true);
$addresses = $paginator->getCurrentPageResults();
return $this->container->get('templating')->renderResponse('SyliusAddressingBundle:Backend/Address:list.html.'.$this->getEngine(), array(
'addresses' => $addresses,
'paginator' => $paginator,
'sorter' => $addressSorter
));
}
/**
* Shows one address.
*
* @param mixed $id The address identifier
*
* @return Response
*/
public function showAction($id)
{
$address = $this->findAddressOr404($id);
return $this->container->get('templating')->renderResponse('SyliusAddressingBundle:Backend/Address:show.html.'.$this->getEngine(), array(
'address' => $address
));
}
/**
* Creating an address.
*
* @param Request $request
*
* @return Response
*/
public function createAction(Request $request)
{
$address = $this->container->get('sylius_addressing.manager.address')->createAddress();
$form = $this->container->get('form.factory')->create('sylius_addressing_address', $address);
if ('POST' === $request->getMethod()) {
$form->bindRequest($request);
if ($form->isValid()) {
$this->container->get('event_dispatcher')->dispatch(SyliusAddressingEvents::ADDRESS_CREATE, new FilterAddressEvent($address));
$this->container->get('sylius_addressing.manipulator.address')->create($address);
return new RedirectResponse($this->container->get('router')->generate('sylius_addressing_backend_address_show', array(
'id' => $address->getId()
)));
}
}
return $this->container->get('templating')->renderResponse('SyliusAddressingBundle:Backend/Address:create.html.'.$this->getEngine(), array(
'form' => $form->createView()
));
}
/**
* Updating an address.
*
* @param Request $request
* @param mixed $id The address identifier
*
* @return Response
*/
public function updateAction(Request $request, $id)
{
$address = $this->findAddressOr404($id);
$form = $this->container->get('form.factory')->create('sylius_addressing_address', $address);
if ('POST' === $request->getMethod()) {
$form->bindRequest($request);
if ($form->isValid()) {
$this->container->get('event_dispatcher')->dispatch(SyliusAddressingEvents::ADDRESS_UPDATE, new FilterAddressEvent($address));
$this->container->get('sylius_addressing.manipulator.address')->update($address);
return new RedirectResponse($this->container->get('router')->generate('sylius_addressing_backend_address_show', array(
'id' => $address->getId()
)));
}
}
return $this->container->get('templating')->renderResponse('SyliusAddressingBundle:Backend/Address:update.html.'.$this->getEngine(), array(
'form' => $form->createView(),
'address' => $address
));
}
/**
* Deletes address.
*
* @param mixed $id The address identifier
*
* @return Response
*/
public function deleteAction($id)
{
$address = $this->findAddressOr404($id);
$this->container->get('event_dispatcher')->dispatch(SyliusAddressingEvents::ADDRESS_DELETE, new FilterAddressEvent($address));
$this->container->get('sylius_addressing.manipulator.address')->delete($address);
return new RedirectResponse($this->container->get('router')->generate('sylius_addressing_backend_address_list'));
}
/**
* Tries to find address with given id.
* Throws special 404 exception when unsuccessful.
*
* @param mixed $id The address identifier
*
* @return AddressInterface
*
* @throws NotFoundHttpException
*/
protected function findAddressOr404($id)
{
if (!$address = $this->container->get('sylius_addressing.manager.address')->findAddress($id)) {
throw new NotFoundHttpException('Requested address does not exist');
}
return $address;
}
/**
* Get engine.
*
* @return string
*/
protected function getEngine()
{
return $this->container->getParameter('sylius_addressing.engine');
}
}

View file

@ -67,18 +67,7 @@ class Configuration implements ConfigurationInterface
->arrayNode('controller') ->arrayNode('controller')
->addDefaultsIfNotSet() ->addDefaultsIfNotSet()
->children() ->children()
->arrayNode('backend') ->scalarNode('address')->defaultValue('Sylius\\Bundle\\ResourceBundle\\Controller\\ResourceController')->end()
->addDefaultsIfNotSet()
->children()
->scalarNode('address')->defaultValue('Sylius\\Bundle\\AddressingBundle\\Controller\\Backend\\AddressController')->end()
->end()
->end()
->end()
->end()
->arrayNode('manipulator')
->addDefaultsIfNotSet()
->children()
->scalarNode('address')->defaultValue('Sylius\\Bundle\\AddressingBundle\\Manipulator\\AddressManipulator')->end()
->end() ->end()
->end() ->end()
->arrayNode('form') ->arrayNode('form')

View file

@ -45,74 +45,10 @@ class SyliusAddressingExtension extends Extension
$container->setParameter('sylius_addressing.driver', $config['driver']); $container->setParameter('sylius_addressing.driver', $config['driver']);
$container->setParameter('sylius_addressing.engine', $config['engine']); $container->setParameter('sylius_addressing.engine', $config['engine']);
$configurations = array( $container->setParameter('sylius_addressing.model.address.class', $config['classes']['model']['address']);
'controllers', $container->setParameter('sylius_addressing.controller.address.class', $config['classes']['controller']['address']);
'forms', $container->setParameter('sylius_addressing.form.type.address.class', $config['classes']['form']['type']['address']);
'manipulators'
);
foreach($configurations as $basename) { $loader->load('services.xml');
$loader->load(sprintf('%s.xml', $basename));
}
$this->remapParametersNamespaces($config['classes'], $container, array(
'manipulator' => 'sylius_addressing.manipulator.%s.class',
'model' => 'sylius_addressing.model.%s.class'
));
$this->remapParametersNamespaces($config['classes']['form'], $container, array(
'type' => 'sylius_addressing.form.type.%s.class',
));
$this->remapParametersNamespaces($config['classes']['controller'], $container, array(
'backend' => 'sylius_addressing.controller.backend.%s.class',
'frontend' => 'sylius_addressing.controller.frontend.%s.class'
));
}
/**
* Remap parameters.
*
* @param array $config
* @param ContainerBuilder $container
* @param array $map
*/
protected function remapParameters(array $config, ContainerBuilder $container, array $map)
{
foreach ($map as $name => $paramName) {
if (isset($config[$name])) {
$container->setParameter($paramName, $config[$name]);
}
}
}
/**
* Remap parameter namespaces.
*
* @param array $config
* @param ContainerBuilder $container
* @param array $namespaceConfig
*/
protected function remapParametersNamespaces(array $config, ContainerBuilder $container, array $namespaces)
{
foreach ($namespaces as $ns => $map) {
if ($ns) {
if (!isset($config[$ns])) {
continue;
}
$namespaceConfig = $config[$ns];
} else {
$namespaceConfig = $config;
}
if (is_array($map)) {
$this->remapParameters($namespaceConfig, $container, $map);
} else {
foreach ($namespaceConfig as $name => $value) {
if (null !== $value) {
$container->setParameter(sprintf($map, $name), $value);
}
}
}
}
} }
} }

View file

@ -1,133 +0,0 @@
<?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\AddressingBundle\Entity;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Pagerfanta\Adapter\DoctrineORMAdapter;
use Pagerfanta\Pagerfanta;
use Sylius\Bundle\AddressingBundle\Model\AddressInterface;
use Sylius\Bundle\AddressingBundle\Model\AddressManager as BaseAddressManager;
use Sylius\Bundle\AddressingBundle\Sorting\SorterInterface;
/**
* Address entity manager.
* Doctrine ORM driver implementation.
*
* @author Paweł Jędrzejewski <pjedrzejewski@sylius.pl>
*/
class AddressManager extends BaseAddressManager
{
/**
* Entity manager.
*
* @var EntityManager
*/
protected $entityManager;
/**
* Address repository.
*
* @var EntityRepository
*/
protected $repository;
/**
* Constructor.
*
* @param EntityManager $entityManager
* @param string $class
*/
public function __construct(EntityManager $entityManager, $class)
{
parent::__construct($class);
$this->entityManager = $entityManager;
$this->repository = $entityManager->getRepository($class);
}
/**
* {@inheritdoc}
*/
public function createAddress()
{
$class = $this->getClass();
return new $class;
}
/**
* {@inheritdoc}
*/
public function createPaginator(SorterInterface $sorter = null)
{
$queryBuilder = $this->repository->createQueryBuilder('a')
->orderBy('a.createdAt', 'DESC')
;
if (null !== $sorter) {
$sorter->sort($queryBuilder);
}
return new Pagerfanta(new DoctrineORMAdapter($queryBuilder->getQuery()));
}
/**
* {@inheritdoc}
*/
public function persistAddress(AddressInterface $address)
{
$this->entityManager->persist($address);
$this->entityManager->flush();
}
/**
* {@inheritdoc}
*/
public function removeAddress(AddressInterface $address)
{
$this->entityManager->remove($address);
$this->entityManager->flush();
}
/**
* {@inheritdoc}
*/
public function findAddress($id)
{
return $this->repository->find($id);
}
/**
* {@inheritdoc}
*/
public function findAddressBy(array $criteria)
{
return $this->repository->findOneBy($criteria);
}
/**
* {@inheritdoc}
*/
public function findAddresses()
{
return $this->repository->findAll();
}
/**
* {@inheritdoc}
*/
public function findAddressesBy(array $criteria)
{
return $this->repository->findBy($criteria);
}
}

View file

@ -1,50 +0,0 @@
<?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\AddressingBundle\EventDispatcher\Event;
use Sylius\Bundle\AddressingBundle\Model\AddressInterface;
use Symfony\Component\EventDispatcher\Event;
/**
* Filter address event.
*
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
*/
class FilterAddressEvent extends Event
{
/**
* Address.
*
* @var AddressInterface
*/
private $address;
/**
* Constructor.
*
* @param AddressInterface $address
*/
public function __construct(AddressInterface $address)
{
$this->address = $address;
}
/**
* Get address.
*
* @return AddressInterface
*/
public function getAddress()
{
return $this->address;
}
}

View file

@ -1,24 +0,0 @@
<?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\AddressingBundle\EventDispatcher;
/**
* Events.
*
* @author Paweł Jędrzejwski <pjedrzejewski@diweb.pl>
*/
final class SyliusAddressingEvents
{
const ADDRESS_CREATE = 'sylius_addressing.event.address.create';
const ADDRESS_UPDATE = 'sylius_addressing.event.address.update';
const ADDRESS_DELETE = 'sylius_addressing.event.address.delete';
}

View file

@ -1,64 +0,0 @@
<?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\AddressingBundle\Manipulator;
use Sylius\Bundle\AddressingBundle\Model\AddressInterface;
use Sylius\Bundle\AddressingBundle\Model\AddressManagerInterface;
/**
* Address manipulator.
*
* @author Paweł Jędrzejewski <pjedrzejewski@sylius.pl>
*/
class AddressManipulator implements AddressManipulatorInterface
{
/**
* Address manager.
*
* @var AddressManagerInterface
*/
protected $addressManager;
/**
* Constructor.
*
* @param $addressManager AddressManagerInterface
*/
public function __construct(AddressManagerInterface $addressManager)
{
$this->addressManager = $addressManager;
}
/**
* {@inheritdoc}
*/
public function create(AddressInterface $address)
{
$this->addressManager->persistAddress($address);
}
/**
* {@inheritdoc}
*/
public function update(AddressInterface $address)
{
$this->addressManager->persistAddress($address);
}
/**
* {@inheritdoc}
*/
public function delete(AddressInterface $address)
{
$this->addressManager->removeAddress($address);
}
}

View file

@ -1,43 +0,0 @@
<?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\AddressingBundle\Manipulator;
use Sylius\Bundle\AddressingBundle\Model\AddressInterface;
/**
* Address manipulator interface.
*
* @author Paweł Jędrzejewski <pjedrzejewski@sylius.pl>
*/
interface AddressManipulatorInterface
{
/**
* Creates a address.
*
* @param AddressInterface $address
*/
function create(AddressInterface $address);
/**
* Updates a address.
*
* @param AddressInterface $address
*/
function update(AddressInterface $address);
/**
* Deletes a address.
*
* @param AddressInterface $address
*/
function delete(AddressInterface $address);
}

View file

@ -47,14 +47,6 @@ abstract class Address implements AddressInterface
return $this->id; return $this->id;
} }
/**
* {@inheritdoc}
*/
public function setId($id)
{
$this->id = $id;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View file

@ -11,27 +11,15 @@
namespace Sylius\Bundle\AddressingBundle\Model; namespace Sylius\Bundle\AddressingBundle\Model;
use Sylius\Bundle\ResourceBundle\Model\ResourceInterface;
/** /**
* Address model interface. * Address model interface.
* *
* @author Paweł Jędrzejewski <pjedrzejewski@sylius.pl> * @author Paweł Jędrzejewski <pjedrzejewski@sylius.pl>
*/ */
interface AddressInterface interface AddressInterface extends ResourceInterface
{ {
/**
* Returns address id.
*
* @return integer
*/
function getId();
/**
* Set id.
*
* @param mixed $id
*/
function setId($id);
/** /**
* Get creation time. * Get creation time.
* *

View file

@ -1,45 +0,0 @@
<?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\AddressingBundle\Model;
/**
* Address manager model.
*
* @author Paweł Jędrzejewski <pjedrzejewski@sylius.pl>
*/
abstract class AddressManager implements AddressManagerInterface
{
/**
* Address model class.
*
* @var string
*/
protected $class;
/**
* Constructor.
*
* @param string $class The address model class
*/
public function __construct($class)
{
$this->class = $class;
}
/**
* {@inheritdoc}
*/
public function getClass()
{
return $this->class;
}
}

View file

@ -1,85 +0,0 @@
<?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\AddressingBundle\Model;
use Sylius\Bundle\AddressingBundle\Sorting\SorterInterface;
/**
* Address manager interface.
*
* @author Paweł Jędrzejewski <pjedrzejewski@sylius.pl>
*/
interface AddressManagerInterface
{
/**
* Creates address model.
*
* @return AddressInterface
*/
function createAddress();
/**
* Creates paginator.
*
* @param SorterInterface $sorter
*/
function createPaginator(SorterInterface $sorter = null);
/**
* Persists address model.
*
* @param AddressInterface $address
*/
function persistAddress(AddressInterface $address);
/**
* Removes address model.
*
* @param AddressInterface $address
*/
function removeAddress(AddressInterface $address);
/**
* Finds address by id.
*
* @param integer $id The address id
*/
function findAddress($id);
/**
* Finds address by criteria.
*
* @param array $criteria The criteria
*/
function findAddressBy(array $criteria);
/**
* Finds all adresses.
*
* @return array The adressess
*/
function findAddresses();
/**
* Finds addresses by criteria.
*
* @param array $criteria The criteria
*/
function findAddressesBy(array $criteria);
/**
* Returns address model class.
*
* @return string The address model class
*/
function getClass();
}

View file

@ -1,28 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
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.
-->
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd"
>
<services>
<service id="sylius_addressing.controller.backend.address" class="%sylius_addressing.controller.backend.address.class%">
<call method="setContainer">
<argument type="service" id="service_container" />
</call>
</service>
</services>
</container>

View file

@ -18,24 +18,27 @@
> >
<parameters> <parameters>
<parameter key="sylius_addressing.manager.address.class">Sylius\Bundle\AddressingBundle\Entity\AddressManager</parameter> <parameter key="sylius_addressing.manager.address.class">Sylius\Bundle\ResourceBundle\Doctrine\ResourceManager</parameter>
<parameter key="sylius_addressing.sorter.address.class">Sylius\Bundle\AddressingBundle\Sorting\ORM\AddressSorter</parameter> <parameter key="sylius_addressing.repository.address.class">Sylius\Bundle\ResourceBundle\Doctrine\ORM\ResourceRepository</parameter>
</parameters> </parameters>
<services> <services>
<!-- managers... --> <service id="sylius_addressing.doctrine.repository.address"
factory-service="doctrine.orm.default_entity_manager"
factory-method="getRepository"
class="Doctrine\ORM\EntityRepository"
public="false"
>
<argument>%sylius_addressing.model.address.class%</argument>
</service>
<service id="sylius_addressing.repository.address" class="%sylius_addressing.repository.address.class%">
<argument type="service" id="sylius_addressing.doctrine.repository.address" />
<argument>%sylius_addressing.model.address.class%</argument>
</service>
<service id="sylius_addressing.manager.address" class="%sylius_addressing.manager.address.class%"> <service id="sylius_addressing.manager.address" class="%sylius_addressing.manager.address.class%">
<argument type="service" id="doctrine.orm.default_entity_manager" /> <argument type="service" id="doctrine.orm.default_entity_manager" />
<argument>%sylius_addressing.model.address.class%</argument> <argument>%sylius_addressing.model.address.class%</argument>
</service> </service>
<!-- sorters... -->
<service id="sylius_addressing.sorter.address" class="%sylius_addressing.sorter.address.class%">
<call method="setContainer">
<argument type="service" id="service_container" />
</call>
</service>
</services> </services>
</container> </container>

View file

@ -1,26 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd"
>
<services>
<service id="sylius_addressing.manipulator.address" class="%sylius_addressing.manipulator.address.class%">
<argument type="service" id="sylius_addressing.manager.address" />
</service>
</services>
</container>

View file

@ -18,12 +18,19 @@
> >
<services> <services>
<service id="sylius_addressing.controller.address" class="%sylius_addressing.controller.address.class%">
<argument>sylius_addressing</argument>
<argument>address</argument>
<argument>SyliusAddressingBundle:Address</argument>
<call method="setContainer">
<argument type="service" id="service_container" />
</call>
</service>
<service id="sylius_addressing.form.type.address" class="%sylius_addressing.form.type.address.class%"> <service id="sylius_addressing.form.type.address" class="%sylius_addressing.form.type.address.class%">
<argument>%sylius_addressing.model.address.class%</argument> <argument>%sylius_addressing.model.address.class%</argument>
<tag name="form.type" alias="sylius_addressing_address" /> <tag name="form.type" alias="sylius_addressing_address" />
</service> </service>
</services> </services>
</container> </container>

View file

@ -1,22 +0,0 @@
# This file is part of the Sylius package.
# (c) Paweł Jędrzejewski
sylius_addressing_backend_address_create:
pattern: /create
defaults: { _controller: sylius_addressing.controller.backend.address:createAction }
sylius_addressing_backend_address_update:
pattern: /{id}/update
defaults: { _controller: sylius_addressing.controller.backend.address:updateAction }
sylius_addressing_backend_address_delete:
pattern: /{id}/delete
defaults: { _controller: sylius_addressing.controller.backend.address:deleteAction }
sylius_addressing_backend_address_list:
pattern: /list
defaults: { _controller: sylius_addressing.controller.backend.address:listAction }
sylius_addressing_backend_address_show:
pattern: /{id}
defaults: { _controller: sylius_addressing.controller.backend.address:showAction }

View file

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8"?>
<!-- <!--
@ -14,8 +14,7 @@
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd" http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
>
<class name="Sylius\Bundle\AddressingBundle\Model\CommonAddress"> <class name="Sylius\Bundle\AddressingBundle\Model\CommonAddress">
<property name="firstname"> <property name="firstname">

View file

@ -1,69 +0,0 @@
<?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\AddressingBundle\Sorting\ORM;
use Doctrine\ORM\QueryBuilder;
use Sylius\Bundle\AddressingBundle\Sorting\SorterInterface;
use Symfony\Component\DependencyInjection\ContainerAware;
/**
* Default ORM sorter.
* Sorts address entities.
*
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
*/
class AddressSorter extends ContainerAware implements SorterInterface
{
public function sort($sortable)
{
if (!$sortable instanceof QueryBuilder) {
throw new InvalidArgumentException('Default sorter supports only "Doctrine\\ORM\\QueryBuilder" as sortable argument.');
}
$request = $this->container->get('request');
if (null === $sortProperty = $request->query->get('sort', null)) {
return;
}
$sortOrder = $request->query->get('order', 'ASC');
if (!in_array($sortOrder, array('ASC', 'DESC'))) {
return;
}
$addressClass = $this->container->getParameter('sylius_addressing.model.address.class');
$reflectionClass = new \ReflectionClass($addressClass);
if (!in_array($sortProperty, array_keys($reflectionClass->getDefaultProperties()))) {
return;
}
/** @var QueryBuilder */
$sortable->orderBy('a.' . $sortProperty, $sortOrder);
}
public function getOrder()
{
$sortOrder = $this->container->get('request')->query->get('order', 'ASC');
if (!in_array($sortOrder, array('ASC', 'DESC'))) {
return;
}
return ($sortOrder == 'ASC') ? 'DESC' : 'ASC';
}
}

View file

@ -1,23 +0,0 @@
<?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\AddressingBundle\Sorting;
/**
* Interface for sorter.
*
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
*/
interface SorterInterface
{
function sort($sortable);
function getOrder();
}

View file

@ -11,6 +11,7 @@
namespace Sylius\Bundle\AddressingBundle; namespace Sylius\Bundle\AddressingBundle;
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle; use Symfony\Component\HttpKernel\Bundle\Bundle;
/** /**
@ -22,13 +23,6 @@ use Symfony\Component\HttpKernel\Bundle\Bundle;
*/ */
class SyliusAddressingBundle extends Bundle class SyliusAddressingBundle extends Bundle
{ {
// Bundle driver list.
const DRIVER_DOCTRINE_ORM = 'doctrine/orm';
const DRIVER_DOCTRINE_MONGODB_ODM = 'doctrine/mongodb-odm';
const DRIVER_DOCTRINE_COUCHDB_ODM = 'doctrine/couchdb-odm';
const DRIVER_PROPEL = 'propel';
const DRIVER_PROPEL2 = 'propel2';
/** /**
* Return array of currently supported drivers. * Return array of currently supported drivers.
* *
@ -37,7 +31,7 @@ class SyliusAddressingBundle extends Bundle
static public function getSupportedDrivers() static public function getSupportedDrivers()
{ {
return array( return array(
self::DRIVER_DOCTRINE_ORM SyliusResourceBundle::DRIVER_DOCTRINE_ORM
); );
} }
} }

View file

@ -1,86 +0,0 @@
<?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\AddressingBundle\Tests\DependencyInjection;
use Sylius\Bundle\AddressingBundle\DependencyInjection\SyliusAddressingExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Yaml\Parser;
/**
* Dependency injection extension test.
*
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
*/
class SyliusAddressingExtensionTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testLoadThrowsExceptionUnlessDriverSet()
{
$loader = new SyliusAddressingExtension();
$config = $this->getEmptyConfig();
unset($config['driver']);
$loader->load(array($config), new ContainerBuilder());
}
/**
* @expectedException \InvalidArgumentException
*/
public function testLoadThrowsExceptionUnlessDriverIsValid()
{
$loader = new SyliusAddressingExtension();
$config = $this->getEmptyConfig();
$config['driver'] = 'foo';
$loader->load(array($config), new ContainerBuilder());
}
/**
* @expectedException \InvalidArgumentException
*/
public function testLoadThrowsExceptionUnlessEngineIsValid()
{
$loader = new SyliusAddressingExtension();
$config = $this->getEmptyConfig();
$config['engine'] = 'foo';
$loader->load(array($config), new ContainerBuilder());
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testLoadThrowsExceptionUnlessaddressModelClassSet()
{
$loader = new SyliusAddressingExtension();
$config = $this->getEmptyConfig();
unset($config['classes']['model']['address']);
$loader->load(array($config), new ContainerBuilder());
}
/**
* Get empty config for tests.
*
* @return array
*/
protected function getEmptyConfig()
{
$yaml = <<<EOF
driver: ORM
classes:
model:
address: Sylius\Bundle\AddressingBundle\Entity\Address
EOF;
$parser = new Parser();
return $parser->parse($yaml);
}
}

View file

@ -1,155 +0,0 @@
<?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\AddressingBundle\Tests\Entity;
use Sylius\Bundle\AddressingBundle\Entity\AddressManager;
/**
* Address manager test.
*
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
*/
class AddressManagerTest extends \PHPUnit_Framework_TestCase
{
public function testPersistAddress()
{
$address = $this->getMockAddress();
$entityManager = $this->getMockEntityManager();
$entityManager->expects($this->once())
->method('persist')
->with($this->equalTo($address))
;
$entityManager->expects($this->once())
->method('flush')
;
$addressManager = new AddressManager($entityManager, 'Foo\\Bar');
$addressManager->persistAddress($address);
}
public function testRemoveAddress()
{
$address = $this->getMockAddress();
$entityManager = $this->getMockEntityManager();
$entityManager->expects($this->once())
->method('remove')
->with($this->equalTo($address))
;
$entityManager->expects($this->once())
->method('flush')
;
$addressManager = new AddressManager($entityManager, 'Foo\\Bar');
$addressManager->removeAddress($address);
}
public function testFindAddress()
{
$address = $this->getMockAddress();
$repository = $this->getMockEntityRepository('find', 3, $address);
$entityManager = $this->getMockEntityManager($repository);
$addressManager = new AddressManager($entityManager, 'Foo\\Bar');
$this->assertEquals($address, $addressManager->findAddress(3));
}
public function testFindAddressBy()
{
$address = $this->getMockAddress();
$repository = $this->getMockEntityRepository('findOneBy', array('firstname' => 'Paweł'), $address);
$entityManager = $this->getMockEntityManager($repository);
$addressManager = new AddressManager($entityManager, 'Foo\\Bar');
$this->assertEquals($address, $addressManager->findAddressBy(array('firstname' => 'Paweł')));
}
public function testFindAddresses()
{
$result = array(
$this->getMockAddress(),
$this->getMockAddress(),
$this->getMockAddress()
);
$repository = $this->getMockEntityRepository('findAll', null, $result);
$entityManager = $this->getMockEntityManager($repository);
$addressManager = new AddressManager($entityManager, 'Foo\\Bar');
$this->assertEquals($result, $addressManager->findAddresses());
}
public function testFindAddressesBy()
{
$result = array(
$this->getMockAddress()
);
$repository = $this->getMockEntityRepository('findBy', array('firstname' => 'Paweł'), $result);
$entityManager = $this->getMockEntityManager($repository);
$addressManager = new AddressManager($entityManager, 'Foo\\Bar');
$this->assertEquals($result, $addressManager->findAddressesBy(array('firstname' => 'Paweł')));
}
private function getMockAddress()
{
return $this->getMock('Sylius\Bundle\AddressingBundle\Model\AddressInterface');
}
private function getMockEntityManager($repository = null)
{
$entityManager = $this->getMockBuilder('Doctrine\ORM\EntityManager')
->disableOriginalConstructor()
->getMock()
;
if (null !== $repository) {
$entityManager->expects($this->once())
->method('getRepository')
->will($this->returnValue($repository))
;
}
return $entityManager;
}
private function getMockEntityRepository($method, $criteria, $result)
{
$repository = $this->getMockBuilder('Doctrine\ORM\EntityRepository')
->disableOriginalConstructor()
->getMock()
;
if (null !== $criteria) {
$repository->expects($this->once())
->method($method)
->with($this->equalTo($criteria))
->will($this->returnValue($result))
;
} else {
$repository->expects($this->once())
->method($method)
->will($this->returnValue($result))
;
}
return $repository;
}
}

View file

@ -1,34 +0,0 @@
<?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\AddressingBundle\Tests\EventDispatcher\Event;
use Sylius\Bundle\AddressingBundle\EventDispatcher\Event\FilterAddressEvent;
/**
* Filter address event test.
*
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
*/
class FilterAddressEventTest extends \PHPUnit_Framework_TestCase
{
public function testConstructor()
{
$address = $this->getMockAddress();
$event = new FilterAddressEvent($address);
$this->assertEquals($address, $event->getAddress());
}
private function getMockAddress()
{
return $this->getMock('Sylius\Bundle\AddressingBundle\Model\AddressInterface');
}
}

View file

@ -1,79 +0,0 @@
<?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\AddressingBundle\Tests\Manipulator;
use Sylius\Bundle\AddressingBundle\Manipulator\AddressManipulator;
/**
* Address manipulator test.
*
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
*/
class AddressManipulatorTest extends \PHPUnit_Framework_TestCase
{
public function testCreatePersistsAddress()
{
$address = $this->getMockAddress();
$addressManager = $this->getMockAddressManager();
$addressManager->expects($this->once())
->method('persistAddress')
->with($this->equalTo($address))
;
$manipulator = new AddressManipulator($addressManager);
$manipulator->create($address);
}
public function testUpdatePersistsAddress()
{
$address = $this->getMockAddress();
$addressManager = $this->getMockAddressManager();
$addressManager->expects($this->once())
->method('persistAddress')
->with($this->equalTo($address))
;
$manipulator = new AddressManipulator($addressManager);
$manipulator->update($address);
}
public function testDeleteRemovesAddress()
{
$address = $this->getMockAddress();
$addressManager = $this->getMockAddressManager();
$addressManager->expects($this->once())
->method('removeAddress')
->with($this->equalTo($address))
;
$manipulator = new AddressManipulator($addressManager);
$manipulator->delete($address);
}
private function getMockAddress()
{
return $this->getMock('Sylius\Bundle\AddressingBundle\Model\AddressInterface');
}
private function getMockAddressManager()
{
$addressManager = $this->getMockBuilder('Sylius\Bundle\AddressingBundle\Model\AddressManagerInterface')
->disableOriginalConstructor()
->getMock()
;
return $addressManager;
}
}

View file

@ -1,16 +0,0 @@
<?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.
*/
if (!file_exists($file = __DIR__.'/../vendor/autoload.php')) {
die("Please install dev dependencies using Composer to run the test suite. \n");
} else {
require_once $file;
}

View file

@ -1,16 +0,0 @@
<?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.
*/
if (file_exists($file = __DIR__.'/autoload.php')) {
require_once $file;
} elseif (file_exists($file = __DIR__.'/autoload.php.dist')) {
require_once $file;
}

View file

@ -16,15 +16,20 @@
"homepage": "https://github.com/Sylius/SyliusAddressingBundle/contributors" "homepage": "https://github.com/Sylius/SyliusAddressingBundle/contributors"
} }
], ],
"minimum-stability": "dev",
"require": { "require": {
"php": ">=5.3.2", "php": ">=5.3.2",
"symfony/symfony": "2.1.*", "symfony/framework-bundle": "~2.1",
"sylius/resource-bundle": "*",
"stof/doctrine-extensions-bundle": "*", "stof/doctrine-extensions-bundle": "*",
"white-october/pagerfanta-bundle": "*" "white-october/pagerfanta-bundle": "*"
}, },
"require-dev": { "require-dev": {
"doctrine/orm": "*" "phpspec/phpspec2": "*"
},
"config": {
"bin-dir": "bin"
}, },
"autoload": { "autoload": {
"psr-0": { "Sylius\\Bundle\\AddressingBundle": "" } "psr-0": { "Sylius\\Bundle\\AddressingBundle": "" }

View file

@ -1,19 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit bootstrap="./Tests/bootstrap.php" colors="true">
<testsuites>
<testsuite name="SyliusAddressingBundle test suite">
<directory suffix="Test.php">./Tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Resources</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>

View file

@ -0,0 +1,13 @@
<?php
namespace spec\Sylius\Bundle\AddressingBundle\Model;
use PHPSpec2\ObjectBehavior;
class CommonAddress extends ObjectBehavior
{
function it_should_be_initializable()
{
$this->shouldHaveType('Sylius\Bundle\AddressingBundle\Model\CommonAddress');
}
}