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

Commit a8c1a06

Browse files
committed
Conditional aliases
Updated the `ServiceListenerFactory` to conditionally add either Titlecase or lowercase aliases under zend-servicemanager v3 for common one-word factories (currently config, request, response, router, and application). This avoids the circular alias issue under v2 (where the normalized factory name ensures all cases match), while simultaneously ensuring common usage scenarios continue to work (fetching by alternate case).
1 parent c11d189 commit a8c1a06

File tree

3 files changed

+121
-5
lines changed

3 files changed

+121
-5
lines changed

Diff for: src/Service/ServiceListenerFactory.php

+38-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
namespace Zend\Mvc\Service;
1111

1212
use Interop\Container\ContainerInterface;
13+
use ReflectionClass;
14+
use Zend\Config\Config;
1315
use Zend\ModuleManager\Listener\ServiceListener;
1416
use Zend\ModuleManager\Listener\ServiceListenerInterface;
17+
use Zend\Mvc\Application;
1518
use Zend\Mvc\View;
1619
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
1720
use Zend\ServiceManager\FactoryInterface;
@@ -38,8 +41,8 @@ class ServiceListenerFactory implements FactoryInterface
3841
*/
3942
protected $defaultServiceConfig = [
4043
'aliases' => [
41-
'Configuration' => 'config',
4244
'configuration' => 'config',
45+
'Configuration' => 'config',
4346
'console' => 'ConsoleAdapter',
4447
'Console' => 'ConsoleAdapter',
4548
'ConsoleDefaultRenderingStrategy' => View\Console\DefaultRenderingStrategy::class,
@@ -65,8 +68,7 @@ class ServiceListenerFactory implements FactoryInterface
6568
],
6669
'invokables' => [],
6770
'factories' => [
68-
'Application' => 'Zend\Mvc\Service\ApplicationFactory',
69-
'application' => 'Zend\Mvc\Service\ApplicationFactory',
71+
'Application' => ApplicationFactory::class,
7072
'config' => 'Zend\Mvc\Service\ConfigFactory',
7173
'ControllerManager' => 'Zend\Mvc\Service\ControllerManagerFactory',
7274
'ControllerPluginManager' => 'Zend\Mvc\Service\ControllerPluginManagerFactory',
@@ -122,6 +124,20 @@ class ServiceListenerFactory implements FactoryInterface
122124
],
123125
];
124126

127+
/**
128+
* Constructor
129+
*
130+
* When executed under zend-servicemanager v3, injects additional aliases
131+
* to ensure backwards compatibility.
132+
*/
133+
public function __construct()
134+
{
135+
$r = new ReflectionClass(ServiceLocatorInterface::class);
136+
if ($r->hasMethod('build')) {
137+
$this->injectV3Aliases();
138+
}
139+
}
140+
125141
/**
126142
* Create the service listener service
127143
*
@@ -276,4 +292,23 @@ private function validatePluginManagerOptions($options, $name)
276292
));
277293
}
278294
}
295+
296+
/**
297+
* Inject additional aliases for zend-servicemanager v3 usage
298+
*
299+
* If the constructor detects that we're operating under zend-servicemanager v3,
300+
* this method injects additional aliases to ensure that common services
301+
* can be retrieved using both Titlecase and lowercase, and will get the
302+
* same instances.
303+
*
304+
* @return void
305+
*/
306+
private function injectV3Aliases()
307+
{
308+
$this->defaultServiceConfig['aliases']['application'] = 'Application';
309+
$this->defaultServiceConfig['aliases']['Config'] = 'config';
310+
$this->defaultServiceConfig['aliases']['request'] = 'Request';
311+
$this->defaultServiceConfig['aliases']['response'] = 'Response';
312+
$this->defaultServiceConfig['aliases']['router'] = 'Router';
313+
}
279314
}

Diff for: test/Service/ServiceListenerFactoryTest.php

+83-1
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,28 @@
1010
namespace ZendTest\Mvc\Service;
1111

1212
use PHPUnit_Framework_TestCase as TestCase;
13+
use ReflectionClass;
1314
use ReflectionProperty;
1415
use Zend\Mvc\Service\ServiceListenerFactory;
16+
use Zend\ServiceManager\ServiceManager;
1517

