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

Commit 27a5e40

Browse files
committed
Merge branch 'hotfix/71' into develop
Forward port #71
2 parents 886554a + c88fb6e commit 27a5e40

File tree

4 files changed

+197
-141
lines changed

4 files changed

+197
-141
lines changed

CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ All notable changes to this project will be documented in this file, in reverse
9191
factories by default. You can force it to do so by passing an optional
9292
second argument, a boolean flag, with a value of boolean true.
9393

94-
## 2.6.2 - TBD
94+
## 2.6.2 - 2016-02-22
9595

9696
### Added
9797

@@ -107,7 +107,13 @@ All notable changes to this project will be documented in this file, in reverse
107107

108108
### Fixed
109109

110-
- Nothing.
110+
- [#71](https://github.com/zendframework/zend-mvc/pull/71) fixes the
111+
`ViewHelperManagerFactory` to be backwards-compatible with v2 by ensuring that
112+
the factories for each of the `url`, `basepath`, and `doctype` view helpers
113+
are registered using the fully qualified class names present in
114+
`Zend\View\HelperPluginManager`; these changes ensure requests for these
115+
helpers resolve to these override factories, instead of the
116+
`InvokableFactory`.
111117

112118
## 2.6.1 - 2016-02-16
113119

src/Service/ViewHelperManagerFactory.php

Lines changed: 75 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,29 @@ class ViewHelperManagerFactory extends AbstractPluginManagerFactory
4747
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
4848
{
4949
$options = $options ?: [];
50+
$options['factories'] = isset($options['factories']) ? $options['factories'] : [];
51+
$plugins = parent::__invoke($container, $requestedName, $options);
52+
53+
// Configure default helpers from other components
54+
$plugins = $this->configureHelpers($plugins);
5055

56+
// Override plugin factories
57+
$plugins = $this->injectOverrideFactories($plugins, $serviceLocator);
58+
59+
return $plugins;
60+
}
61+
62+
/**
63+
* Configure helpers from other components.
64+
*
65+
* Loops through the list of default helper configuration classes, and uses
66+
* each to configure the helper plugin manager.
67+
*
68+
* @param HelperPluginManager $plugins
69+
* @return HelperPluginManager
70+
*/
71+
private function configureHelpers(HelperPluginManager $plugins)
72+
{
5173
foreach ($this->defaultHelperMapClasses as $configClass) {
5274
if (! is_string($configClass) || ! class_exists($configClass)) {
5375
continue;
@@ -66,35 +88,55 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
6688
$options = ArrayUtils::merge($options, $config->toArray());
6789
}
6890

69-
$config = $container->has('config') ? $container->get('config') : [];
70-
71-
$options['factories'] = isset($options['factories']) ? $options['factories'] : [];
72-
73-
// Configure URL view helper factory
74-
$options['factories'][ViewHelper\Url::class] = $this->createUrlHelperFactory();
75-
76-
// Configure basepath view helper factory
77-
$options['factories'][ViewHelper\BasePath::class] = $this->createBasePathHelperFactory($config);
78-
79-
// Configure doctype view helper factory
80-
$options['factories'][ViewHelper\Doctype::class] = $this->createDoctypeHelperFactory();
91+
$plugins->configure($options);
92+
return $plugins;
93+
}
8194

82-
return parent::__invoke($container, $requestedName, $options);
95+
/**
96+
* Inject override factories into the plugin manager.
97+
*
98+
* @param HelperPluginManager $plugins
99+
* @param ContainerInterface $services
100+
* @return HelperPluginManager
101+
*/
102+
private function injectOverrideFactories(HelperPluginManager $plugins, ContainerInterface $services)
103+
{
104+
// Configure URL view helper
105+
$urlFactory = $this->createUrlHelperFactory($services);
106+
$plugins->setFactory(ViewHelper\Url::class, $urlFactory);
107+
$plugins->setFactory('zendviewhelperurl', $urlFactory);
108+
109+
// Configure base path helper
110+
$basePathFactory = $this->createBasePathHelperFactory($services);
111+
$plugins->setFactory(ViewHelper\BasePath::class, $basePathFactory);
112+
$plugins->setFactory('zendviewhelperbasepath', $basePathFactory);
113+
114+
// Configure doctype view helper
115+
$doctypeFactory = $this->createDoctypeHelperFactory($services);
116+
$plugins->setFactory(ViewHelper\doctype::class, $doctypeFactory);
117+
$plugins->setFactory('zendviewhelperdoctype', $doctypeFactory);
118+
119+
return $plugins;
83120
}
84121

85122
/**
86-
* Create a factory for the "url" view helper
123+
* Create and return a factory for creating a URL helper.
124+
*
125+
* Retrieves the application and router from the servicemanager,
126+
* and the route match from the MvcEvent composed by the application,
127+
* using them to configure the helper.
87128
*
129+
* @param ContainerInterface $services
88130
* @return callable
89131
*/
90-
private function createUrlHelperFactory()
132+
private function createUrlHelperFactory(ContainerInterface $services)
91133
{
92-
return function ($container, $name, array $options = null) {
134+
return function () use ($services) {
93135
$helper = new ViewHelper\Url;
94136
$router = Console::isConsole() ? 'HttpRouter' : 'Router';
95-
$helper->setRouter($container->get($router));
137+
$helper->setRouter($services->get($router));
96138

97-
$match = $container->get('application')
139+
$match = $services->get('application')
98140
->getMvcEvent()
99141
->getRouteMatch()
100142
;
@@ -107,16 +149,17 @@ private function createUrlHelperFactory()
107149
};
108150
}
109151

110-
/**
111-
* Create a factory for the "basepath" view helper
152+
* Create and return a factory for creating a BasePath helper.
112153
*
113-
* @param array $config
154+
* Uses configuration and request services to configure the helper.
155+
*
156+
* @param ContainerInterface $services
114157
* @return callable
115158
*/
116-
private function createBasePathHelperFactory($config)
159+
private function createBasePathHelperFactory(ContainerInterface $services)
117160
{
118-
return function ($container, $name, array $options = null) {
119-
$config = $container->has('config') ? $container->get('config') : [];
161+
return function () use ($services) {
162+
$config = $services->has('config') ? $services->get('config') : [];
120163
$helper = new ViewHelper\BasePath;
121164

122165
if (Console::isConsole()
@@ -131,7 +174,8 @@ private function createBasePathHelperFactory($config)
131174
return $helper;
132175
}
133176

134-
$request = $container->get('Request');
177+
$request = $services->get('Request');
178+
135179
if (is_callable([$request, 'getBasePath'])) {
136180
$helper->setBasePath($request->getBasePath());
137181
}
@@ -141,19 +185,18 @@ private function createBasePathHelperFactory($config)
141185
}
142186

143187
/**
144-
* Configure doctype view helper with doctype from configuration, if available.
188+
* Create and return a Doctype helper factory.
145189
*
146190
* Other view helpers depend on this to decide which spec to generate their tags
147-
* based on.
148-
*
149-
* This is why it must be set early instead of later in the layout phtml.
191+
* based on. This is why it must be set early instead of later in the layout phtml.
150192
*
193+
* @param ContainerInterface $services
151194
* @return callable
152195
*/
153-
private function createDoctypeHelperFactory()
196+
private function createDoctypeHelperFactory(ContainerInterface $services)
154197
{
155-
return function ($container, $name, array $options = null) {
156-
$config = $container->has('config') ? $container->get('config') : [];
198+
return function () use ($services) {
199+
$config = $services->has('config') ? $services->get('config') : [];
157200
$config = isset($config['view_manager']) ? $config['view_manager'] : [];
158201
$helper = new ViewHelper\Doctype;
159202
if (isset($config['doctype']) && $config['doctype']) {

test/Service/HydratorManagerFactoryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class HydratorManagerFactoryTest extends TestCase
1919
public function setUp()
2020
{
2121
$this->factory = new HydratorManagerFactory();
22-
$this->services = $this->prophesize(ContainerInterface::class);
22+
$this->services = $this->prophesize(ServiceLocatorInterface::class);
23+
$this->services->get('config')->willReturn([]);
2324
}
2425

2526
public function testFactoryReturnsZendHydratorManagerInstance()

0 commit comments

Comments
 (0)