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 #260 from TomHAnderson/hotfix/service-locator-awar…
Browse files Browse the repository at this point in the history
…e-interface

Hotfix/service locator aware interface
  • Loading branch information
TomHAnderson authored Jul 3, 2016
2 parents 7a39419 + 5c28f36 commit a782bd2
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 192 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ matrix:
- php: 7
- php: hhvm
allow_failures:
- php: 7
- php: hhvm

notifications:
Expand Down
28 changes: 9 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Apigility with Doctrine
Doctrine in Apigility
==============================

[![Build status](https://api.travis-ci.org/zfcampus/zf-apigility-doctrine.svg)](http://travis-ci.org/zfcampus/zf-apigility-doctrine)
[![Total Downloads](https://poser.pugx.org/zfcampus/zf-apigility-doctrine/downloads)](https://packagist.org/packages/zfcampus/zf-apigility-doctrine)

This module provides the classes for integrating Doctrine with Apigility.

Expand Down Expand Up @@ -40,7 +41,7 @@ for the object manager.

`/apigility/api/module[/:name]/doctrine[/:controller_service_name]`

This is a Doctrine resource route _like_ Apigility Rest `/apigility/api/module[/:name]/rest[/:controller_service_name]`
This is a Doctrine resource route _like_ Apigility Rest `/apigility/api/module[/:name]/rest[/:controller_service_name]`
To create a resource do not include `[/:controller_service_name]`

POST Parameters
Expand Down Expand Up @@ -135,23 +136,12 @@ setting the DoctrineResource::setMultiKeyDelimiter($value)
Complex queries through route parameters
----------------------------------------

You may specify multiple route parameters and as long as the route
matches then the route parameter names will be matched to the entity.
NO LONGER SUPPORTED. As of version 2.0.4 this functionality has been removed from
this module. The intended use of this module is a 1:1 mapping of entities to resources
and using subroutes is not in the spirit of this intention. It is STRONGLY recommended
you use [zfcampus/zf-doctrine-querybuilder](https://github.com/zfcampus/zf-doctrine-querybuilder)
for complex query-ability.

For instance, a route of ```/api/artist/:artist_id/album/:album_id``` mapped to the *Album*
entity will filter the *Album* for field names. So, given an album with id, name, and artist
fields the album_id matches to the resoruce configuration and will be queried by key
and the artist is a field on album and will be queried by criteria so the final query
would be

```php
$objectManager->getRepository('Album')->findOneBy(
'id' => :album_id,
'artist' => :artist_id
);
```

The album(_id) is not a field on the *Album* entity and will be ignored.


Query Providers
Expand Down Expand Up @@ -197,7 +187,7 @@ When the query provider is registered attach it to the doctrine-connected resour
Query Create Filters
==============

In order to filter or change data sent to a create statement before it is used to hydrate the entity you may use a query create filter. Create filters are very similar to *Query Providers* in their implementation.
In order to filter or change data sent to a create statement before it is used to hydrate the entity you may use a query create filter. Create filters are very similar to *Query Providers* in their implementation.

Create filters take the data as a parameter and return the data, modified or filtered.

Expand Down
24 changes: 9 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zfcampus/zf-apigility-doctrine",
"type": "library",
"description": "Doctrine for Apigility",
"description": "Doctrine in Apigility",
"keywords": [
"apigility",
"doctrine",
Expand All @@ -12,33 +12,27 @@
"authors": [
{
"name": "Tom Anderson",
"email": "tanderson@soliantconsulting.com"
"email": "tom.h.anderson@gmail.com"
}
],
"extra": {
"branch-alias": {
"dev-master": "1.1-dev",
"dev-develop": "1.2-dev"
}
},
"require-dev": {
"doctrine/doctrine-mongo-odm-module": "^0.9",
"doctrine/doctrine-orm-module": "^0.9",
"doctrine/doctrine-mongo-odm-module": "~0.10 || ^1.0",
"doctrine/doctrine-orm-module": "^0.10",
"doctrine/mongodb-odm": "^1",
"phpunit/phpunit": "^4.7",
"squizlabs/php_codesniffer": "^2.3.0",
"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-stdlib": "~2.4",
"zfcampus/zf-apigility-admin": "~1.4",
"zendframework/zend-i18n": "~2.4"
},
"require": {
"php": ">=5.4",
"phpro/zf-doctrine-hydration-module": "^0.1.5",
"zfcampus/zf-apigility": "^1.0"
"php": ">=5.4 || ^7.0",
"phpro/zf-doctrine-hydration-module": "^2.0",
"zfcampus/zf-apigility": "^1.1"
},
"suggest": {
"doctrine/doctrine-orm-module": "For ORM mapping",
Expand Down
3 changes: 1 addition & 2 deletions src/Admin/Model/DoctrineMetadataServiceResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
use ZF\ApiProblem\ApiProblem;
use ZF\Rest\AbstractResourceListener;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Exception;

class DoctrineMetadataServiceResource extends AbstractResourceListener implements ServiceManagerAwareInterface
class DoctrineMetadataServiceResource extends AbstractResourceListener
{
protected $serviceManager;

Expand Down
4 changes: 2 additions & 2 deletions src/Admin/Model/DoctrineRestServiceModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
use ZF\Rest\Exception\CreationException;
use Zf\Apigility\Admin\Model\ModuleEntity;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use ZF\ApiProblem\ApiProblem;

class DoctrineRestServiceModel implements EventManagerAwareInterface, ServiceManagerAwareInterface
class DoctrineRestServiceModel implements
EventManagerAwareInterface
{
/**
* @var ConfigResource
Expand Down
3 changes: 1 addition & 2 deletions src/Admin/Model/DoctrineRestServiceModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
use ZF\Apigility\Admin\Exception;
use ZF\Apigility\Admin\Model\RpcServiceModelFactory;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;

class DoctrineRestServiceModelFactory extends RpcServiceModelFactory implements ServiceManagerAwareInterface
class DoctrineRestServiceModelFactory extends RpcServiceModelFactory
{
const TYPE_DEFAULT = 'ZF\Apigility\Doctrine\Admin\Model\DoctrineRestServiceModel';

Expand Down
18 changes: 13 additions & 5 deletions src/Admin/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Zend\ModuleManager\Feature\DependencyIndicatorInterface;
use Zend\ModuleManager\Feature\ServiceProviderInterface;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use ZF\Apigility\Doctrine\Admin\Model\DoctrineMetadataServiceResource;

class Module implements
ConfigProviderInterface,
Expand Down Expand Up @@ -53,11 +54,15 @@ public function getConfig()
public function getServiceConfig()
{
return array(
'invokables' => array(
'ZF\Apigility\Doctrine\Admin\Model\DoctrineMetadataServiceResource' =>
'ZF\Apigility\Doctrine\Admin\Model\DoctrineMetadataServiceResource',
),
'factories' => array(
// This resource pulls the object manager dynamically
// so it needs access to the service manager
'ZF\Apigility\Doctrine\Admin\Model\DoctrineMetadataServiceResource' => function ($services) {
$instance = new DoctrineMetadataServiceResource();
$instance->setServiceManager($services);

return $instance;
},
'ZF\Apigility\Doctrine\Admin\Model\DoctrineAutodiscoveryModel' => function ($services) {
if (!$services->has('Config')) {
// @codeCoverageIgnoreStart
Expand Down Expand Up @@ -96,12 +101,15 @@ public function getServiceConfig()
__NAMESPACE__ . '\Model\DoctrineRestServiceModel::onFetch'
);

return new Model\DoctrineRestServiceModelFactory(
$instance = new Model\DoctrineRestServiceModelFactory(
$modulePathSpec,
$configFactory,
$sharedEvents,
$moduleModel
);
$instance->setServiceManager($services);

return $instance;
},
'ZF\Apigility\Doctrine\Admin\Model\DoctrineRestServiceResource' => function ($services) {
if (!$services->has('ZF\Apigility\Doctrine\Admin\Model\DoctrineRestServiceModelFactory')) {
Expand Down
95 changes: 0 additions & 95 deletions src/Server/Controller/RpcController.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Server/Event/Listener/CollectionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use Zend\InputFilter\InputFilterInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Stdlib\ArrayObject;
use Zend\Stdlib\Hydrator\HydratorAwareInterface;
use Zend\Stdlib\Hydrator\HydratorInterface;
use Zend\Hydrator\HydratorAwareInterface;
use Zend\Hydrator\HydratorInterface;
use ZF\Apigility\Doctrine\Server\Event\DoctrineResourceEvent;
use ZF\Apigility\Doctrine\Server\Exception\InvalidArgumentException;

Expand Down
39 changes: 2 additions & 37 deletions src/Server/Resource/DoctrineResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\Hydrator\HydratorAwareInterface;
use Zend\Stdlib\Hydrator\HydratorInterface;
use Zend\Hydrator\HydratorAwareInterface;
use Zend\Hydrator\HydratorInterface;
use Zend\EventManager\SharedEventManager;
use Traversable;
use ReflectionClass;
Expand Down Expand Up @@ -268,34 +268,6 @@ public function getMultiKeyDelimiter()
return $this->multiKeyDelimiter;
}

/**
* For /multi/1/keyed/2/routes/3 the route parameter
* names may include an id suffix (e.g. id, _id, Id)
* and this will be striped to create criteria
*
* Example
* $objectManager->getRepository(...)->findOneBy(
* 'multi' => 1,
* 'keyed' => 2,
* 'routes' => 3
* );
*
* @var string
*/
protected $stripRouteParameterSuffix = '_id';

public function setStripRouteParameterSuffix($value)
{
$this->stripRouteParameterSuffix = $value;

return $this;
}

public function getStripRouteParameterSuffix()
{
return $this->stripRouteParameterSuffix;
}

/**
* @var HydratorInterface
*/
Expand Down Expand Up @@ -744,13 +716,6 @@ protected function findEntity($id, $method, $data = null)
* Append query selection parameters by route match.
*/
foreach ($allowedRouteParams as $routeMatchParam => $value) {
if ($this->getStripRouteParameterSuffix() === substr(
$routeMatchParam,
-1 * strlen($this->getStripRouteParameterSuffix())
)) {
$routeMatchParam = substr($routeMatchParam, 0, -1 * strlen($this->getStripRouteParameterSuffix()));
}

if (in_array($routeMatchParam, $associationMappings) || in_array($routeMatchParam, $fieldNames)) {
$criteria[$routeMatchParam] = $value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Resource/DoctrineResourceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Stdlib\Hydrator\HydratorInterface;
use Zend\Hydrator\HydratorInterface;
use ZF\Apigility\Doctrine\Server\Collection\Query;
use RuntimeException;

Expand Down
1 change: 0 additions & 1 deletion test/src/Server/ODM/CRUD/CRUDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public function testCreate()
$this->getRequest()->setContent('{"name": "ArtistOne","createdAt": "2011-12-18 13:17:17"}');
$this->dispatch('/test/meta');
$body = json_decode($this->getResponse()->getBody(), true);

$this->assertEquals('ArtistOne', $body['name']);
$this->assertEquals(201, $this->getResponseStatusCode());
$this->validateTriggeredEvents(array(
Expand Down
10 changes: 0 additions & 10 deletions test/src/Server/ORM/CRUD/CRUDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,15 +651,5 @@ public function testRpcController()
$this->assertEquals(201, $this->getResponseStatusCode());

$albumId = $body['id'];

$this->getRequest()->setMethod(Request::METHOD_GET);
$this->getRequest()->setContent(null);
$this->dispatch("/test/artist/$artistId/album");
$body = json_decode($this->getResponse()->getBody(), true);
$this->assertEquals(2, $body['total_items']);

$this->dispatch("/test/artist/$artistId/album/$albumId");
$body = json_decode($this->getResponse()->getBody(), true);
$this->assertEquals('AlbumTwo', $body['name']);
}
}

0 comments on commit a782bd2

Please sign in to comment.