1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Novaway\Bundle\OpenGraphBundle\Twig; |
4
|
|
|
|
5
|
|
|
use Novaway\Component\OpenGraph\OpenGraphInterface; |
6
|
|
|
use Novaway\Component\OpenGraph\OpenGraphTagInterface; |
7
|
|
|
use Novaway\Component\OpenGraph\View\OpenGraphRendererInterface; |
8
|
|
|
|
9
|
|
|
class OpenGraphExtension extends \Twig_Extension |
10
|
|
|
{ |
11
|
|
|
/** @var OpenGraphRendererInterface */ |
12
|
|
|
private $renderer; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Constructor |
17
|
|
|
* |
18
|
|
|
* @param OpenGraphRendererInterface $renderer |
19
|
|
|
*/ |
20
|
|
|
public function __construct(OpenGraphRendererInterface $renderer) |
21
|
|
|
{ |
22
|
|
|
$this->renderer = $renderer; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function getFunctions() |
29
|
|
|
{ |
30
|
|
|
return [ |
31
|
|
|
new \Twig_SimpleFunction('renderNamespace', [$this, 'renderNamespaceFunction'], ['is_safe' => ['html']]), |
32
|
|
|
new \Twig_SimpleFunction('renderGraph', [$this, 'renderGraphFunction'], ['is_safe' => ['html']]), |
33
|
|
|
new \Twig_SimpleFunction('renderTag', [$this, 'renderTagFunction'], ['is_safe' => ['html']]), |
34
|
|
|
]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Render namespaces |
39
|
|
|
* |
40
|
|
|
* @param OpenGraphInterface $graph |
41
|
|
|
* @param bool $withTag |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function renderNamespaceFunction(OpenGraphInterface $graph, $withTag = true) |
45
|
|
|
{ |
46
|
|
|
return $this->renderer->renderNamespaceAttributes($graph, $withTag); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Render graph |
51
|
|
|
* |
52
|
|
|
* @param OpenGraphInterface $graph |
53
|
|
|
* @param string $separator |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function renderGraphFunction(OpenGraphInterface $graph, $separator = PHP_EOL) |
57
|
|
|
{ |
58
|
|
|
return $this->renderer->render($graph, $separator); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Render graph tag |
63
|
|
|
* |
64
|
|
|
* @param OpenGraphTagInterface $tag |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function renderTagFunction(OpenGraphTagInterface $tag) |
68
|
|
|
{ |
69
|
|
|
return $this->renderer->renderTag($tag); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function getName() |
76
|
|
|
{ |
77
|
|
|
return 'novaway_open_graph_extension'; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|