Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #40 from TomHAnderson/hotfix/customHydrator
Browse files Browse the repository at this point in the history
Allow abstract factory for custom hydrator
  • Loading branch information
veewee authored Oct 25, 2018
2 parents e7a7554 + 69f399f commit 02c8517
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"require": {
"php": "^5.6 || ^7.0",
"doctrine/common": "^2.6.1",
"zendframework/zend-servicemanager": "^2.7.6 || ^3.1",
"zendframework/zend-servicemanager": "^3.3.2",
"zendframework/zend-hydrator": "^1.1 || ^2.2.1",
"zendframework/zend-modulemanager": "^2.7.2",
"api-skeletons/zf-doctrine-module-zend-hydrator": "^1.0",
Expand Down
7 changes: 6 additions & 1 deletion src/Service/DoctrineHydratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,12 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
}

if ($useCustomHydrator) {
$extractService = $container->get($config['hydrator']);
try {
$extractService = $container->build($config['hydrator'], $config);
} catch (ServiceNotFoundException $e) {
$extractService = $container->get($config['hydrator']);
}

$hydrateService = $extractService;
}

Expand Down
18 changes: 18 additions & 0 deletions test/src/Hydrator/CustomBuildHydratorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace PhproTest\DoctrineHydrationModule\Hydrator;

use Interop\Container\ContainerInterface;
use Phpro\DoctrineHydrationModule\Hydrator\DoctrineHydrator;
use Zend\Hydrator\ArraySerializable;

final class CustomBuildHydratorFactory
{
public function __invoke(
ContainerInterface $container,
$requestedName,
array $options = null
) {
return new ArraySerializable();
}
}
20 changes: 20 additions & 0 deletions test/src/Tests/Service/DoctrineHydratorFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhproTest\DoctrineHydrationModule\Tests\Service;

use PhproTest\DoctrineHydrationModule\Hydrator\CustomBuildHydratorFactory;
use Phpro\DoctrineHydrationModule\Service\DoctrineHydratorFactory;
use Zend\ServiceManager\ServiceManager;
use Zend\Hydrator\HydratorPluginManager;
Expand Down Expand Up @@ -190,6 +191,25 @@ public function it_should_be_possible_to_configure_a_custom_hydrator()
$this->assertInstanceOf('Zend\Hydrator\ArraySerializable', $hydrator->getExtractService());
}

/**
* @test
*/
public function it_should_be_possible_to_configure_a_custom_hydrator_as_factory()
{
$this->serviceConfig['doctrine-hydrator']['custom-hydrator']['hydrator'] = 'custom.build.hydrator';
$this->serviceManager->setService('config', $this->serviceConfig);

$this->serviceManager->setFactory(
'custom.build.hydrator',
new CustomBuildHydratorFactory()
);

$hydrator = $this->createOrmHydrator();

$this->assertInstanceOf('Zend\Hydrator\ArraySerializable', $hydrator->getHydrateService());
$this->assertInstanceOf('Zend\Hydrator\ArraySerializable', $hydrator->getExtractService());
}

/**
* @test
*/
Expand Down

0 comments on commit 02c8517

Please sign in to comment.