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

Continue fixing service casing issues #82

Merged
merged 6 commits into from
Feb 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions src/Service/ServiceListenerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
namespace Zend\Mvc\Service;

use Interop\Container\ContainerInterface;
use ReflectionClass;
use Zend\Config\Config;
use Zend\ModuleManager\Listener\ServiceListener;
use Zend\ModuleManager\Listener\ServiceListenerInterface;
use Zend\Mvc\Application;
use Zend\Mvc\View;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\FactoryInterface;
Expand All @@ -38,8 +41,8 @@ class ServiceListenerFactory implements FactoryInterface
*/
protected $defaultServiceConfig = [
'aliases' => [
'Configuration' => 'config',
'configuration' => 'config',
'Configuration' => 'config',
'console' => 'ConsoleAdapter',
'Console' => 'ConsoleAdapter',
'ConsoleDefaultRenderingStrategy' => View\Console\DefaultRenderingStrategy::class,
Expand All @@ -65,7 +68,7 @@ class ServiceListenerFactory implements FactoryInterface
],
'invokables' => [],
'factories' => [
'Application' => 'Zend\Mvc\Service\ApplicationFactory',
'Application' => ApplicationFactory::class,
'config' => 'Zend\Mvc\Service\ConfigFactory',
'ControllerManager' => 'Zend\Mvc\Service\ControllerManagerFactory',
'ControllerPluginManager' => 'Zend\Mvc\Service\ControllerPluginManagerFactory',
Expand Down Expand Up @@ -121,6 +124,20 @@ class ServiceListenerFactory implements FactoryInterface
],
];

/**
* Constructor
*
* When executed under zend-servicemanager v3, injects additional aliases
* to ensure backwards compatibility.
*/
public function __construct()
{
$r = new ReflectionClass(ServiceLocatorInterface::class);
if ($r->hasMethod('build')) {
$this->injectV3Aliases();
}
}

