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

Commit 9babf3f

Browse files
committed
Merge branch 'hotfix/71'
Close #71 Fixes zendframework/zend-view#51
2 parents 9181d0e + c88fb6e commit 9babf3f

5 files changed

+262
-46
lines changed

CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5-
## 2.6.2 - TBD
5+
## 2.6.2 - 2016-02-22
66

77
### Added
88

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

1919
### Fixed
2020

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

2329
## 2.6.1 - 2016-02-16
2430

src/Service/AbstractPluginManagerFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function createService(ServiceLocatorInterface $serviceLocator)
3131
/* @var $plugins AbstractPluginManager */
3232
$plugins = new $pluginManagerClass;
3333
$plugins->setServiceLocator($serviceLocator);
34-
$configuration = $serviceLocator->get('Config');
34+
$configuration = $serviceLocator->get('config');
3535

3636
if (isset($configuration['di']) && $serviceLocator->has('Di')) {
3737
$plugins->addAbstractFactory($serviceLocator->get('DiAbstractServiceFactory'));

src/Service/ViewHelperManagerFactory.php

Lines changed: 114 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@
99

1010
namespace Zend\Mvc\Service;
1111

12+
use Interop\Container\ContainerInterface;
1213
use Zend\Console\Console;
1314
use Zend\Mvc\Exception;
1415
use Zend\Mvc\Router\RouteMatch;
1516
use Zend\ServiceManager\ConfigInterface;
1617
use Zend\ServiceManager\ServiceLocatorInterface;
1718
use Zend\View\Helper as ViewHelper;
19+
use Zend\View\HelperPluginManager;
1820
use Zend\View\Helper\HelperInterface as ViewHelperInterface;
1921

2022
class ViewHelperManagerFactory extends AbstractPluginManagerFactory
2123
{
22-
const PLUGIN_MANAGER_CLASS = 'Zend\View\HelperPluginManager';
24+
const PLUGIN_MANAGER_CLASS = HelperPluginManager::class;
2325

2426
/**
2527
* An array of helper configuration classes to ensure are on the helper_map stack.
@@ -43,29 +45,92 @@ public function createService(ServiceLocatorInterface $serviceLocator)
4345
{
4446
$plugins = parent::createService($serviceLocator);
4547

48+
// Configure default helpers from other components
49+
$plugins = $this->configureHelpers($plugins);
50+
51+
// Override plugin factories
52+
$plugins = $this->injectOverrideFactories($plugins, $serviceLocator);
53+
54+
return $plugins;
55+
}
56+
57+
/**
58+
* Configure helpers from other components.
59+
*
60+
* Loops through the list of default helper configuration classes, and uses
61+
* each to configure the helper plugin manager.
62+
*
63+
* @param HelperPluginManager $plugins
64+
* @return HelperPluginManager
65+
*/
66+
private function configureHelpers(HelperPluginManager $plugins)
67+
{
4668
foreach ($this->defaultHelperMapClasses as $configClass) {
47-
if (is_string($configClass) && class_exists($configClass)) {
48-
$config = new $configClass;
49-
50-
if (!$config instanceof ConfigInterface) {
51-
throw new Exception\RuntimeException(sprintf(
52-
'Invalid service manager configuration class provided; received "%s", expected class implementing %s',
53-
$configClass,
54-
'Zend\ServiceManager\ConfigInterface'
55-
));
56-
}
57-
58-
$config->configureServiceManager($plugins);
69+
if (! is_string($configClass) || ! class_exists($configClass)) {
70+
continue;
71+
}
72+
73+
$config = new $configClass;
74+
75+
if (! $config instanceof ConfigInterface) {
76+
throw new Exception\RuntimeException(sprintf(
77+
'Invalid service manager configuration class provided; received "%s", expected class implementing %s',
78+
$configClass,
79+
'Zend\ServiceManager\ConfigInterface'
80+
));
5981
}
82+
83+
$config->configureServiceManager($plugins);
6084
}
6185

62-
// Configure URL view helper with router
63-
$plugins->setFactory('url', function () use ($serviceLocator) {
86+
return $plugins;
87+
}
88+
89+
/**
90+
* Inject override factories into the plugin manager.
91+
*
92+
* @param HelperPluginManager $plugins
93+
* @param ContainerInterface $services
94+
* @return HelperPluginManager
95+
*/
96+
private function injectOverrideFactories(HelperPluginManager $plugins, ContainerInterface $services)
97+
{
98+
// Configure URL view helper
99+
$urlFactory = $this->createUrlHelperFactory($services);
100+
$plugins->setFactory(ViewHelper\Url::class, $urlFactory);
101+
$plugins->setFactory('zendviewhelperurl', $urlFactory);
102+
103+
// Configure base path helper
104+
$basePathFactory = $this->createBasePathHelperFactory($services);
105+
$plugins->setFactory(ViewHelper\BasePath::class, $basePathFactory);
106+
$plugins->setFactory('zendviewhelperbasepath', $basePathFactory);
107+
108+
// Configure doctype view helper
109+
$doctypeFactory = $this->createDoctypeHelperFactory($services);
110+
$plugins->setFactory(ViewHelper\doctype::class, $doctypeFactory);
111+
$plugins->setFactory('zendviewhelperdoctype', $doctypeFactory);
112+
113+
return $plugins;
114+
}
115+
116+
/**
117+
* Create and return a factory for creating a URL helper.
118+
*
119+
* Retrieves the application and router from the servicemanager,
120+
* and the route match from the MvcEvent composed by the application,
121+
* using them to configure the helper.
122+
*
123+
* @param ContainerInterface $services
124+
* @return callable
125+
*/
126+
private function createUrlHelperFactory(ContainerInterface $services)
127+
{
128+
return function () use ($services) {
64129
$helper = new ViewHelper\Url;
65130
$router = Console::isConsole() ? 'HttpRouter' : 'Router';
66-
$helper->setRouter($serviceLocator->get($router));
131+
$helper->setRouter($services->get($router));
67132

68-
$match = $serviceLocator->get('application')
133+
$match = $services->get('application')
69134
->getMvcEvent()
70135
->getRouteMatch()
71136
;
@@ -75,10 +140,21 @@ public function createService(ServiceLocatorInterface $serviceLocator)
75140
}
76141

77142
return $helper;
78-
});
143+
};
144+
}
79145

80-
$plugins->setFactory('basepath', function () use ($serviceLocator) {
81-
$config = $serviceLocator->has('Config') ? $serviceLocator->get('Config') : [];
146+
/**
147+
* Create and return a factory for creating a BasePath helper.
148+
*
149+
* Uses configuration and request services to configure the helper.
150+
*
151+
* @param ContainerInterface $services
152+
* @return callable
153+
*/
154+
private function createBasePathHelperFactory(ContainerInterface $services)
155+
{
156+
return function () use ($services) {
157+
$config = $services->has('config') ? $services->get('config') : [];
82158
$basePathHelper = new ViewHelper\BasePath;
83159

84160
if (Console::isConsole()
@@ -96,31 +172,35 @@ public function createService(ServiceLocatorInterface $serviceLocator)
96172
return $basePathHelper;
97173
}
98174

99-
$request = $serviceLocator->get('Request');
175+
$request = $services->get('Request');
100176

101177
if (is_callable([$request, 'getBasePath'])) {
102178
$basePathHelper->setBasePath($request->getBasePath());
103179
}
104180

105181
return $basePathHelper;
106-
});
107-
108-
/**
109-
* Configure doctype view helper with doctype from configuration, if available.
110-
*
111-
* Other view helpers depend on this to decide which spec to generate their tags
112-
* based on. This is why it must be set early instead of later in the layout phtml.
113-
*/
114-
$plugins->setFactory('doctype', function () use ($serviceLocator) {
115-
$config = $serviceLocator->has('Config') ? $serviceLocator->get('Config') : [];
182+
};
183+
}
184+
185+
/**
186+
* Create and return a Doctype helper factory.
187+
*
188+
* Other view helpers depend on this to decide which spec to generate their tags
189+
* based on. This is why it must be set early instead of later in the layout phtml.
190+
*
191+
* @param ContainerInterface $services
192+
* @return callable
193+
*/
194+
private function createDoctypeHelperFactory(ContainerInterface $services)
195+
{
196+
return function () use ($services) {
197+
$config = $services->has('config') ? $services->get('config') : [];
116198
$config = isset($config['view_manager']) ? $config['view_manager'] : [];
117199
$doctypeHelper = new ViewHelper\Doctype;
118200
if (isset($config['doctype']) && $config['doctype']) {
119201
$doctypeHelper->setDoctype($config['doctype']);
120202
}
121203
return $doctypeHelper;
122-
});
123-
124-
return $plugins;
204+
};
125205
}
126206
}

test/Service/HydratorManagerFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function setUp()
2121
{
2222
$this->factory = new HydratorManagerFactory();
2323
$this->services = $this->prophesize(ServiceLocatorInterface::class);
24-
$this->services->get('Config')->willReturn([]);
24+
$this->services->get('config')->willReturn([]);
2525
}
2626

2727
public function testFactoryReturnsZendHydratorManagerInstance()

0 commit comments

Comments
 (0)