forked from symfony-cmf/seo-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeoPresentation.php
349 lines (307 loc) · 10.6 KB
/
SeoPresentation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2015 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Cmf\Bundle\SeoBundle;
use Sonata\SeoBundle\Seo\SeoPage;
use Symfony\Cmf\Bundle\SeoBundle\Cache\CacheInterface;
use Symfony\Cmf\Bundle\SeoBundle\DependencyInjection\ConfigValues;
use Symfony\Cmf\Bundle\SeoBundle\Exception\InvalidArgumentException;
use Symfony\Cmf\Bundle\SeoBundle\Extractor\ExtractorInterface;
use Symfony\Cmf\Bundle\SeoBundle\Model\AlternateLocaleCollection;
use Symfony\Cmf\Bundle\SeoBundle\Model\SeoMetadata;
use Symfony\Cmf\Bundle\SeoBundle\Model\SeoMetadataInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Translation\TranslatorInterface;
/**
* This presentation model prepares the data for the SeoPage service of the
* SonataSeoBundle, which is able to provide the values to its Twig helpers.
*
* Preparing means that it creates the title/description by using the configured
* translation keys with the help of the Symfony Translator and the contents
* SeoMetadata value as parameter.
*
* The original_route_pattern will decide how to handle duplicate
* content. If it is set to canonical a canonical link is created by an Twig helper
* (url must be set to the SeoPage), otherwise the url is set to the redirectResponse property
* which triggers an redirectResponse.
*
* @author Maximilian Berghoff <Maximilian.Berghoff@gmx.de>
*/
class SeoPresentation implements SeoPresentationInterface
{
/**
* Original URL should be output as canonical URL.
*/
const ORIGINAL_URL_CANONICAL = 'canonical';
/**
* Redirect to original URL if not currently on original URL.
*/
const ORIGINAL_URL_REDIRECT = 'redirect';
public static $originalUrlBehaviours = array(
self::ORIGINAL_URL_CANONICAL,
self::ORIGINAL_URL_REDIRECT,
);
/**
* @var SeoPage
*/
private $sonataPage;
/**
* @var bool
*/
private $redirectResponse = false;
/**
* @var ExtractorInterface[]
*/
private $extractors = array();
/**
* @var TranslatorInterface
*/
private $translator;
/**
* @var ConfigValues
*/
private $configValues;
/**
* @var null|CacheInterface
*/
private $cache;
/**
* @var
*/
private $locale;
/**
* The constructor will set the injected SeoPage - the service of
* sonata which is responsible for storing the seo data.
*
* @param SeoPage $sonataPage
* @param TranslatorInterface $translator
* @param ConfigValues $configValues
* @param CacheInterface $cache
* @param string $locale
*/
public function __construct(
SeoPage $sonataPage,
TranslatorInterface $translator,
ConfigValues $configValues,
CacheInterface $cache = null,
$locale = null
) {
$this->sonataPage = $sonataPage;
$this->translator = $translator;
$this->configValues = $configValues;
$this->cache = $cache;
$this->locale = $locale;
}
/**
* @param RedirectResponse $redirect
*/
private function setRedirectResponse(RedirectResponse $redirect)
{
$this->redirectResponse = $redirect;
}
/**
* {@inheritdoc}
*/
public function getRedirectResponse()
{
return $this->redirectResponse;
}
/**
* Add an extractor for SEO metadata.
*
* @param ExtractorInterface $extractor
* @param int $priority
*/
public function addExtractor(ExtractorInterface $extractor, $priority = 0)
{
if (!isset($this->extractors[$priority])) {
$this->extractors[$priority] = array();
}
$this->extractors[$priority][] = $extractor;
}
/**
* Extract the SEO metadata from this object.
*
* @param object $content The content to extract metadata from.
*
* @return SeoMetadataInterface
*/
public function getSeoMetadata($content)
{
if ($content instanceof SeoAwareInterface) {
$contentSeoMetadata = $content->getSeoMetadata();
if ($contentSeoMetadata instanceof SeoMetadataInterface) {
$seoMetadata = $this->copyMetadata($contentSeoMetadata);
} elseif (null === $contentSeoMetadata) {
$seoMetadata = new SeoMetadata();
$content->setSeoMetadata($seoMetadata); // make sure it has metadata the next time
} else {
throw new InvalidArgumentException(
sprintf(
'getSeoMetadata must return either an instance of SeoMetadataInterface or null, "%s" given',
is_object($contentSeoMetadata) ? get_class($contentSeoMetadata) : gettype($contentSeoMetadata)
)
);
}
} else {
$seoMetadata = new SeoMetadata();
}
$cachingAvailable = (boolean) $this->cache;
if ($cachingAvailable) {
$extractors = $this->cache->loadExtractorsFromCache(get_class($content));
if (null === $extractors || !$extractors->isFresh()) {
$extractors = $this->getExtractorsForContent($content);
$this->cache->putExtractorsInCache(get_class($content), $extractors);
}
} else {
$extractors = $this->getExtractorsForContent($content);
}
foreach ($extractors as $extractor) {
$extractor->updateMetadata($content, $seoMetadata);
}
return $seoMetadata;
}
/**
* Returns the extractors for content.
*
* @param object $content
*
* @return ExtractorInterface[]
*/
private function getExtractorsForContent($content)
{
$extractors = array();
ksort($this->extractors);
foreach ($this->extractors as $priority) {
$supportedExtractors = array_filter($priority, function (ExtractorInterface $extractor) use ($content) {
return $extractor->supports($content);
});
$extractors = array_merge($extractors, $supportedExtractors);
}
return $extractors;
}
/**
* {@inheritdoc}
*/
public function updateSeoPage($content)
{
$seoMetadata = $this->getSeoMetadata($content);
$translationDomain = $this->configValues->getTranslationDomain();
if ($extraProperties = $seoMetadata->getExtraProperties()) {
foreach ($extraProperties as $key => $value) {
$this->sonataPage->addMeta('property', $key, $value);
}
}
if ($extraNames = $seoMetadata->getExtraNames()) {
foreach ($extraNames as $key => $value) {
$this->sonataPage->addMeta('name', $key, $value);
}
}
if ($extraHttp = $seoMetadata->getExtraHttp()) {
foreach ($extraHttp as $key => $value) {
$this->sonataPage->addMeta('http-equiv', $key, $value);
}
}
if ($seoMetadata->getTitle()) {
$pageTitle = null !== $this->configValues->getTitle()
? $this->translator->trans(
$this->configValues->getTitle(),
array('%content_title%' => $seoMetadata->getTitle()),
$translationDomain
)
: $seoMetadata->getTitle();
$this->sonataPage->setTitle($pageTitle);
}
if ($seoMetadata->getMetaDescription()) {
$pageDescription = null !== $this->configValues->getDescription()
? $this->translator->trans(
$this->configValues->getDescription(),
array('%content_description%' => $seoMetadata->getMetaDescription()),
$translationDomain
)
: $seoMetadata->getMetaDescription();
$this->sonataPage->addMeta(
'name',
'description',
$pageDescription
);
}
if ($seoMetadata->getMetaKeywords()) {
$this->sonataPage->addMeta(
'name',
'keywords',
$this->createKeywords($seoMetadata->getMetaKeywords())
);
}
$url = $seoMetadata->getOriginalUrl();
if ($url) {
switch ($this->configValues->getOriginalUrlBehaviour()) {
case self::ORIGINAL_URL_CANONICAL:
$this->sonataPage->setLinkCanonical($url);
break;
case self::ORIGINAL_URL_REDIRECT:
$this->setRedirectResponse(
new RedirectResponse($url)
);
break;
}
}
if ($this->locale) {
$this->sonataPage->addMeta('http-equiv', 'language', $this->locale);
}
}
/**
* Creates a concatenated list of keywords based on sonatas default
* values.
*
* @param string $contentKeywords
*
* @return string
*/
private function createKeywords($contentKeywords)
{
$metas = $this->sonataPage->getMetas();
$sonataKeywords = isset($metas['name']['keywords'][0])
? $metas['name']['keywords'][0]
: '';
return ('' !== $sonataKeywords ? $sonataKeywords.', ' : '').$contentKeywords;
}
/**
* Copy the metadata object to sanitize it and remove doctrine traces.
*
* @param SeoMetadataInterface $contentSeoMetadata
*
* @return SeoMetadata
*/
private function copyMetadata(SeoMetadataInterface $contentSeoMetadata)
{
$metadata = new SeoMetadata();
return $metadata
->setTitle($contentSeoMetadata->getTitle())
->setMetaKeywords($contentSeoMetadata->getMetaKeywords())
->setMetaDescription($contentSeoMetadata->getMetaDescription())
->setOriginalUrl($contentSeoMetadata->getOriginalUrl())
->setExtraProperties($contentSeoMetadata->getExtraProperties() ?: array())
->setExtraNames($contentSeoMetadata->getExtraNames() ?: array())
->setExtraHttp($contentSeoMetadata->getExtraHttp() ?: array())
;
}
/**
* {inheritDoc}.
*/
public function updateAlternateLocales(AlternateLocaleCollection $collection)
{
foreach ($collection as $alternateLocale) {
$this->sonataPage->addLangAlternate(
$alternateLocale->href,
$alternateLocale->hrefLocale
);
}
}
}