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

Commit 5ac36c4

Browse files
committed
Merge branch 'feature/60' into develop
Close #60
2 parents ce48ee0 + f9303d4 commit 5ac36c4

File tree

4 files changed

+41
-15
lines changed

4 files changed

+41
-15
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ All notable changes to this project will be documented in this file, in reverse
1010
method to the `partialLoop()` helper, allowing the ability to chain setters
1111
with rendering:
1212
`$this->partialLoop()->setObjectKey('foo')->loop('partial', $data)`
13+
- [#60](https://github.com/zendframework/zend-view/pull/60) adds the ability to
14+
register and consume arbitrary callables as view helpers within the
15+
`HelperPluginManager`.
1316

1417
### Deprecated
1518

src/HelperPluginManager.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,6 @@ class HelperPluginManager extends AbstractPluginManager
137137
'ViewModel' => Helper\ViewModel::class,
138138
];
139139

140-
protected $instanceOf = Helper\HelperInterface::class;
141-
142140
/**
143141
* Default factories
144142
*
@@ -289,6 +287,10 @@ public function injectRenderer($first, $second)
289287
? $second
290288
: $first;
291289

290+
if (! $helper instanceof Helper\HelperInterface) {
291+
return;
292+
}
293+
292294
$renderer = $this->getRenderer();
293295
if (null === $renderer) {
294296
return;
@@ -393,19 +395,19 @@ public function injectEventManager($first, $second)
393395
/**
394396
* Validate the plugin is of the expected type (v3).
395397
*
396-
* Validates against `$instanceOf`.
398+
* Validates against callables and HelperInterface implementations.
397399
*
398400
* @param mixed $instance
399401
* @throws InvalidServiceException
400402
*/
401403
public function validate($instance)
402404
{
403-
if (!$instance instanceof $this->instanceOf) {
405+
if (! is_callable($instance) && ! $instance instanceof Helper\HelperInterface) {
404406
throw new InvalidServiceException(
405407
sprintf(
406-
'%s can only create instances of %s; %s is invalid',
408+
'%s can only create instances of %s and/or callables; %s is invalid',
407409
get_class($this),
408-
$this->instanceOf,
410+
Helper\HelperInterface::class,
409411
(is_object($instance) ? get_class($instance) : gettype($instance))
410412
)
411413
);

test/HelperPluginManagerCompatibilityTest.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111

1212
use PHPUnit_Framework_TestCase as TestCase;
1313
use ReflectionProperty;
14-
use Zend\Mvc\Controller\PluginManager as ControllerPluginManager;
1514
use Zend\Mvc\Controller\Plugin\FlashMessenger;
15+
use Zend\Mvc\Controller\PluginManager as ControllerPluginManager;
1616
use Zend\ServiceManager\Config;
17-
use Zend\View\Exception\InvalidHelperException;
18-
use Zend\View\Helper\HelperInterface;
19-
use Zend\View\HelperPluginManager;
2017
use Zend\ServiceManager\ServiceManager;
2118
use Zend\ServiceManager\Test\CommonPluginManagerTrait;
19+
use Zend\View\Exception\InvalidHelperException;
20+
use Zend\View\HelperPluginManager;
2221

2322
class HelperPluginManagerCompatibilityTest extends TestCase
2423
{
@@ -56,11 +55,6 @@ protected function getV2InvalidPluginException()
5655
return InvalidHelperException::class;
5756
}
5857

59-
protected function getInstanceOf()
60-
{
61-
return HelperInterface::class;
62-
}
63-
6458
public function aliasProvider()
6559
{
6660
$pluginManager = $this->getPluginManager();
@@ -82,4 +76,14 @@ public function aliasProvider()
8276
yield $alias => [$alias, $target];
8377
}
8478
}
79+
80+
public function getInstanceOf()
81+
{
82+
// no-op; instanceof is not used in this implementation
83+
}
84+
85+
public function testInstanceOfMatches()
86+
{
87+
$this->markTestSkipped('instanceOf is not used with this implementation');
88+
}
8589
}

test/HelperPluginManagerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,23 @@ public function testCanOverrideAFactoryViaConfigurationPassedToConstructor()
194194
$this->assertSame($helper, $helpers->get('url'));
195195
}
196196

197+
public function testCanUseCallableAsHelper()
198+
{
199+
$helper = function () {};
200+
$helpers = new HelperPluginManager(new ServiceManager());
201+
$config = new Config(
202+
[
203+
'factories' => [
204+
'foo' => function ($container) use ($helper) {
205+
return $helper;
206+
},
207+
]
208+
]
209+
);
210+
$config->configureServiceManager($helpers);
211+
$this->assertSame($helper, $helpers->get('foo'));
212+
}
213+
197214
private function getServiceNotFoundException(HelperPluginManager $manager)
198215
{
199216
if (method_exists($manager, 'configure')) {

0 commit comments

Comments
 (0)