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

Commit f0afd11

Browse files
committed
Use a delegator factory + HelperConfig
`HelperConfig` contains a lot of logic for ensuring that the navigation helpers are injected with the view helper plugin manager as well as the application container, and cannot be deprecated safely. As a result, this patch uses a new approach: a delegator factory, which uses `HelperConfig` to configure the helper manager before returning it. This approach should still be safe, as the various other approaches that configure it operate on the already retrieved helper manager instance.
1 parent f1e8c30 commit f0afd11

File tree

5 files changed

+99
-44
lines changed

5 files changed

+99
-44
lines changed

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ All notable changes to this project will be documented in this file, in reverse
77
### Added
88

99
- [#26](https://github.com/zendframework/zend-navigation/pull/26) adds:
10+
- `Zend\Navigation\View\ViewHelperManagerDelegatorFactory`, which decorates
11+
the `ViewHelperManager` service to configure it using
12+
`Zend\Navigation\View\HelperConfig`.
1013
- `ConfigProvider`, which maps the default navigation factory and the
1114
navigation abstract factory, as well as the navigation view helper.
1215
- `Module`, which does the same as the above, but for zend-mvc
1316
applications.
1417

1518
### Deprecated
1619

17-
- [#26](https://github.com/zendframework/zend-navigation/pull/26) deprecates
18-
`Zend\Navigation\View\HelperConfig`, as the functionality is now provided
19-
by the above additions.
20+
- Nothing.
2021

2122
### Removed
2223

src/ConfigProvider.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
namespace Zend\Navigation;
99

10-
use Zend\View\Helper\Havigation as NavigationHelper;
11-
1210
class ConfigProvider
1311
{
1412
/**
@@ -38,29 +36,11 @@ public function getDependencyConfig()
3836
'aliases' => [
3937
'navigation' => Navigation::class,
4038
],
41-
'factories' => [
42-
Navigation::class => Service/DefaultNavigationFactory::class,
43-
],
44-
];
45-
}
46-
47-
/**
48-
* Return zend-navigation helper configuration.
49-
*
50-
* Obsoletes View\HelperConfig.
51-
*
52-
* @return array
53-
*/
54-
public function getViewHelperConfig()
55-
{
56-
return [
57-
'aliases' => [
58-
'navigation' => NavigationHelper::class,
59-
'Navigation' => NavigationHelper::class,
39+
'delegators' => [
40+
'ViewHelperManager' => [ View\ViewHelperManagerDelegatorFactory::class ],
6041
],
6142
'factories' => [
62-
NavigationHelper::class => View\NavigationHelperFactory::class,
63-
'zendviewhelpernavigation' => View\NavigationHelperFactory::class,
43+
Navigation::class => Service/DefaultNavigationFactory::class,
6444
],
6545
];
6646
}

src/View/HelperConfig.php

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use ReflectionProperty;
1313
use Traversable;
14-
use Zend\Navigation\ConfigProvider;
1514
use Zend\ServiceManager\Config;
1615
use Zend\ServiceManager\Factory\InvokableFactory;
1716
use Zend\ServiceManager\ServiceManager;
@@ -20,26 +19,30 @@
2019

2120
/**
2221
* Service manager configuration for navigation view helpers
23-
*
24-
* @deprecated since 2.7.0, by \Zend\Navigation\ConfigProvider
2522
*/
2623
class HelperConfig extends Config
2724
{
2825
/**
29-
* Default configuration keys.
26+
* Default configuration to apply.
3027
*
3128
* @var string[][]
3229
*/
3330
protected $config = [
3431
'abstract_factories' => [],
35-
'aliases' => [],
36-
'delegators' => [],
37-
'factories' => [],
38-
'initializers' => [],
39-
'invokables' => [],
40-
'lazy_services' => [],
41-
'services' => [],
42-
'shared' => [],
32+
'aliases' => [
33+
'navigation' => NavigationHelper::class,
34+
'Navigation' => NavigationHelper::class,
35+
],
36+
'delegators' => [],
37+
'factories' => [
38+
NavigationHelper::class => NavigationHelperFactory::class,
39+
'zendviewhelpernavigation' => NavigationHelperFactory::class,
40+
],
41+
'initializers' => [],
42+
'invokables' => [],
43+
'lazy_services' => [],
44+
'services' => [],
45+
'shared' => [],
4346
];
4447

4548
/**
@@ -52,17 +55,12 @@ class HelperConfig extends Config
5255
/**
5356
* Constructor.
5457
*
55-
* Imports the configuration from the ConfigProvider, and then ensures
56-
* incoming configuration is merged with it.
58+
* Ensure incoming configuration is *merged* with the defaults defined.
5759
*
5860
* @param array
5961
*/
6062
public function __construct(array $config = [])
6163
{
62-
$this->config = ArrayUtils::merge(
63-
$this->config,
64-
(new ConfigProvider())->getViewHelperConfig()
65-
);
6664
$this->mergeConfig($config);
6765
}
6866

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* @link http://github.com/zendframework/zend-navigation for the canonical source repository
4+
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license http://framework.zend.com/license/new-bsd New BSD License
6+
*/
7+
8+
namespace Zend\Navigation\View;
9+
10+
use Interop\Container\ContainerInterface;
11+
use Zend\ServiceManager\DelegatorFactoryInterface;
12+
use Zend\ServiceManager\ServiceLocatorInterface;
13+
14+
/**
15+
* Inject the zend-view HelperManager with zend-navigation view helper configuration.
16+
*
17+
* This approach is used for backwards compatibility. The HelperConfig class performs
18+
* work to ensure that the navigation helper and all its sub-helpers are injected
19+
* with the view helper manager and application container.
20+
*/
21+
class ViewHelperManagerDelegatorFactory implements DelegatorFactoryInterface
22+
{
23+
/**
24+
* {@inheritDoc}
25+
*
26+
* @return \Zend\View\HelperPluginManager
27+
*/
28+
public function __invoke(ContainerInterface $container, $name, callable $callback, array $options = [])
29+
{
30+
$viewHelpers = $callback();
31+
(new HelperConfig())->configureServiceManager($viewHelpers);
32+
return $viewHelpers;
33+
}
34+
35+
/**
36+
* {@inheritDoc}
37+
*
38+
* @return \Zend\View\HelperPluginManager
39+
*/
40+
public function createDelegatorWithName(ServiceLocatorInterface $container, $name, $requestedName, $callback)
41+
{
42+
return $this($container, $requestedName, $callback);
43+
}
44+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* @link http://github.com/zendframework/zend-navigation for the canonical source repository
4+
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license http://framework.zend.com/license/new-bsd New BSD License
6+
*/
7+
8+
namespace ZendTest\Navigation\View;
9+
10+
use PHPUnit_Framework_TestCase as TestCase;
11+
use Zend\Navigation\View\ViewHelperManagerDelegatorFactory;
12+
use Zend\ServiceManager\ServiceManager;
13+
use Zend\View\HelperPluginManager;
14+
use Zend\View\Helper\Navigation as NavigationHelper;
15+
16+
class ViewHelperManagerDelegatorFactoryTest extends TestCase
17+
{
18+
public function testFactoryConfiguresViewHelperManagerWithNavigationHelpers()
19+
{
20+
$services = new ServiceManager();
21+
$helpers = new HelperPluginManager($services);
22+
$callback = function () use ($helpers) {
23+
return $helpers;
24+
};
25+
26+
$factory = new ViewHelperManagerDelegatorFactory();
27+
$this->assertSame($helpers, $factory($services, 'ViewHelperManager', $callback));
28+
29+
$this->assertTrue($helpers->has('navigation'));
30+
$this->assertTrue($helpers->has(NavigationHelper::class));
31+
}
32+
}

0 commit comments

Comments
 (0)