Skip to content

Commit 6bf29d1

Browse files
author
Kyle Spraggs
committed
Added magic __call method to proxy to plugin().
1 parent 59bf6a6 commit 6bf29d1

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

src/ZfcTwig/View/Renderer/TwigRenderer.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
namespace ZfcTwig\View\Renderer;
44

5-
use RuntimeException;
65
use Twig_Environment;
76
use Zend\View\Exception;
87
use Zend\View\HelperPluginManager;
98
use Zend\View\Model\ModelInterface;
109
use Zend\View\Renderer\RendererInterface;
1110
use Zend\View\Resolver\ResolverInterface;
12-
1311
use ZfcTwig\View\Resolver\TwigResolver;
1412

1513
class TwigRenderer implements RendererInterface
@@ -30,14 +28,44 @@ class TwigRenderer implements RendererInterface
3028
protected $resolver;
3129

3230
/**
33-
* @param Twig_Environment $environment
31+
* @var array Cache for the plugin call
32+
*/
33+
private $__pluginCache = array();
34+
35+
/**
36+
* @param \Twig_Environment $environment
37+
* @param TwigResolver $resolver
3438
*/
3539
public function __construct(Twig_Environment $environment, TwigResolver $resolver)
3640
{
3741
$this->environment = $environment;
3842
$this->resolver = $resolver;
3943
}
4044

45+
/**
46+
* Overloading: proxy to helpers
47+
*
48+
* Proxies to the attached plugin manager to retrieve, return, and potentially
49+
* execute helpers.
50+
*
51+
* * If the helper does not define __invoke, it will be returned
52+
* * If the helper does define __invoke, it will be called as a functor
53+
*
54+
* @param string $method
55+
* @param array $argv
56+
* @return mixed
57+
*/
58+
public function __call($method, $argv)
59+
{
60+
if (!isset($this->__pluginCache[$method])) {
61+
$this->__pluginCache[$method] = $this->plugin($method);
62+
}
63+
if (is_callable($this->__pluginCache[$method])) {
64+
return call_user_func_array($this->__pluginCache[$method], $argv);
65+
}
66+
return $this->__pluginCache[$method];
67+
}
68+
4169
/**
4270
* Get plugin instance, proxy to HelperPluginManager::get
4371
*

0 commit comments

Comments
 (0)