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

Commit

Permalink
Merge branch 'hotfix/18-controller-classes-as-rpc'
Browse files Browse the repository at this point in the history
Close #19
Fixes #18
  • Loading branch information
weierophinney committed Jan 7, 2019
2 parents c03f498 + 571b98f commit f4f740e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 1.4.1 - TBD
## 1.4.1 - 2019-01-07

### Added

Expand All @@ -22,7 +22,7 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#19](https://github.com/zfcampus/zf-rpc/pull/19) fixes a circular dependency issue in the `RpcControllerFactory`.

## 1.4.0 - 2018-05-03

Expand Down
16 changes: 15 additions & 1 deletion src/Factory/RpcControllerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@

class RpcControllerFactory implements AbstractFactoryInterface
{
/**
* Marker used to ensure we do not end up in a circular dependency lookup
* loop.
*
* @see https://github.com/zfcampus/zf-rpc/issues/18
* @var null|string
*/
private $lastRequestedControllerService;

/**
* Determine if we can create a service with name
*
Expand Down Expand Up @@ -118,10 +127,15 @@ private function marshalCallable($string, ContainerInterface $container)
$callable = false;
list($class, $method) = explode('::', $string, 2);

if ($container->has('ControllerManager')) {
if ($container->has('ControllerManager')
&& $this->lastRequestedControllerService !== $class
) {
$this->lastRequestedControllerService = $class;
$callable = $this->marshalCallableFromContainer($class, $method, $container->get('ControllerManager'));
}

$this->lastRequestedControllerService = null;

if (! $callable) {
$callable = $this->marshalCallableFromContainer($class, $method, $container);
}
Expand Down
40 changes: 40 additions & 0 deletions test/Factory/RpcControllerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ProphecyInterface;
use ReflectionProperty;
use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\Controller\ControllerManager;
use Zend\Mvc\Controller\PluginManager;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\ServiceLocatorInterface;
use ZF\Rpc\Factory\RpcControllerFactory;
Expand Down Expand Up @@ -287,4 +289,42 @@ public function testServiceCreationReturnsRpcControllerWrappingCallableForValidC
$this->assertInstanceOf(RpcController::class, $controller);
$this->assertAttributeSame($callable, 'wrappedCallable', $controller);
}

/**
* @group 7
* @see https://github.com/zfcampus/zf-rpc/issues/18
*/
public function testFactoryDoesNotEnterACircularDependencyLookupCondition()
{
$config = [
'controllers' => [
'abstract_factories' => [
RpcControllerFactory::class,
],
],
'zf-rpc' => [
TestAsset\Foo::class => [
'callable' => TestAsset\Foo::class . '::bar',
],
],
];

$this->services->has('config')->willReturn(true);
$this->services->get('config')->willReturn($config);

$this->services->has(TestAsset\Foo::class)->willReturn(false);

$this->services->get('EventManager')->willReturn($this->prophesize(EventManagerInterface::class)->reveal());
$this->services->get('ControllerPluginManager')->willReturn($this->prophesize(PluginManager::class)->reveal());

$controllerManager = new ControllerManager($this->services->reveal(), $config['controllers']);

$this->services->has('ControllerManager')->willReturn(true);
$this->services->get('ControllerManager')->willReturn($controllerManager);

$this->assertTrue($controllerManager->has(TestAsset\Foo::class));

$controller = $controllerManager->get(TestAsset\Foo::class);
$this->assertInstanceOf(RpcController::class, $controller);
}
}

0 comments on commit f4f740e

Please sign in to comment.