|
1
|
|
|
<?php |
|
2
|
|
|
namespace Axstrad\Bundle\ContentBundle\Twig; |
|
3
|
|
|
|
|
4
|
|
|
use Axstrad\Bundle\ContentBundle\Resolver\ContentResolver as Resolver; |
|
5
|
|
|
use Axstrad\Bundle\ContentBundle\Renderer\ContentRenderer as Renderer; |
|
6
|
|
|
use Twig_Extension; |
|
7
|
|
|
use Twig_SimpleFunction; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Axstrad\Bundle\ContentBundle\Twig\Extension |
|
11
|
|
|
*/ |
|
12
|
|
|
class Extension extends Twig_Extension |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Resolver |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $resolver; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Renderer |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $renderer; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param Resolver $resolver |
|
26
|
|
|
* @param Renderer $renderer |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(Resolver $resolver, Renderer $renderer) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->resolver = $resolver; |
|
31
|
|
|
$this->renderer = $renderer; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function getFunctions() |
|
35
|
|
|
{ |
|
36
|
|
|
return array( |
|
37
|
|
|
new Twig_SimpleFunction( |
|
38
|
|
|
'axstrad_content', |
|
39
|
|
|
array($this, 'axstradContent'), |
|
40
|
|
|
array( |
|
41
|
|
|
'needs_context' => true, |
|
42
|
|
|
'is_safe' => array('html'), |
|
43
|
|
|
) |
|
44
|
|
|
), |
|
45
|
|
|
|
|
46
|
|
|
new Twig_SimpleFunction( |
|
47
|
|
|
'axstrad_content_heading', |
|
48
|
|
|
array($this, 'axstradContentHeading'), |
|
49
|
|
|
array( |
|
50
|
|
|
'needs_context' => true, |
|
51
|
|
|
'is_safe' => array('html'), |
|
52
|
|
|
) |
|
53
|
|
|
), |
|
54
|
|
|
|
|
55
|
|
|
new Twig_SimpleFunction( |
|
56
|
|
|
'axstrad_content_copy', |
|
57
|
|
|
array($this, 'axstradContentCopy'), |
|
58
|
|
|
array( |
|
59
|
|
|
'needs_context' => true, |
|
60
|
|
|
'is_safe' => array('html'), |
|
61
|
|
|
) |
|
62
|
|
|
), |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function axstradContent($context, $content = null) |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->resolver |
|
69
|
|
|
->resolveFromContext($context, $content) |
|
70
|
|
|
->getContent() |
|
71
|
|
|
->map(function($content) { |
|
72
|
|
|
return $this->renderer->render($content); |
|
73
|
|
|
}) |
|
74
|
|
|
->getOrElse(null) |
|
75
|
|
|
; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function axstradContentHeading($context, $content = null) |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->resolver |
|
81
|
|
|
->resolveFromContext($context, $content) |
|
82
|
|
|
->getContent() |
|
83
|
|
|
->map(function($content) { |
|
84
|
|
|
return $this->renderer->renderHeading($content); |
|
85
|
|
|
}) |
|
86
|
|
|
->getOrElse(null) |
|
87
|
|
|
; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function axstradContentCopy($context, $content = null) |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->resolver |
|
93
|
|
|
->resolveFromContext($context, $content) |
|
94
|
|
|
->getContent() |
|
95
|
|
|
->map(function($content) { |
|
96
|
|
|
return $this->renderer->renderCopy($content); |
|
97
|
|
|
}) |
|
98
|
|
|
->getOrElse(null) |
|
99
|
|
|
; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function getName() |
|
103
|
|
|
{ |
|
104
|
|
|
return 'axstrad_content'; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|