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

Commit e649aca

Browse files
committed
Merge branch 'hotfix/80' into develop
Close #80 Close #82 Fixes #78
2 parents ae54c5f + 055c3da commit e649aca

File tree

5 files changed

+127
-22
lines changed

5 files changed

+127
-22
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ All notable changes to this project will be documented in this file, in reverse
6666
[#76](https://github.com/zendframework/zend-mvc/pull/76) update the component
6767
to be forwards-compatible with zend-eventmanager v3.
6868
- [#36](https://github.com/zendframework/zend-mvc/pull/36),
69-
[#76](https://github.com/zendframework/zend-mvc/pull/76), and
70-
[#81](https://github.com/zendframework/zend-mvc/pull/81) update the component
69+
[#76](https://github.com/zendframework/zend-mvc/pull/76),
70+
[#80](https://github.com/zendframework/zend-mvc/pull/80),
71+
[#81](https://github.com/zendframework/zend-mvc/pull/81), and
72+
[#82](https://github.com/zendframework/zend-mvc/pull/82) update the component
7173
to be forwards-compatible with zend-servicemanager v3. Several changes were
7274
introduced to support this effort:
7375
- Added a `RouteInvokableFactory`, which can act as either a

src/Service/ServiceListenerFactory.php

Lines changed: 38 additions & 2 deletions
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,7 +68,7 @@ class ServiceListenerFactory implements FactoryInterface
6568
],
6669
'invokables' => [],
6770
'factories' => [
68-
'Application' => 'Zend\Mvc\Service\ApplicationFactory',
71+
'Application' => ApplicationFactory::class,
6972
'config' => 'Zend\Mvc\Service\ConfigFactory',
7073
'ControllerManager' => 'Zend\Mvc\Service\ControllerManagerFactory',
7174
'ControllerPluginManager' => 'Zend\Mvc\Service\ControllerPluginManagerFactory',
@@ -121,6 +124,20 @@ class ServiceListenerFactory implements FactoryInterface
121124
],
122125
];
123126

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+
124141
/**
125142
* Create the service listener service
126143
*
@@ -275,4 +292,23 @@ private function validatePluginManagerOptions($options, $name)
275292
));
276293
}
277294
}
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+
}
278314
}

src/Service/ViewHelperManagerFactory.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,11 @@ private function injectOverrideFactories(HelperPluginManager $plugins, Container
129129
private function createUrlHelperFactory(ContainerInterface $services)
130130
{
131131
return function () use ($services) {
132-
// zend-servicemanager v2: fetch parent locator
133-
if (method_exists($services, 'getServiceLocator') && ! method_exists($services, 'configure')) {
134-
$services = $services->getServiceLocator() ?: $services;
135-
}
136-
137132
$helper = new ViewHelper\Url;
138133
$router = Console::isConsole() ? 'HttpRouter' : 'Router';
139134
$helper->setRouter($services->get($router));
140135

141-
$match = $services->get('application')
136+
$match = $services->get('Application')
142137
->getMvcEvent()
143138
->getRouteMatch()
144139
;
@@ -162,11 +157,6 @@ private function createUrlHelperFactory(ContainerInterface $services)
162157
private function createBasePathHelperFactory(ContainerInterface $services)
163158
{
164159
return function () use ($services) {
165-
// zend-servicemanager v2: fetch parent locator
166-
if (method_exists($services, 'getServiceLocator') && ! method_exists($services, 'configure')) {
167-
$services = $services->getServiceLocator() ?: $services;
168-
}
169-
170160
$config = $services->has('config') ? $services->get('config') : [];
171161
$helper = new ViewHelper\BasePath;
172162

@@ -204,11 +194,6 @@ private function createBasePathHelperFactory(ContainerInterface $services)
204194
private function createDoctypeHelperFactory(ContainerInterface $services)
205195
{
206196
return function () use ($services) {
207-
// zend-servicemanager v2: fetch parent locator
208-
if (method_exists($services, 'getServiceLocator') && ! method_exists($services, 'configure')) {
209-
$services = $services->getServiceLocator() ?: $services;
210-
}
211-
212197
$config = $services->has('config') ? $services->get('config') : [];
213198
$config = isset($config['view_manager']) ? $config['view_manager'] : [];
214199
$helper = new ViewHelper\Doctype;

test/Service/ServiceListenerFactoryTest.php

Lines changed: 83 additions & 1 deletion
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
}

test/Service/ViewHelperManagerFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function testUrlHelperFactoryCanBeInvokedViaShortNameOrFullClassName($nam
123123

124124
$this->services->setService('HttpRouter', $router);
125125
$this->services->setService('Router', $router);
126-
$this->services->setService('application', $application->reveal());
126+
$this->services->setService('Application', $application->reveal());
127127
$this->services->setService('config', []);
128128

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

0 commit comments

Comments
 (0)