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

Commit 89f86b7

Browse files
committed
Merge pull request #60 from DASPRiD/feature/callable-view-helpers
Allow view helpers to be simple callables
2 parents ce48ee0 + 432cd80 commit 89f86b7

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

src/HelperPluginManager.php

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

140-
protected $instanceOf = Helper\HelperInterface::class;
140+
protected $instanceOf = 'callable';
141141

142142
/**
143143
* Default factories
@@ -289,6 +289,10 @@ public function injectRenderer($first, $second)
289289
? $second
290290
: $first;
291291

292+
if (! $helper instanceof Helper\HelperInterface) {
293+
return;
294+
}
295+
292296
$renderer = $this->getRenderer();
293297
if (null === $renderer) {
294298
return;
@@ -400,7 +404,17 @@ public function injectEventManager($first, $second)
400404
*/
401405
public function validate($instance)
402406
{
403-
if (!$instance instanceof $this->instanceOf) {
407+
if ('callable' === $this->instanceOf) {
408+
if (is_callable($instance)) {
409+
return;
410+
}
411+
412+
$instanceOf = Helper\HelperInterface::class;
413+
} else {
414+
$instanceOf = $this->instanceOf;
415+
}
416+
417+
if (!$instance instanceof $instanceOf) {
404418
throw new InvalidServiceException(
405419
sprintf(
406420
'%s can only create instances of %s; %s is invalid',

test/HelperPluginManagerCompatibilityTest.php

Lines changed: 4 additions & 5 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
{
@@ -58,7 +57,7 @@ protected function getV2InvalidPluginException()
5857

5958
protected function getInstanceOf()
6059
{
61-
return HelperInterface::class;
60+
return 'callable';
6261
}
6362

6463
public function aliasProvider()

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)