1618
class ServiceListenerFactoryTest extends TestCase
1719
{
1820
public function setUp()
1921
{
20-
$sm = $this->sm = $this->getMockBuilder('Zend\ServiceManager\ServiceManager')
22+
$sm = $this->sm = $this->getMockBuilder(ServiceManager::class)
2123
->setMethods(['get'])
2224
->getMock();
2325

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

29+
private function isServiceManagerV3()
30+
{
31+
$r = new ReflectionClass(ServiceManager::class);
32+
return $r->hasMethod('configure');
33+
}
34+
2735
/**
2836
* @expectedException Zend\ServiceManager\Exception\ServiceNotCreatedException
2937
* @expectedExceptionMessage The value of service_listener_options must be an array, string given.
@@ -191,4 +199,78 @@ public function testDefinesExpectedAliasesForConsole()
191199
$this->assertArrayHasKey('console', $config['aliases'], 'Missing "console" alias from default service config');
192200
$this->assertArrayHasKey('Console', $config['aliases'], 'Missing "Console" alias from default service config');
193201
}
202+
203+
public function testDefinesExpectedApplicationAliasesUnderV3()
204+
{
205+
if (! $this->isServiceManagerV3()) {
206+
$this->markTestSkipped('Application aliases are only defined under zend-servicemanager v3');
207+
}
208+
209+
$r = new ReflectionProperty($this->factory, 'defaultServiceConfig');
210+
$r->setAccessible(true);
211+
$config = $r->getValue($this->factory);
212+
213+
// @codingStandardsIgnoreStart
214+
$this->assertArrayHasKey('aliases', $config, 'Missing aliases from default service config');
215+
$this->assertArrayHasKey('application', $config['aliases'], 'Missing "application" alias from default service config');
216+
// @codingStandardsIgnoreEnd
217+
}
218+
219+
public function testDefinesExpectedConfigAliasesUnderV3()
220+
{
221+
if (! $this->isServiceManagerV3()) {
222+
$this->markTestSkipped('Config aliases are only defined under zend-servicemanager v3');
223+
}
224+
225+
$r = new ReflectionProperty($this->factory, 'defaultServiceConfig');
226+
$r->setAccessible(true);
227+
$config = $r->getValue($this->factory);
228+
229+
$this->assertArrayHasKey('aliases', $config, 'Missing aliases from default service config');
230+
$this->assertArrayHasKey('Config', $config['aliases'], 'Missing "Config" alias from default service config');
231+
}
232+
233+
public function testDefinesExpectedRequestAliasesUnderV3()
234+
{
235+
if (! $this->isServiceManagerV3()) {
236+
$this->markTestSkipped('Request aliases are only defined under zend-servicemanager v3');
237+
}
238+
239+
$r = new ReflectionProperty($this->factory, 'defaultServiceConfig');
240+
$r->setAccessible(true);
241+
$config = $r->getValue($this->factory);
242+
243+
$this->assertArrayHasKey('aliases', $config, 'Missing aliases from default service config');
244+
$this->assertArrayHasKey('request', $config['aliases'], 'Missing "request" alias from default service config');
245+
}
246+
247+
public function testDefinesExpectedResponseFactories()
248+
{
249+
if (! $this->isServiceManagerV3()) {
250+
$this->markTestSkipped('Response aliases are only defined under zend-servicemanager v3');
251+
}
252+
253+
$r = new ReflectionProperty($this->factory, 'defaultServiceConfig');
254+
$r->setAccessible(true);
255+
$config = $r->getValue($this->factory);
256+
257+
// @codingStandardsIgnoreStart
258+
$this->assertArrayHasKey('aliases', $config, 'Missing aliases from default service config');
259+
$this->assertArrayHasKey('response', $config['aliases'], 'Missing "response" alias from default service config');
260+
// @codingStandardsIgnoreEnd
261+
}
262+
263+
public function testDefinesExpectedRouterAliases()
264+
{
265+
if (! $this->isServiceManagerV3()) {
266+
$this->markTestSkipped('Router aliases are only defined under zend-servicemanager v3');
267+
}
268+
269+
$r = new ReflectionProperty($this->factory, 'defaultServiceConfig');
270+
$r->setAccessible(true);
271+
$config = $r->getValue($this->factory);
272+
273+
$this->assertArrayHasKey('aliases', $config, 'Missing aliases from default service config');
274+
$this->assertArrayHasKey('router', $config['aliases'], 'Missing "router" alias from default service config');
275+
}
194276
}

Diff for: test/Service/ViewHelperManagerFactoryTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ public function testUrlHelperFactoryCanBeInvokedViaShortNameOrFullClassName($nam
123123

124124
$this->services->setService('HttpRouter', $router);
125125
$this->services->setService('Router', $router);
126-
$this->services->setService('application', $application->reveal());
127126
$this->services->setService('Application', $application->reveal());
128127
$this->services->setService('config', []);
129128

0 commit comments

Comments
 (0)