Skip to content

Commit

Permalink
Support per-resource hydrators
Browse files Browse the repository at this point in the history
  • Loading branch information
TomHAnderson committed Dec 8, 2015
1 parent d0891cd commit f8e5cde
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"zendframework/zend-log": "^2.5.0",
"zendframework/zend-serializer": "^2.5.0",
"zendframework/zend-test": "^2.5.0",
"zendframework/zend-hydrator": "~1",
"zfcampus/zf-apigility-admin": "^1.1",
"zendframework/zend-i18n": "^2.5.0"
},
Expand Down
34 changes: 28 additions & 6 deletions src/Server/Resource/DoctrineResourceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $
{
$config = $serviceLocator->get('Config');
$doctrineConnectedConfig = $config['zf-apigility']['doctrine-connected'][$requestedName];
$doctrineHydratorConfig = $config['doctrine-hydrator'];

$restConfig = null;
foreach ($config['zf-rest'] as $restControllerConfig) {
Expand All @@ -129,7 +130,12 @@ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $
$className = $this->normalizeClassname($className);

$objectManager = $this->loadObjectManager($serviceLocator, $doctrineConnectedConfig);
$hydrator = $this->loadHydrator($serviceLocator, $doctrineConnectedConfig, $objectManager);
$hydrator = $this->loadHydrator(
$serviceLocator,
$doctrineConnectedConfig,
$doctrineHydratorConfig,
$objectManager
);
$queryProviders = $this->loadQueryProviders($serviceLocator, $doctrineConnectedConfig, $objectManager);
$queryCreateFilter = $this->loadQueryCreateFilter($serviceLocator, $doctrineConnectedConfig, $objectManager);
$configuredListeners = $this->loadConfiguredListeners($serviceLocator, $doctrineConnectedConfig);
Expand Down Expand Up @@ -187,10 +193,15 @@ protected function loadObjectManager(ServiceLocatorInterface $serviceLocator, $c
*
* @return HydratorInterface
*/
protected function loadHydrator(ServiceLocatorInterface $serviceLocator, $config)
{
protected function loadHydrator(
ServiceLocatorInterface $serviceLocator,
array $doctrineConnectedConfig,
array $doctrineHydratorConfig,
$objectManager
) {

// @codeCoverageIgnoreStart
if (!isset($config['hydrator'])) {
if (!isset($doctrineConnectedConfig['hydrator'])) {
return null;
}

Expand All @@ -199,11 +210,22 @@ protected function loadHydrator(ServiceLocatorInterface $serviceLocator, $config
}

$hydratorManager = $serviceLocator->get('HydratorManager');
if (!$hydratorManager->has($config['hydrator'])) {
if (!$hydratorManager->has($doctrineConnectedConfig['hydrator'])) {
return null;
}

// Set the hydrator for the entity for this resource to the hydrator
// configured for the resource. This removes per-entity hydrator configuration
// allowing multiple hydrators per resource.
if (isset($doctrineConnectedConfig['hydrator'])) {
$entityClass = $doctrineHydratorConfig[$doctrineConnectedConfig['hydrator']]['entity_class'];
$viewHelpers = $serviceLocator->get('ViewHelperManager');
$hal = $viewHelpers->get('Hal');
$hal->getEntityHydratorManager()->addHydrator($entityClass, $doctrineConnectedConfig['hydrator']);
}

// @codeCoverageIgnoreEnd
return $hydratorManager->get($config['hydrator']);
return $hydratorManager->get($doctrineConnectedConfig['hydrator']);
}

/**
Expand Down

0 comments on commit f8e5cde

Please sign in to comment.