/**
* Create the service listener service
*
Expand Down Expand Up @@ -275,4 +292,23 @@ private function validatePluginManagerOptions($options, $name)
));
}
}

/**
* Inject additional aliases for zend-servicemanager v3 usage
*
* If the constructor detects that we're operating under zend-servicemanager v3,
* this method injects additional aliases to ensure that common services
* can be retrieved using both Titlecase and lowercase, and will get the
* same instances.
*
* @return void
*/
private function injectV3Aliases()
{
$this->defaultServiceConfig['aliases']['application'] = 'Application';
$this->defaultServiceConfig['aliases']['Config'] = 'config';
$this->defaultServiceConfig['aliases']['request'] = 'Request';
$this->defaultServiceConfig['aliases']['response'] = 'Response';
$this->defaultServiceConfig['aliases']['router'] = 'Router';
}
}
17 changes: 1 addition & 16 deletions src/Service/ViewHelperManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,11 @@ private function injectOverrideFactories(HelperPluginManager $plugins, Container
private function createUrlHelperFactory(ContainerInterface $services)
{
return function () use ($services) {
// zend-servicemanager v2: fetch parent locator
if (method_exists($services, 'getServiceLocator') && ! method_exists($services, 'configure')) {
$services = $services->getServiceLocator() ?: $services;
}

$helper = new ViewHelper\Url;
$router = Console::isConsole() ? 'HttpRouter' : 'Router';
$helper->setRouter($services->get($router));

$match = $services->get('application')
$match = $services->get('Application')
->getMvcEvent()
->getRouteMatch()
;
Expand All @@ -162,11 +157,6 @@ private function createUrlHelperFactory(ContainerInterface $services)
private function createBasePathHelperFactory(ContainerInterface $services)
{
return function () use ($services) {
// zend-servicemanager v2: fetch parent locator
if (method_exists($services, 'getServiceLocator') && ! method_exists($services, 'configure')) {
$services = $services->getServiceLocator() ?: $services;
}

$config = $services->has('config') ? $services->get('config') : [];
$helper = new ViewHelper\BasePath;

Expand Down Expand Up @@ -204,11 +194,6 @@ private function createBasePathHelperFactory(ContainerInterface $services)
private function createDoctypeHelperFactory(ContainerInterface $services)
{
return function () use ($services) {
// zend-servicemanager v2: fetch parent locator
if (method_exists($services, 'getServiceLocator') && ! method_exists($services, 'configure')) {
$services = $services->getServiceLocator() ?: $services;
}

$config = $services->has('config') ? $services->get('config') : [];
$config = isset($config['view_manager']) ? $config['view_manager'] : [];
$helper = new ViewHelper\Doctype;
Expand Down
84 changes: 83 additions & 1 deletion test/Service/ServiceListenerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,28 @@
namespace ZendTest\Mvc\Service;

use PHPUnit_Framework_TestCase as TestCase;
use ReflectionClass;
use ReflectionProperty;
use Zend\Mvc\Service\ServiceListenerFactory;
use Zend\ServiceManager\ServiceManager;

class ServiceListenerFactoryTest extends TestCase
{
public function setUp()
{
$sm = $this->sm = $this->getMockBuilder('Zend\ServiceManager\ServiceManager')
$sm = $this->sm = $this->getMockBuilder(ServiceManager::class)
->setMethods(['get'])
->getMock();

$this->factory = new ServiceListenerFactory();
}

private function isServiceManagerV3()
{
$r = new ReflectionClass(ServiceManager::class);
return $r->hasMethod('configure');
}

/**
* @expectedException Zend\ServiceManager\Exception\ServiceNotCreatedException
* @expectedExceptionMessage The value of service_listener_options must be an array, string given.
Expand Down Expand Up @@ -191,4 +199,78 @@ public function testDefinesExpectedAliasesForConsole()
$this->assertArrayHasKey('console', $config['aliases'], 'Missing "console" alias from default service config');
$this->assertArrayHasKey('Console', $config['aliases'], 'Missing "Console" alias from default service config');
}

public function testDefinesExpectedApplicationAliasesUnderV3()
{
if (! $this->isServiceManagerV3()) {
$this->markTestSkipped('Application aliases are only defined under zend-servicemanager v3');
}

$r = new ReflectionProperty($this->factory, 'defaultServiceConfig');
$r->setAccessible(true);
$config = $r->getValue($this->factory);

// @codingStandardsIgnoreStart
$this->assertArrayHasKey('aliases', $config, 'Missing aliases from default service config');
$this->assertArrayHasKey('application', $config['aliases'], 'Missing "application" alias from default service config');
// @codingStandardsIgnoreEnd
}

public function testDefinesExpectedConfigAliasesUnderV3()
{
if (! $this->isServiceManagerV3()) {
$this->markTestSkipped('Config aliases are only defined under zend-servicemanager v3');
}

$r = new ReflectionProperty($this->factory, 'defaultServiceConfig');
$r->setAccessible(true);
$config = $r->getValue($this->factory);

$this->assertArrayHasKey('aliases', $config, 'Missing aliases from default service config');
$this->assertArrayHasKey('Config', $config['aliases'], 'Missing "Config" alias from default service config');
}

public function testDefinesExpectedRequestAliasesUnderV3()
{
if (! $this->isServiceManagerV3()) {
$this->markTestSkipped('Request aliases are only defined under zend-servicemanager v3');
}

$r = new ReflectionProperty($this->factory, 'defaultServiceConfig');
$r->setAccessible(true);
$config = $r->getValue($this->factory);

$this->assertArrayHasKey('aliases', $config, 'Missing aliases from default service config');
$this->assertArrayHasKey('request', $config['aliases'], 'Missing "request" alias from default service config');
}

public function testDefinesExpectedResponseFactories()
{
if (! $this->isServiceManagerV3()) {
$this->markTestSkipped('Response aliases are only defined under zend-servicemanager v3');
}

$r = new ReflectionProperty($this->factory, 'defaultServiceConfig');
$r->setAccessible(true);
$config = $r->getValue($this->factory);

// @codingStandardsIgnoreStart
$this->assertArrayHasKey('aliases', $config, 'Missing aliases from default service config');
$this->assertArrayHasKey('response', $config['aliases'], 'Missing "response" alias from default service config');
// @codingStandardsIgnoreEnd
}

public function testDefinesExpectedRouterAliases()
{
if (! $this->isServiceManagerV3()) {
$this->markTestSkipped('Router aliases are only defined under zend-servicemanager v3');
}

$r = new ReflectionProperty($this->factory, 'defaultServiceConfig');
$r->setAccessible(true);
$config = $r->getValue($this->factory);

$this->assertArrayHasKey('aliases', $config, 'Missing aliases from default service config');
$this->assertArrayHasKey('router', $config['aliases'], 'Missing "router" alias from default service config');
}
}
2 changes: 1 addition & 1 deletion test/Service/ViewHelperManagerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function testUrlHelperFactoryCanBeInvokedViaShortNameOrFullClassName($nam

$this->services->setService('HttpRouter', $router);
$this->services->setService('Router', $router);
$this->services->setService('application', $application->reveal());
$this->services->setService('Application', $application->reveal());
$this->services->setService('config', []);

$manager = $this->factory->createService($this->services);
Expand Down