diff --git a/CHANGELOG.md b/CHANGELOG.md index cbc83702..4e578285 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,12 @@ All notable changes to this project will be documented in this file, in reverse ### Changed +- [#155](https://github.com/zendframework/zend-view/pull/155) modifies the `Zend\View\Helper\Service\IdentifyFactory` such that it will + now also look for the service `Zend\Authentication\AuthenticationServiceInterface` + if the service `Zend\Authentication\AuthenticationService` is not found. This + allows using a service named after the interface instead of the + implementation if desired. + - [#158](https://github.com/zendframework/zend-view/pull/158) modifies how a `ViewModel` (and all extensions) is cloned; the `$variables` property, if it is an object, is now cloned as well to ensure changes in the new instance do not affect the current one. diff --git a/doc/book/helpers/identity.md b/doc/book/helpers/identity.md index daf8e1f2..7ed4cadc 100644 --- a/doc/book/helpers/identity.md +++ b/doc/book/helpers/identity.md @@ -4,7 +4,8 @@ The `Identity` helper allows retrieving the identity from the `AuthenticationService`. For the `Identity` helper to work, a `Zend\Authentication\AuthenticationService` -name or alias must be defined and recognized by the `ServiceManager`. +or `Zend\Authentication\AuthenticationServiceInterface` name or alias must be +defined and recognized by the `ServiceManager`. `Identity` returns the identity discovered in the `AuthenticationService`, or `null` if no identity is available. @@ -43,3 +44,6 @@ return [ ], ]; ``` + +If that service is not registered, the plugin will then look for a service named +`Zend\Authentication\AuthenticationServiceInterface`, and use that if found. diff --git a/src/Helper/Service/IdentityFactory.php b/src/Helper/Service/IdentityFactory.php index a2630be5..371d7b76 100644 --- a/src/Helper/Service/IdentityFactory.php +++ b/src/Helper/Service/IdentityFactory.php @@ -3,13 +3,15 @@ * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\View\Helper\Service; use Interop\Container\ContainerInterface; +use Zend\Authentication\AuthenticationService; +use Zend\Authentication\AuthenticationServiceInterface; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use Zend\View\Helper\Identity; @@ -30,10 +32,13 @@ public function __invoke(ContainerInterface $container, $name, array $options = if (! method_exists($container, 'configure')) { $container = $container->getServiceLocator(); } + $helper = new Identity(); - if ($container->has('Zend\Authentication\AuthenticationService')) { - $helper->setAuthenticationService($container->get('Zend\Authentication\AuthenticationService')); + + if (null !== ($authenticationService = $this->discoverAuthenticationService($container))) { + $helper->setAuthenticationService($authenticationService); } + return $helper; } @@ -47,4 +52,18 @@ public function createService(ServiceLocatorInterface $serviceLocator, $rName = { return $this($serviceLocator, $cName); } + + /** + * @return null|AuthenticationServiceInterface + */ + private function discoverAuthenticationService(ContainerInterface $container) + { + if ($container->has(AuthenticationService::class)) { + return $container->get(AuthenticationService::class); + } + + return $container->has(AuthenticationServiceInterface::class) + ? $container->get(AuthenticationServiceInterface::class) + : null; + } } diff --git a/test/Helper/Service/IdentityFactoryTest.php b/test/Helper/Service/IdentityFactoryTest.php new file mode 100644 index 00000000..e12a30dc --- /dev/null +++ b/test/Helper/Service/IdentityFactoryTest.php @@ -0,0 +1,67 @@ +prophesize(ServiceManager::class); + $container->has(AuthenticationService::class)->willReturn(false); + $container->get(AuthenticationService::class)->shouldNotBeCalled(); + $container->has(AuthenticationServiceInterface::class)->willReturn(false); + $container->get(AuthenticationServiceInterface::class)->shouldNotBeCalled(); + + $factory = new IdentityFactory(); + $plugin = $factory($container->reveal(), Identity::class); + + $this->assertInstanceOf(Identity::class, $plugin); + $this->assertNull($plugin->getAuthenticationService()); + } + + public function testFactoryReturnsIdentityWithConfiguredAuthenticationServiceWhenPresent() + { + $container = $this->prophesize(ServiceManager::class); + $authentication = $this->prophesize(AuthenticationService::class); + + $container->has(AuthenticationService::class)->willReturn(true); + $container->get(AuthenticationService::class)->will([$authentication, 'reveal']); + $container->has(AuthenticationServiceInterface::class)->willReturn(false); + $container->get(AuthenticationServiceInterface::class)->shouldNotBeCalled(); + + $factory = new IdentityFactory(); + $plugin = $factory($container->reveal(), Identity::class); + + $this->assertInstanceOf(Identity::class, $plugin); + $this->assertSame($authentication->reveal(), $plugin->getAuthenticationService()); + } + + public function testFactoryReturnsIdentityWithConfiguredAuthenticationServiceInterfaceWhenPresent() + { + $container = $this->prophesize(ServiceManager::class); + $authentication = $this->prophesize(AuthenticationServiceInterface::class); + + $container->has(AuthenticationService::class)->willReturn(false); + $container->get(AuthenticationService::class)->shouldNotBeCalled(); + $container->has(AuthenticationServiceInterface::class)->willReturn(true); + $container->get(AuthenticationServiceInterface::class)->will([$authentication, 'reveal']); + + $factory = new IdentityFactory(); + $plugin = $factory($container->reveal(), Identity::class); + + $this->assertInstanceOf(Identity::class, $plugin); + $this->assertSame($authentication->reveal(), $plugin->getAuthenticationService()); + } +}