diff --git a/src/Sylius/Bundle/ResourceBundle/.travis.yml b/src/Sylius/Bundle/ResourceBundle/.travis.yml index a7d1437e74..7b3362010d 100644 --- a/src/Sylius/Bundle/ResourceBundle/.travis.yml +++ b/src/Sylius/Bundle/ResourceBundle/.travis.yml @@ -6,5 +6,7 @@ php: before_script: composer install --dev +script: php bin/phpspec run + notifications: email: travis-ci@sylius.org diff --git a/src/Sylius/Bundle/ResourceBundle/Controller/RequestFetcher.php b/src/Sylius/Bundle/ResourceBundle/Controller/RequestFetcher.php new file mode 100644 index 0000000000..ba94b2a44d --- /dev/null +++ b/src/Sylius/Bundle/ResourceBundle/Controller/RequestFetcher.php @@ -0,0 +1,107 @@ + + */ +class RequestFetcher +{ + protected $request; + protected $configuration; + + public function __construct(Request $request) + { + $this->request = $request; + $this->configuration = $request->attributes->get('_sylius.resource', $this->getDefaultConfiguration()); + } + + public function getCriteria() + { + return $this->get('criteria', array()); + } + + public function getSorting() + { + return $this->get('sorting', array()); + } + + public function getRedirect() + { + return $this->get('redirect'); + } + + public function getFormType() + { + return $this->get('form'); + } + + public function getIdentifierCriteria() + { + return array( + $this->getIdentifierName() => $this->getIdentifierValue() + ); + } + + public function getIdentifierName() + { + return $this->get('identifier', 'id'); + } + + public function getIdentifierValue() + { + return $this->get($this->getIdentifierName(), 'id'); + } + + public function isCollectionPaginated() + { + return (Boolean) $this->get('paginate', true); + } + + public function getPaginationMaxPerPage() + { + if (!$this->isCollectionPaginated()) { + throw new \BadMethodCallException('The current request configuration does not paginate the resources'); + } + + return $this->get('paginate', 10); + } + + public function getTemplate() + { + return $this->get('template'); + } + + public function isHtmlRequest() + { + return 'html' === $this->request->getRequestFormat(); + } + + protected function get($name, $default = null) + { + return isset($this->configuration[$name]) ? $this->configuration[$name] : $default; + } + + protected function getDefaultConfiguration() + { + return array(); + } +} diff --git a/src/Sylius/Bundle/ResourceBundle/Controller/ResourceController.php b/src/Sylius/Bundle/ResourceBundle/Controller/ResourceController.php index a5f8044fac..c63e2a0ba0 100644 --- a/src/Sylius/Bundle/ResourceBundle/Controller/ResourceController.php +++ b/src/Sylius/Bundle/ResourceBundle/Controller/ResourceController.php @@ -16,9 +16,7 @@ use FOS\RestBundle\View\RouteRedirectView; use FOS\RestBundle\View\View; use Sylius\Bundle\ResourceBundle\Model\ResourceInterface; use Symfony\Bundle\FrameworkBundle\Controller\Controller; -use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** @@ -26,7 +24,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; * * @author Paweł Jędrzejewski */ -class ResourceController extends Controller implements ResourceControllerInterface +class ResourceController extends Controller { protected $bundlePrefix; protected $resourceName; @@ -40,18 +38,20 @@ class ResourceController extends Controller implements ResourceControllerInterfa } /** - * Get single resource. + * Get single resource by its identifier. */ public function getAction(Request $request) { - $criteria = $this->getCriteria(); - $criteria[$this->getIdentifierName()] = $this->getIdentifierValue(); + $criteria = $this + ->getRequestFetcher() + ->getIdentifierCriteria() + ; $resource = $this->findResourceOr404($criteria); $view = View::create() ->setTemplate($this->getFullTemplateName('show.html')) - ->setTemplateVar($this->getResourceName()) + ->setTemplateVar($this->resourceName) ->setData($resource) ; @@ -63,49 +63,50 @@ class ResourceController extends Controller implements ResourceControllerInterfa */ public function getCollectionAction(Request $request) { - $criteria = $this->getCriteria(); - $sorting = $this->getSorting(); + $fetcher = $this->getRequestFetcher(); + $pluralName = Pluralization::pluralize($this->resourceName); + $criteria = $fetcher->getCriteria(); + $sorting = $fetcher->getSorting(); - if ($this->isPaginated()) { + $view = View::create() + ->setTemplate($this->getFullTemplateName('list.html')) + ; + + if ($fetcher->isCollectionPaginated()) { $paginator = $this ->getRepository() - ->paginate($criteria, $sorting) + ->createPaginator($criteria, $sorting) ; - $paginator->setCurrentPage($request->query->get('page', 1), true, true); + $paginator->setCurrentPage($request->get('page', 1), true, true); + $paginator->setMaxPerPage($fetcher->getPaginationMaxPerPage()); + $resources = $paginator->getCurrentPageResults(); - $pluralName = Pluralization::pluralize($this->getResourceName()); - - $data = $this->isHtmlRequest() ? array( + $data = $fetcher->isHtmlRequest() ? array( $pluralName => $resources, 'paginator' => $paginator ) : $resources; } else { + $view->setTemplateVar($pluralName); + $data = $this ->getRepository() - ->getCollection($criteria, $sorting) + ->findBy($criteria, $sorting, $fetcher->getLimit()) ; } - $view = View::create() - ->setTemplate($this->getFullTemplateName('list.html')) - ->setData($data) - ; + $view->setData($data); return $this->handleView($view); } /** - * Create resource. - * - * @param Request $request - * - * @return Response + * Create new resource or just display the form. */ public function createAction(Request $request) { - $resource = $this->createResource(); + $resource = $this->create(); $form = $this->createResourceForm($resource); if ($request->isMethod('POST') && $form->bind($request)->isValid()) { @@ -121,8 +122,8 @@ class ResourceController extends Controller implements ResourceControllerInterfa $view = View::create() ->setTemplate($this->getFullTemplateName('create.html')) ->setData(array( - $this->getResourceName() => $resource, - 'form' => $form->createView() + $this->resourceName => $resource, + 'form' => $form->createView() )) ; @@ -130,20 +131,20 @@ class ResourceController extends Controller implements ResourceControllerInterfa } /** - * Update resource. - * - * @param Request $request - * - * @return Response + * Display the form for editing or update the resource. */ public function updateAction(Request $request) { - $resource = $this->findResourceOr404(array($this->getIdentifierName() => $this->getIdentifierValue())); + $criteria = $this + ->getRequestFetcher() + ->getIdentifierCriteria() + ; + + $resource = $this->findResourceOr404($criteria); $form = $this->createResourceForm($resource); if ($request->isMethod('POST') && $form->bind($request)->isValid()) { $this->getManager()->persist($resource); - $this->setFlash('success', sprintf("%s has been updated", ucfirst($this->getResourceName()))); return $this->redirectToResource($resource); } @@ -164,76 +165,56 @@ class ResourceController extends Controller implements ResourceControllerInterfa } /** - * Deletes resource. - * - * @param Request $request - * - * @return Response + * Delete resource. */ public function deleteAction(Request $request) { - $resource = $this->findResourceOr404(array($this->getIdentifierName() => $this->getIdentifierValue())); + $criteria = $this + ->getRequestFetcher() + ->getIdentifierCriteria() + ; + $resource = $this->findResourceOr404($criteria); $this->getManager()->remove($resource); - $this->setFlash('success', sprintf("%s has been deleted", ucfirst($this->getResourceName()))); return $this->redirectToResourceCollection(); } - public function setFlash($name, $value) + protected function getRequestFetcher() { - if ($this->isHtmlRequest()) { - $this - ->get('session') - ->getFlashBag() - ->set($name, $value) - ; - } + return $this->get('sylius_resource.fetcher'); } - /** - * Create new resource instance. - */ - protected function createResource() + protected function create() { return $this->getManager()->create(); } - /** - * Create resource form. - * - * @param ResourceInterface $resource - * - * @return FormInterface - */ protected function createResourceForm(ResourceInterface $resource = null) { return $this->createForm($this->getResourceFormType(), $resource); } - /** - * Redirect to resource resource. - * - * @param ResourceInterface $resource - * - * @return RouteRedirectView - */ + protected function getResourceFormType() + { + if (null !== $type = $this->requestFetcher->getFormType()) { + return $type; + } + + return sprintf('%s_%s', $this->bundlePrefix, $this->resourceName); + } + protected function redirectToResource(ResourceInterface $resource) { - $redirect = $this->getRequest()->attributes->get('_sylius.redirect'); + $this->requestFetcher->getRedirect(); $route = $redirect ? $redirect : $this->getResourceRoute(); return $this->handleView(RouteRedirectView::create($route, array('id' => $resource->getId()))); } - /** - * Redirect to list of resourcees. - * - * @return RouteRedirectView - */ protected function redirectToResourceCollection() { - $redirect = $this->getRequest()->attributes->get('_sylius.redirect'); + $this->requestFetcher->getRedirect(); $route = $redirect ? $redirect : $this->getResourceCollectionRoute(); return $this->handleView(RouteRedirectView::create($route)); @@ -241,29 +222,19 @@ class ResourceController extends Controller implements ResourceControllerInterfa protected function getResourceRoute() { - throw new \BadMethodCallException('You have to implement this method'); + return sprintf('%s_%s', $this->bundlePrefix, $this->resourceName); } protected function getResourceCollectionRoute() { - throw new \BadMethodCallException('You have to implement this method'); + return sprintf('%s_%s', $this->bundlePrefix, Pluralization::pluralize($this->resourceName)); } - /** - * Get resource manager. - * - * @return ResourceManagerInterface - */ protected function getManager() { return $this->get($this->getServiceName('manager')); } - /** - * Get resource repository. - * - * @return ResourceRepositoryInterface - */ protected function getRepository() { return $this->get($this->getServiceName('repository')); @@ -271,57 +242,18 @@ class ResourceController extends Controller implements ResourceControllerInterfa protected function getServiceName($name) { - return sprintf('%s.%s.%s', $this->getBundlePrefix(), $name, $this->getResourceName()); + return sprintf('%s.%s.%s', $this->bundlePrefix, $name, $this->resourceName); } - /** - * Tries to find resource with given id. - * Throws special 404 exception when unsuccessful. - * - * @param array $criteria Criteria - * - * @return ResourceInterface - * - * @throws NotFoundHttpException - */ protected function findResourceOr404(array $criteria) { - if (!$resource = $this->getRepository()->get($criteria)) { + if (!$resource = $this->getRepository()->findOneBy($criteria)) { throw new NotFoundHttpException('Requested resource does not exist'); } return $resource; } - protected function getIdentifierName() - { - return $this->getRequest()->attributes->get('_sylius.identifier', 'id'); - } - - protected function getIdentifierValue() - { - if (null === $identifier = $this->getRequest()->get($this->getIdentifierName())) { - throw new NotFoundHttpException('No resource identifier supplied'); - } - - return $identifier; - } - - protected function isPaginated() - { - return (Boolean) $this->getRequest()->attributes->get('_sylius.paginate', true); - } - - protected function getCriteria() - { - return $this->getRequest()->get('_sylius.criteria', array()); - } - - protected function getSorting() - { - return $this->getRequest()->get('_sylius.sorting', array()); - } - protected function renderResponse($templateName, array $parameters = array()) { return $this->render($this->getFullTemplateName($templateName), $parameters); @@ -329,14 +261,12 @@ class ResourceController extends Controller implements ResourceControllerInterfa protected function getFullTemplateName($name) { - $template = $this->getRequest()->attributes->get('_sylius.template'); - - if (null !== $template) { + if (null !== $template = $this->getRequestFetcher()->getTemplate()) { return $template; } return sprintf('%s:%s.%s', - $this->getTemplateNamespace(), + $this->templateNamespace, $name, $this->getEngine() ); @@ -344,12 +274,7 @@ class ResourceController extends Controller implements ResourceControllerInterfa protected function getEngine() { - return $this->container->getParameter(sprintf('%s.engine', $this->getBundlePrefix())); - } - - protected function isHtmlRequest() - { - return 'html' === $this->getRequest()->getRequestFormat(); + return $this->container->getParameter(sprintf('%s.engine', $this->bundlePrefix)); } protected function handleView(View $view) @@ -357,23 +282,12 @@ class ResourceController extends Controller implements ResourceControllerInterfa return $this->get('fos_rest.view_handler')->handle($view); } - protected function getResourceFormType() + protected function setFlash($type, $message) { - return sprintf('%s_%s', $this->getBundlePrefix(), $this->getResourceName()); - } - - protected function getTemplateNamespace() - { - return $this->templateNamespace; - } - - protected function getBundlePrefix() - { - return $this->bundlePrefix; - } - - protected function getResourceName() - { - return $this->resourceName; + return $this + ->get('session') + ->getFlashBag() + ->set($type, $message) + ; } } diff --git a/src/Sylius/Bundle/ResourceBundle/Controller/ResourceControllerInterface.php b/src/Sylius/Bundle/ResourceBundle/Controller/ResourceControllerInterface.php deleted file mode 100644 index c1be408a20..0000000000 --- a/src/Sylius/Bundle/ResourceBundle/Controller/ResourceControllerInterface.php +++ /dev/null @@ -1,28 +0,0 @@ - - */ -interface ResourceControllerInterface -{ - function getAction(Request $request); - function getCollectionAction(Request $request); - function createAction(Request $request); - function updateAction(Request $request); - function deleteAction(Request $request); -} diff --git a/src/Sylius/Bundle/ResourceBundle/DependencyInjection/SyliusResourceExtension.php b/src/Sylius/Bundle/ResourceBundle/DependencyInjection/SyliusResourceExtension.php index 9800cd624b..8bb5b937d2 100644 --- a/src/Sylius/Bundle/ResourceBundle/DependencyInjection/SyliusResourceExtension.php +++ b/src/Sylius/Bundle/ResourceBundle/DependencyInjection/SyliusResourceExtension.php @@ -29,5 +29,6 @@ class SyliusResourceExtension extends Extension public function load(array $config, ContainerBuilder $container) { $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config/container')); + $loader->load('services.xml'); } } diff --git a/src/Sylius/Bundle/ResourceBundle/Doctrine/ORM/ResourceRepository.php b/src/Sylius/Bundle/ResourceBundle/Doctrine/ORM/ResourceRepository.php new file mode 100644 index 0000000000..f1d11893fa --- /dev/null +++ b/src/Sylius/Bundle/ResourceBundle/Doctrine/ORM/ResourceRepository.php @@ -0,0 +1,47 @@ + + */ +class ResourceRepository extends BaseResourceRepository +{ + /** + * {@inheritdoc} + */ + public function createPaginator(array $criteria = array(), array $sortBy = null) + { + $alias = $this->getAlias(); + $queryBuilder = $this->objectRepository->createQueryBuilder($alias); + + if (null !== $sortBy) { + foreach ($sortBy as $property => $order) { + $queryBuilder->orderBy($alias.$property, $order); + } + } + + return new Pagerfanta(new DoctrineORMAdapter($queryBuilder)); + } + + protected function getAlias() + { + return 'r'; + } +} diff --git a/src/Sylius/Bundle/ResourceBundle/Manager/Doctrine/DoctrineResourceManager.php b/src/Sylius/Bundle/ResourceBundle/Doctrine/ResourceManager.php similarity index 63% rename from src/Sylius/Bundle/ResourceBundle/Manager/Doctrine/DoctrineResourceManager.php rename to src/Sylius/Bundle/ResourceBundle/Doctrine/ResourceManager.php index 301db80c81..08d3ced51a 100644 --- a/src/Sylius/Bundle/ResourceBundle/Manager/Doctrine/DoctrineResourceManager.php +++ b/src/Sylius/Bundle/ResourceBundle/Doctrine/ResourceManager.php @@ -9,10 +9,10 @@ * file that was distributed with this source code. */ -namespace Sylius\Bundle\ResourceBundle\Manager\Doctrine; +namespace Sylius\Bundle\ResourceBundle\Doctrine; use Doctrine\Common\Persistence\ObjectManager; -use Sylius\Bundle\ResourceBundle\Manager\ResourceManager; +use Sylius\Bundle\ResourceBundle\Manager\ResourceManager as BaseResourceManager; use Sylius\Bundle\ResourceBundle\Model\ResourceInterface; /** @@ -20,25 +20,25 @@ use Sylius\Bundle\ResourceBundle\Model\ResourceInterface; * * @author Paweł Jędrzejewski */ -class DoctrineResourceManager extends ResourceManager implements DoctrineResourceManagerInterface +class ResourceManager extends BaseResourceManager { protected $objectManager; - public function __construct(ObjectManager $objectManager, $class) + public function __construct(ObjectManager $objectManager, $className) { $this->objectManager = $objectManager; - parent::__construct($class); + parent::__construct($className); } /** * {@inheritdoc} */ - public function persist(ResourceInterface $resource, $commit = true) + public function persist(ResourceInterface $resource, $flush = true) { $this->objectManager->persist($resource); - if ($commit) { + if ($flush) { $this->objectManager->flush(); } } @@ -46,17 +46,12 @@ class DoctrineResourceManager extends ResourceManager implements DoctrineResourc /** * {@inheritdoc} */ - public function remove(ResourceInterface $resource, $commit = true) + public function remove(ResourceInterface $resource, $flush = true) { $this->objectManager->remove($resource); - if ($commit) { + if ($flush) { $this->objectManager->flush(); } } - - public function getObjectManager() - { - return $this->objectManager; - } } diff --git a/src/Sylius/Bundle/ResourceBundle/Doctrine/ResourceRepository.php b/src/Sylius/Bundle/ResourceBundle/Doctrine/ResourceRepository.php new file mode 100644 index 0000000000..63ebf187c1 --- /dev/null +++ b/src/Sylius/Bundle/ResourceBundle/Doctrine/ResourceRepository.php @@ -0,0 +1,53 @@ + + */ +abstract class ResourceRepository extends BaseResourceRepository +{ + protected $objectRepository; + + public function __construct(ObjectRepository $objectRepository, $className) + { + $this->objectRepository = $objectRepository; + + parent::__construct($className); + } + + public function find($id) + { + return $this->objectRepository->find($id); + } + + public function findAll() + { + return $this->objectRepository->findAll(); + } + + public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) + { + return $this->objectRepository->findBy($criteria, $orderBy, $limit, $offset); + } + + public function findOneBy(array $criteria) + { + return $this->objectRepository->findBy($criteria); + } + +} diff --git a/src/Sylius/Bundle/ResourceBundle/Manager/Doctrine/DoctrineResourceManagerInterface.php b/src/Sylius/Bundle/ResourceBundle/Manager/Doctrine/DoctrineResourceManagerInterface.php deleted file mode 100644 index ef80db20b5..0000000000 --- a/src/Sylius/Bundle/ResourceBundle/Manager/Doctrine/DoctrineResourceManagerInterface.php +++ /dev/null @@ -1,24 +0,0 @@ - - */ -interface DoctrineResourceManagerInterface extends ResourceManagerInterface -{ - function getObjectManager(); -} diff --git a/src/Sylius/Bundle/ResourceBundle/Manager/ResourceManager.php b/src/Sylius/Bundle/ResourceBundle/Manager/ResourceManager.php index ef40e43c31..a09489e9cf 100644 --- a/src/Sylius/Bundle/ResourceBundle/Manager/ResourceManager.php +++ b/src/Sylius/Bundle/ResourceBundle/Manager/ResourceManager.php @@ -18,22 +18,22 @@ namespace Sylius\Bundle\ResourceBundle\Manager; */ abstract class ResourceManager implements ResourceManagerInterface { - protected $class; + protected $className; - public function __construct($class) + public function __construct($className) { - $this->class = $class; + $this->className = $className; } public function create() { - $class = $this->getClass(); + $class = $this->getClassName(); return new $class; } - public function getClass() + public function getClassName() { - return $this->class; + return $this->className; } } diff --git a/src/Sylius/Bundle/ResourceBundle/Manager/ResourceManagerInterface.php b/src/Sylius/Bundle/ResourceBundle/Manager/ResourceManagerInterface.php index 47f19cc49b..857afcc754 100644 --- a/src/Sylius/Bundle/ResourceBundle/Manager/ResourceManagerInterface.php +++ b/src/Sylius/Bundle/ResourceBundle/Manager/ResourceManagerInterface.php @@ -25,28 +25,28 @@ interface ResourceManagerInterface * * @return ResourceInterface */ - function create(); + public function create(); /** * Persist. * * @param ResourceInterface $resource - * @param Boolean $commit + * @param Boolean $flush */ - function persist(ResourceInterface $resource, $commit = true); + public function persist(ResourceInterface $resource, $flush = true); /** * Removes resource. * * @param ResourceInterface $resource - * @param Boolean $commit + * @param Boolean $flush */ - function remove(ResourceInterface $resource, $commit = true); + public function remove(ResourceInterface $resource, $flush = true); /** * Get resource class name. * * @return string */ - function getClass(); + public function getClassName(); } diff --git a/src/Sylius/Bundle/ResourceBundle/Model/ResourceInterface.php b/src/Sylius/Bundle/ResourceBundle/Model/ResourceInterface.php index 3bb20e395e..1097b5f979 100644 --- a/src/Sylius/Bundle/ResourceBundle/Model/ResourceInterface.php +++ b/src/Sylius/Bundle/ResourceBundle/Model/ResourceInterface.php @@ -23,5 +23,5 @@ interface ResourceInterface * * @return mixed */ - function getId(); + public function getId(); } diff --git a/src/Sylius/Bundle/ResourceBundle/Repository/Doctrine/DoctrineResourceRepository.php b/src/Sylius/Bundle/ResourceBundle/Repository/Doctrine/DoctrineResourceRepository.php deleted file mode 100644 index 90aa31eca6..0000000000 --- a/src/Sylius/Bundle/ResourceBundle/Repository/Doctrine/DoctrineResourceRepository.php +++ /dev/null @@ -1,37 +0,0 @@ - - */ -abstract class DoctrineResourceRepository extends ResourceRepository implements DoctrineResourceRepositoryInterface -{ - protected $objectRepository; - - public function __construct(ObjectRepository $objectRepository, $class) - { - $this->objectRepository = $objectRepository; - - parent::__construct($class); - } - - public function getObjectRepository() - { - return $this->objectRepository; - } -} diff --git a/src/Sylius/Bundle/ResourceBundle/Repository/Doctrine/DoctrineResourceRepositoryInterface.php b/src/Sylius/Bundle/ResourceBundle/Repository/Doctrine/DoctrineResourceRepositoryInterface.php deleted file mode 100644 index 19944f4cbc..0000000000 --- a/src/Sylius/Bundle/ResourceBundle/Repository/Doctrine/DoctrineResourceRepositoryInterface.php +++ /dev/null @@ -1,24 +0,0 @@ - - */ -interface DoctrineResourceRepositoryInterface extends ResourceRepositoryInterface -{ - function getObjectRepository(); -} diff --git a/src/Sylius/Bundle/ResourceBundle/Repository/Doctrine/ORM/ResourceRepository.php b/src/Sylius/Bundle/ResourceBundle/Repository/Doctrine/ORM/ResourceRepository.php deleted file mode 100644 index d53bf0b79f..0000000000 --- a/src/Sylius/Bundle/ResourceBundle/Repository/Doctrine/ORM/ResourceRepository.php +++ /dev/null @@ -1,122 +0,0 @@ - - */ -class ResourceRepository extends DoctrineResourceRepository -{ - /** - * {@inheritdoc} - */ - public function get(array $criteria) - { - return $this - ->getQueryBuilder($criteria, array()) - ->getQuery() - ->getOneOrNullResult() - ; - } - - /** - * {@inheritdoc} - */ - public function getCollection(array $criteria = array(), array $sorting = array(), $limit = null) - { - $queryBuilder = $this->getQueryBuilder($criteria, $sorting); - - if (null !== $limit) { - $queryBuilder->setMaxResults($limit); - } - - return $queryBuilder - ->getQuery() - ->execute() - ; - } - - /** - * {@inheritdoc} - */ - public function paginate(array $criteria = array(), array $sorting = array()) - { - return new Pagerfanta(new DoctrineORMAdapter($this->getQueryBuilder($criteria, $sorting))); - } - - protected function getQueryBuilder(array $criteria, array $sorting) - { - $queryBuilder = $this->getBasicQueryBuilder(); - - $this->filter($queryBuilder, $criteria); - $this->sort($queryBuilder, $sorting); - - return $queryBuilder; - } - - protected function filter(QueryBuilder $queryBuilder, array $criteria) - { - $reflectionClass = new \ReflectionClass($this->getClass()); - $properties = array_keys($reflectionClass->getDefaultProperties()); - - $i = 0; - foreach ($criteria as $property => $condition) { - if (!is_array($condition)) { - $condition = array('=', $condition); - } - - list($comparison, $value) = $condition; - - if (in_array($property, $properties) && in_array($comparison, array('=', '!=', '>', '<', '>=', '<='))) { - - $formula = sprintf("%s.%s %s :PARAM%d", $this->getAlias(), $property, $comparison, $i); - $queryBuilder - ->andWhere($formula) - ->setParameter('PARAM'.$i, $value) - ; - - $i++; - } - } - } - - protected function sort(QueryBuilder $queryBuilder, array $sorting) - { - $acceptedOrders = array('asc', 'desc'); - - foreach ($sorting as $property => $order) { - if (in_array(strtolower($order), $acceptedOrders)) { - $queryBuilder->orderBy($this->getAlias().'.'.$property, $order); - } - } - } - - protected function getBasicQueryBuilder() - { - return $this - ->getObjectRepository() - ->createQueryBuilder($this->getAlias()) - ; - } - - protected function getAlias() - { - return 'r'; - } -} diff --git a/src/Sylius/Bundle/ResourceBundle/Repository/ResourceRepository.php b/src/Sylius/Bundle/ResourceBundle/Repository/ResourceRepository.php index 7df8c476e4..3450a7b3ba 100644 --- a/src/Sylius/Bundle/ResourceBundle/Repository/ResourceRepository.php +++ b/src/Sylius/Bundle/ResourceBundle/Repository/ResourceRepository.php @@ -12,21 +12,21 @@ namespace Sylius\Bundle\ResourceBundle\Repository; /** - * Base resource repository class. + * Base Doctrine resource manager class. * * @author Paweł Jędrzejewski */ abstract class ResourceRepository implements ResourceRepositoryInterface { - protected $class; + protected $className; - public function __construct($class) + public function __construct($className) { - $this->class = $class; + $this->className = $className; } - public function getClass() + public function getClassName() { - return $this->class; + return $this->className; } } diff --git a/src/Sylius/Bundle/ResourceBundle/Repository/ResourceRepositoryInterface.php b/src/Sylius/Bundle/ResourceBundle/Repository/ResourceRepositoryInterface.php index fad98b8f50..ab7a01c5f9 100644 --- a/src/Sylius/Bundle/ResourceBundle/Repository/ResourceRepositoryInterface.php +++ b/src/Sylius/Bundle/ResourceBundle/Repository/ResourceRepositoryInterface.php @@ -11,44 +11,19 @@ namespace Sylius\Bundle\ResourceBundle\Repository; -use Sylius\Bundle\ResourceBundle\Model\ResourceInterface; +use Doctrine\Common\Persistence\ObjectRepository; /** * Resource repository interface. * * @author Paweł Jędrzejewski */ -interface ResourceRepositoryInterface +interface ResourceRepositoryInterface extends ObjectRepository { - /** - * Finds resource by identifier. - * - * @param array $criteria - * - * @return ResourceInterface - */ - function get(array $criteria); - - /** - * Finds resource by criteria. - * - * @param array $criteria - * - * @return ResourceInterface - */ - function getCollection(array $criteria = array(), array $sorting = array(), $limit = null); - /** * Creates a new Pagerfanta instance to paginate the resources. * * @return PagerfantaInterface */ - function paginate(array $criteria = array(), array $sorting = array()); - - /** - * Get resource class name. - * - * @return string - */ - function getClass(); + public function createPaginator(array $criteria = array(), array $sortBy = null); } diff --git a/src/Sylius/Bundle/ResourceBundle/Resources/config/container/controllers.xml b/src/Sylius/Bundle/ResourceBundle/Resources/config/container/controllers.xml deleted file mode 100644 index a6d3aa3b08..0000000000 --- a/src/Sylius/Bundle/ResourceBundle/Resources/config/container/controllers.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - - - - - - - diff --git a/src/Sylius/Bundle/ResourceBundle/Resources/config/container/driver/doctrine/orm.xml b/src/Sylius/Bundle/ResourceBundle/Resources/config/container/driver/doctrine/orm.xml deleted file mode 100644 index c40846c670..0000000000 --- a/src/Sylius/Bundle/ResourceBundle/Resources/config/container/driver/doctrine/orm.xml +++ /dev/null @@ -1,41 +0,0 @@ - - - - - - - - Sylius\Bundle\AddressingBundle\Entity\AddressManager - Sylius\Bundle\AddressingBundle\Sorting\ORM\AddressSorter - - - - - - - %sylius_addressing.model.address.class% - - - - - - - - - - - - diff --git a/src/Sylius/Bundle/ResourceBundle/Resources/config/container/forms.xml b/src/Sylius/Bundle/ResourceBundle/Resources/config/container/forms.xml deleted file mode 100644 index a78d80a61b..0000000000 --- a/src/Sylius/Bundle/ResourceBundle/Resources/config/container/forms.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - - - %sylius_addressing.model.address.class% - - - - - - diff --git a/src/Sylius/Bundle/ResourceBundle/Resources/config/container/manipulators.xml b/src/Sylius/Bundle/ResourceBundle/Resources/config/container/services.xml similarity index 62% rename from src/Sylius/Bundle/ResourceBundle/Resources/config/container/manipulators.xml rename to src/Sylius/Bundle/ResourceBundle/Resources/config/container/services.xml index 5cc8562cc5..9a3e0b09a7 100644 --- a/src/Sylius/Bundle/ResourceBundle/Resources/config/container/manipulators.xml +++ b/src/Sylius/Bundle/ResourceBundle/Resources/config/container/services.xml @@ -14,12 +14,15 @@ + http://symfony.com/schema/dic/services/services-1.0.xsd"> + + + Sylius\Bundle\ResourceBundle\Controller\RequestFetcher + - - + +