1 | <?php |
||
16 | class SeoExtension extends \Twig_Extension |
||
17 | { |
||
18 | /** |
||
19 | * @var SeoPageInterface |
||
20 | */ |
||
21 | protected $page; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $encoding; |
||
27 | |||
28 | /** |
||
29 | * @param SeoPageInterface $page |
||
30 | * @param string $encoding |
||
31 | */ |
||
32 | public function __construct(SeoPageInterface $page, $encoding) |
||
33 | { |
||
34 | $this->page = $page; |
||
35 | $this->encoding = $encoding; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * {@inheritdoc} |
||
40 | */ |
||
41 | public function getFunctions() |
||
42 | { |
||
43 | return array( |
||
44 | new \Twig_SimpleFunction('sonata_seo_title', array($this, 'getTitle'), array('is_safe' => array('html'))), |
||
45 | new \Twig_SimpleFunction('sonata_seo_metadatas', array($this, 'getMetadatas'), array('is_safe' => array('html'))), |
||
46 | new \Twig_SimpleFunction('sonata_seo_html_attributes', array($this, 'getHtmlAttributes'), array('is_safe' => array('html'))), |
||
47 | new \Twig_SimpleFunction('sonata_seo_head_attributes', array($this, 'getHeadAttributes'), array('is_safe' => array('html'))), |
||
48 | new \Twig_SimpleFunction('sonata_seo_link_canonical', array($this, 'getLinkCanonical'), array('is_safe' => array('html'))), |
||
49 | new \Twig_SimpleFunction('sonata_seo_lang_alternates', array($this, 'getLangAlternates'), array('is_safe' => array('html'))), |
||
50 | new \Twig_SimpleFunction('sonata_seo_oembed_links', array($this, 'getOembedLinks'), array('is_safe' => array('html'))), |
||
51 | ); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | public function getName() |
||
58 | { |
||
59 | return 'sonata_seo'; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @deprecated Deprecated as of 1.2, echo the return value of getTitle() instead. |
||
64 | */ |
||
65 | public function renderTitle() |
||
69 | |||
70 | /** |
||
71 | * @return string |
||
72 | */ |
||
73 | public function getTitle() |
||
77 | |||
78 | /** |
||
79 | * @deprecated Deprecated as of 1.2, echo the return value of getMetadatas() instead. |
||
80 | */ |
||
81 | public function renderMetadatas() |
||
85 | |||
86 | /** |
||
87 | * @return string |
||
88 | */ |
||
89 | public function getMetadatas() |
||
90 | { |
||
91 | $html = ''; |
||
92 | foreach ($this->page->getMetas() as $type => $metas) { |
||
93 | foreach ((array) $metas as $name => $meta) { |
||
94 | list($content, $extras) = $meta; |
||
|
|||
95 | |||
96 | if (!empty($content)) { |
||
97 | $html .= sprintf("<meta %s=\"%s\" content=\"%s\" />\n", |
||
98 | $type, |
||
99 | $this->normalize($name), |
||
100 | $this->normalize($content) |
||
101 | ); |
||
102 | } else { |
||
103 | $html .= sprintf("<meta %s=\"%s\" />\n", |
||
104 | $type, |
||
105 | $this->normalize($name) |
||
106 | ); |
||
107 | } |
||
108 | } |
||
109 | } |
||
110 | |||
111 | return $html; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @deprecated Deprecated as of 1.2, echo the return value of getHtmlAttributes() instead. |
||
116 | */ |
||
117 | public function renderHtmlAttributes() |
||
121 | |||
122 | /** |
||
123 | * @return string |
||
124 | */ |
||
125 | public function getHtmlAttributes() |
||
126 | { |
||
127 | $attributes = ''; |
||
128 | foreach ($this->page->getHtmlAttributes() as $name => $value) { |
||
129 | $attributes .= sprintf('%s="%s" ', $name, $value); |
||
130 | } |
||
131 | |||
132 | return rtrim($attributes); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @deprecated Deprecated as of 1.2, echo the return value of getHeadAttributes() instead. |
||
137 | */ |
||
138 | public function renderHeadAttributes() |
||
142 | |||
143 | /** |
||
144 | * @return string |
||
145 | */ |
||
146 | public function getHeadAttributes() |
||
147 | { |
||
148 | $attributes = ''; |
||
149 | foreach ($this->page->getHeadAttributes() as $name => $value) { |
||
150 | $attributes .= sprintf('%s="%s" ', $name, $value); |
||
151 | } |
||
152 | |||
153 | return rtrim($attributes); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @deprecated Deprecated as of 1.2, echo the return value of getLinkCanonical() instead. |
||
158 | */ |
||
159 | public function renderLinkCanonical() |
||
160 | { |
||
161 | echo $this->getLinkCanonical(); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @return string |
||
166 | */ |
||
167 | public function getLinkCanonical() |
||
168 | { |
||
169 | if ($this->page->getLinkCanonical()) { |
||
170 | return sprintf("<link rel=\"canonical\" href=\"%s\"/>\n", $this->page->getLinkCanonical()); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @deprecated Deprecated as of 1.2, echo the return value of getLangAlternates() instead. |
||
176 | */ |
||
177 | public function renderLangAlternates() |
||
181 | |||
182 | /** |
||
183 | * @return string |
||
184 | */ |
||
185 | public function getLangAlternates() |
||
186 | { |
||
194 | |||
195 | /** |
||
196 | * @return string |
||
197 | */ |
||
198 | public function getOembedLinks() |
||
207 | |||
208 | /** |
||
209 | * @param string $string |
||
210 | * |
||
211 | * @return mixed |
||
212 | */ |
||
213 | private function normalize($string) |
||
217 | } |
||
218 |
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.