Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #242 from TomHAnderson/feature/query-provider-para…
Browse files Browse the repository at this point in the history
…meters

Feature/query provider parameters
  • Loading branch information
TomHAnderson committed Dec 16, 2015
2 parents f8e5cde + 7c0de77 commit e2e63e9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 48 deletions.
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@
}
},
"require-dev": {
"doctrine/doctrine-mongo-odm-module": "^0.9.0",
"doctrine/doctrine-orm-module": "^0.9.0",
"doctrine/doctrine-mongo-odm-module": "^0.9",
"doctrine/doctrine-orm-module": "^0.9",
"doctrine/mongodb-odm": "^1",
"phpunit/phpunit": "^4.7",
"squizlabs/php_codesniffer": "^2.3.0",
"zendframework/zend-form": "^2.5.0",
"zendframework/zend-log": "^2.5.0",
"zendframework/zend-serializer": "^2.5.0",
"zendframework/zend-test": "^2.5.0",
"zendframework/zend-hydrator": "~1",
"zendframework/zend-form": "~2.4",
"zendframework/zend-log": "~2.4",
"zendframework/zend-serializer": "~2.4",
"zendframework/zend-test": "~2.4",
"zendframework/zend-stdlib": "2.4.9",
"zfcampus/zf-apigility-admin": "^1.1",
"zendframework/zend-i18n": "^2.5.0"
"zendframework/zend-i18n": "~2.4"
},
"require": {
"php": ">=5.4",
Expand Down
56 changes: 19 additions & 37 deletions src/Server/Resource/DoctrineResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace ZF\Apigility\Doctrine\Server\Resource;

use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface;
Expand All @@ -21,10 +19,10 @@
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\StaticEventManager;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\Hydrator\HydratorAwareInterface;
use Zend\Stdlib\Hydrator\HydratorInterface;
use Zend\EventManager\SharedEventManager;
use Traversable;
use ReflectionClass;

Expand All @@ -34,34 +32,25 @@
* @package ZF\Apigility\Doctrine\Server\Resource
*/
class DoctrineResource extends AbstractResourceListener implements
ServiceManagerAwareInterface,
ObjectManagerAwareInterface,
EventManagerAwareInterface,
HydratorAwareInterface
{
/**
* @var ServiceManager
* @var SharedEventManager Interface
*/
protected $serviceManager;
protected $sharedEventManager;

/**
* @param ServiceManager $serviceManager
*
* @return $this
*/
public function setServiceManager(ServiceManager $serviceManager)
public function getSharedEventManager()
{
$this->serviceManager = $serviceManager;

return $this;
return $this->sharedEventManager;
}

/**
* @return ServiceManager
*/
public function getServiceManager()
public function setSharedEventManager(SharedEventManager $sharedEventManager)
{
return $this->serviceManager;
$this->sharedEventManager = $sharedEventManager;

return $this;
}

/**
Expand Down Expand Up @@ -571,7 +560,8 @@ public function fetchAll($data = array())

// Add event to set extra HAL data
$entityClass = $this->getEntityClass();
StaticEventManager::getInstance()->attach(

$this->getSharedEventManager()->attach(
'ZF\Rest\RestController',
'getList.post',
function (EventInterface $e) use ($queryProvider, $entityClass, $data) {
Expand All @@ -582,17 +572,9 @@ function (EventInterface $e) use ($queryProvider, $entityClass, $data) {
$collection->setItemCountPerPage($halCollection->getPageSize());
$collection->setCurrentPageNumber($halCollection->getPage());

$halCollection->setAttributes(
array(
'count' => $collection->getCurrentItemCount(),
'total' => $collection->getTotalItemCount(),
'collectionTotal' => $queryProvider->getCollectionTotal($entityClass)
)
);

$halCollection->setCollectionRouteOptions(
array(
'query' => ArrayUtils::iteratorToArray($data)
'query' => $e->getTarget()->getRequest()->getQuery()->toArray()
)
);
}
Expand All @@ -610,7 +592,7 @@ function (EventInterface $e) use ($queryProvider, $entityClass, $data) {
*/
public function patch($id, $data)
{
$entity = $this->findEntity($id, 'patch');
$entity = $this->findEntity($id, 'patch', $data);

if ($entity instanceof ApiProblem) {
// @codeCoverageIgnoreStart
Expand Down Expand Up @@ -657,7 +639,7 @@ public function replaceList($data)
*/
public function update($id, $data)
{
$entity = $this->findEntity($id, 'update');
$entity = $this->findEntity($id, 'update', $data);

if ($entity instanceof ApiProblem) {
// @codeCoverageIgnoreStart
Expand Down Expand Up @@ -720,7 +702,7 @@ protected function triggerDoctrineEvent($name, $entity, $data = null)
*
* @return object
*/
protected function findEntity($id, $method)
protected function findEntity($id, $method, $data = null)
{
// Match identiy identifier name(s) with id(s)
$ids = explode($this->getMultiKeyDelimiter(), $id);
Expand Down Expand Up @@ -752,12 +734,12 @@ protected function findEntity($id, $method)
if (array_key_exists($this->getRouteIdentifierName(), $routeParams)) {
unset($routeParams[$this->getRouteIdentifierName()]);
}

$reservedRouteParams = ['controller','action',
\Zend\Mvc\ModuleRouteListener::MODULE_NAMESPACE,\Zend\Mvc\ModuleRouteListener::ORIGINAL_CONTROLLER
];
$allowedRouteParams = array_diff_key($routeParams, array_flip($reservedRouteParams));

/**
* Append query selection parameters by route match.
*/
Expand All @@ -776,8 +758,8 @@ protected function findEntity($id, $method)

// Build query
$queryProvider = $this->getQueryProvider($method);
$queryBuilder = $queryProvider->createQuery($this->getEvent(), $this->getEntityClass(), null);
$queryBuilder = $queryProvider->createQuery($this->getEvent(), $this->getEntityClass(), $data);

if ($queryBuilder instanceof ApiProblem) {
// @codeCoverageIgnoreStart
return $queryBuilder;
Expand Down
9 changes: 7 additions & 2 deletions src/Server/Resource/DoctrineResourceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
class DoctrineResourceFactory implements AbstractFactoryInterface
{

/**
* Cache of canCreateServiceWithName lookups
* @var array
Expand All @@ -39,11 +38,13 @@ class DoctrineResourceFactory implements AbstractFactoryInterface
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
if (array_key_exists($requestedName, $this->lookupCache)) {

return $this->lookupCache[$requestedName];
}

if (!$serviceLocator->has('Config')) {
// @codeCoverageIgnoreStart

return false;
}
// @codeCoverageIgnoreEnd
Expand Down Expand Up @@ -142,13 +143,14 @@ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $

/** @var DoctrineResource $listener */
$listener = new $className();
$listener->setServiceManager($serviceLocator);
$listener->setSharedEventManager($serviceLocator->get('Application')->getEventManager()->getSharedManager());
$listener->setObjectManager($objectManager);
$listener->setHydrator($hydrator);
$listener->setQueryProviders($queryProviders);
$listener->setQueryCreateFilter($queryCreateFilter);
$listener->setEntityIdentifierName($restConfig['entity_identifier_name']);
$listener->setRouteIdentifierName($restConfig['route_identifier_name']);

if (count($configuredListeners)) {
foreach ($configuredListeners as $configuredListener) {
$listener->getEventManager()->attach($configuredListener);
Expand Down Expand Up @@ -184,6 +186,7 @@ protected function loadObjectManager(ServiceLocatorInterface $serviceLocator, $c
throw new ServiceNotCreatedException('The object_manager could not be found.');
}
// @codeCoverageIgnoreEnd

return $objectManager;
}

Expand Down Expand Up @@ -225,6 +228,7 @@ protected function loadHydrator(
}

// @codeCoverageIgnoreEnd

return $hydratorManager->get($doctrineConnectedConfig['hydrator']);
}

Expand Down Expand Up @@ -309,6 +313,7 @@ protected function loadConfiguredListeners(ServiceLocatorInterface $serviceLocat
foreach ($config['listeners'] as $listener) {
$listeners[] = $serviceLocator->get($listener);
}

return $listeners;
}
}
2 changes: 1 addition & 1 deletion test/src/Server/ORM/CRUD/CRUDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ public function testRpcController()
$this->getRequest()->setContent(null);
$this->dispatch("/test/artist/$artistId/album");
$body = json_decode($this->getResponse()->getBody(), true);
$this->assertEquals(2, $body['count']);
$this->assertEquals(2, $body['total_items']);

$this->dispatch("/test/artist/$artistId/album/$albumId");
$body = json_decode($this->getResponse()->getBody(), true);
Expand Down

0 comments on commit e2e63e9

Please sign in to comment.