Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

Twig/Extension/PageTemplateTwigExtension.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\PagePartBundle\Twig\Extension;
4
5
use Kunstmaan\PagePartBundle\Helper\HasPageTemplateInterface;
6
use Kunstmaan\PagePartBundle\PageTemplate\PageTemplateConfigurationService;
7
8
/**
9
 * PagePartTwigExtension
10
 */
11
class PageTemplateTwigExtension extends \Twig_Extension
12
{
13
    /**
14
     * @var PageTemplateConfigurationService
15
     */
16
    private $templateConfiguration;
17
18
    public function __construct(PageTemplateConfigurationService $templateConfiguration)
19
    {
20
        $this->templateConfiguration = $templateConfiguration;
21
    }
22
23
    /**
24
     * @return array
25
     */
26
    public function getFunctions()
27
    {
28
        return array(
29
            new \Twig_SimpleFunction('render_pagetemplate', [$this, 'renderPageTemplate'], ['needs_environment' => true, 'needs_context' => true, 'is_safe' => ['html']]),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
30
            new \Twig_SimpleFunction('getpagetemplate', [$this, 'getPageTemplate']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
31
            new \Twig_SimpleFunction('render_pagetemplate_configuration', [$this, 'renderPageTemplateConfiguration'], ['needs_environment' => true, 'needs_context' => true, 'is_safe' => ['html']]),
32
        );
33
    }
34
35
    /**
36
     * @param \Twig_Environment        $env
37
     * @param array                    $twigContext
38
     * @param HasPageTemplateInterface $page
39
     * @param array                    $parameters
40
     *
41
     * @return string
42
     */
43 View Code Duplication
    public function renderPageTemplate(\Twig_Environment $env, array $twigContext, HasPageTemplateInterface $page, array $parameters = array())
44
    {
45
        $pageTemplates = $this->templateConfiguration->getPageTemplates($page);
46
47
        $pageTemplate = $pageTemplates[$this->getPageTemplate($page)];
48
49
        $template = $env->loadTemplate($pageTemplate->getTemplate());
50
51
        return $template->render(array_merge($parameters, $twigContext));
52
    }
53
54
    /**
55
     * @param HasPageTemplateInterface $page The page
56
     *
57
     * @return string
58
     */
59
    public function getPageTemplate(HasPageTemplateInterface $page)
60
    {
61
        return $this->templateConfiguration->findOrCreateFor($page)->getPageTemplate();
62
    }
63
64
    /**
65
     * @param \Twig_Environment        $env
66
     * @param array                    $twigContext
67
     * @param HasPageTemplateInterface $page
68
     * @param array                    $parameters
69
     *
70
     * @return string
71
     */
72 View Code Duplication
    public function renderPageTemplateConfiguration(\Twig_Environment $env, array $twigContext, HasPageTemplateInterface $page, array $parameters = array())
73
    {
74
        $pageTemplates = $this->templateConfiguration->getPageTemplates($page);
75
76
        $pageTemplate = $pageTemplates[$this->getPageTemplate($page)];
77
78
        $template = $env->loadTemplate($parameters['template']);
79
80
        return $template->render(array_merge(['pageTemplate' => $pageTemplate], $twigContext));
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function getName()
87
    {
88
        return 'pagetemplate_twig_extension';
89
    }
90
}
91