diff --git a/composer.json b/composer.json index 36f6ab80..3b2c7825 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Server/Resource/DoctrineResource.php b/src/Server/Resource/DoctrineResource.php index 28bf1eb3..c084133d 100644 --- a/src/Server/Resource/DoctrineResource.php +++ b/src/Server/Resource/DoctrineResource.php @@ -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; @@ -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; @@ -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; } /** @@ -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) { @@ -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() ) ); } @@ -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 @@ -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 @@ -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); @@ -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. */ @@ -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; diff --git a/src/Server/Resource/DoctrineResourceFactory.php b/src/Server/Resource/DoctrineResourceFactory.php index 8373fce1..ed52c7e4 100644 --- a/src/Server/Resource/DoctrineResourceFactory.php +++ b/src/Server/Resource/DoctrineResourceFactory.php @@ -19,7 +19,6 @@ */ class DoctrineResourceFactory implements AbstractFactoryInterface { - /** * Cache of canCreateServiceWithName lookups * @var array @@ -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 @@ -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); @@ -184,6 +186,7 @@ protected function loadObjectManager(ServiceLocatorInterface $serviceLocator, $c throw new ServiceNotCreatedException('The object_manager could not be found.'); } // @codeCoverageIgnoreEnd + return $objectManager; } @@ -225,6 +228,7 @@ protected function loadHydrator( } // @codeCoverageIgnoreEnd + return $hydratorManager->get($doctrineConnectedConfig['hydrator']); } @@ -309,6 +313,7 @@ protected function loadConfiguredListeners(ServiceLocatorInterface $serviceLocat foreach ($config['listeners'] as $listener) { $listeners[] = $serviceLocator->get($listener); } + return $listeners; } } diff --git a/test/src/Server/ORM/CRUD/CRUDTest.php b/test/src/Server/ORM/CRUD/CRUDTest.php index 782a4d46..dac7616c 100644 --- a/test/src/Server/ORM/CRUD/CRUDTest.php +++ b/test/src/Server/ORM/CRUD/CRUDTest.php @@ -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);