1: <?php
2: /**
3: * This file is part of the Autarky package.
4: *
5: * (c) Andreas Lutro <anlutro@gmail.com>
6: *
7: * For the full copyright and license information, please view the LICENSE
8: * file that was distributed with this source code.
9: */
10:
11: namespace Autarky\TwigTemplating\Extensions;
12:
13: use Twig_Extension;
14: use Twig_SimpleFunction;
15:
16: use Autarky\Container\ContainerInterface;
17:
18: /**
19: * Extension to provide richer partial views. Adds the partial() function to all
20: * twig templates. See PartialExtension::getPartial() for method signature.
21: */
22: class PartialExtension extends Twig_Extension
23: {
24: /**
25: * @var ContainerInterface
26: */
27: protected $container;
28:
29: /**
30: * Constructor.
31: *
32: * @param ContainerInterface $container
33: */
34: public function __construct(ContainerInterface $container)
35: {
36: $this->container = $container;
37: }
38:
39: /**
40: * {@inheritdoc}
41: */
42: public function getFunctions()
43: {
44: return [
45: new Twig_SimpleFunction('partial', [$this, 'getPartial'], ['is_safe' => ['html']]),
46: ];
47: }
48:
49: /**
50: * The implementation of the twig partial() function.
51: *
52: * @param array $name ['ClassName', 'method']
53: * @param array $params
54: *
55: * @return string
56: */
57: public function getPartial(array $name, array $params = array())
58: {
59: list($class, $method) = $name;
60: $obj = $this->container->resolve($class);
61: return call_user_func_array([$obj, $method], $params);
62: }
63:
64: /**
65: * {@inheritdoc}
66: */
67: public function getName()
68: {
69: return 'partial';
70: }
71: }
72: