1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\PagePartBundle\Twig\Extension; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\PagePartBundle\Helper\HasPageTemplateInterface; |
6
|
|
|
use Kunstmaan\PagePartBundle\PageTemplate\PageTemplateConfigurationService; |
7
|
|
|
use Twig\Environment; |
8
|
|
|
use Twig\Extension\AbstractExtension; |
9
|
|
|
use Twig\TwigFunction; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* PagePartTwigExtension |
13
|
|
|
* |
14
|
|
|
* @final since 5.4 |
15
|
|
|
*/ |
16
|
|
|
class PageTemplateTwigExtension extends AbstractExtension |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var PageTemplateConfigurationService |
20
|
|
|
*/ |
21
|
|
|
private $templateConfiguration; |
22
|
|
|
|
23
|
|
|
public function __construct(PageTemplateConfigurationService $templateConfiguration) |
24
|
|
|
{ |
25
|
|
|
$this->templateConfiguration = $templateConfiguration; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
public function getFunctions() |
32
|
|
|
{ |
33
|
|
|
return array( |
34
|
|
|
new TwigFunction('render_pagetemplate', [$this, 'renderPageTemplate'], ['needs_environment' => true, 'needs_context' => true, 'is_safe' => ['html']]), |
35
|
|
|
new TwigFunction('getpagetemplate', [$this, 'getPageTemplate']), |
36
|
|
|
new TwigFunction('render_pagetemplate_configuration', [$this, 'renderPageTemplateConfiguration'], ['needs_environment' => true, 'needs_context' => true, 'is_safe' => ['html']]), |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Environment $env |
42
|
|
|
* @param array $twigContext |
43
|
|
|
* @param HasPageTemplateInterface $page |
44
|
|
|
* @param array $parameters |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
public function renderPageTemplate(Environment $env, array $twigContext, HasPageTemplateInterface $page, array $parameters = array()) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$pageTemplates = $this->templateConfiguration->getPageTemplates($page); |
51
|
|
|
|
52
|
|
|
$pageTemplate = $pageTemplates[$this->getPageTemplate($page)]; |
53
|
|
|
|
54
|
|
|
$template = $env->load($pageTemplate->getTemplate()); |
55
|
|
|
|
56
|
|
|
return $template->render(array_merge($parameters, $twigContext)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param HasPageTemplateInterface $page The page |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function getPageTemplate(HasPageTemplateInterface $page) |
65
|
|
|
{ |
66
|
|
|
return $this->templateConfiguration->findOrCreateFor($page)->getPageTemplate(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param Environment $env |
71
|
|
|
* @param array $twigContext |
72
|
|
|
* @param HasPageTemplateInterface $page |
73
|
|
|
* @param array $parameters |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public function renderPageTemplateConfiguration(Environment $env, array $twigContext, HasPageTemplateInterface $page, array $parameters = array()) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$pageTemplates = $this->templateConfiguration->getPageTemplates($page); |
80
|
|
|
|
81
|
|
|
$pageTemplate = $pageTemplates[$this->getPageTemplate($page)]; |
82
|
|
|
|
83
|
|
|
$template = $env->load($parameters['template']); |
84
|
|
|
|
85
|
|
|
return $template->render(array_merge(['pageTemplate' => $pageTemplate], $twigContext)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getName() |
92
|
|
|
{ |
93
|
|
|
return 'pagetemplate_twig_extension'; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.