Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit c5835e1

Browse files
committed
Merge pull request #278 from symfony-cmf/locale_parameter_issue
Set Content-Language header instead of language meta tag
2 parents 2d80990 + 094444d commit c5835e1

File tree

9 files changed

+145
-72
lines changed

9 files changed

+145
-72
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Changelog
44
1.2.0
55
-----
66

7+
* **2016-04-04**: Moved content-language from http-equiv to a real header.
78
* **2016-03-31**: [Form] Form type for seo metadata set by_reference to false by default when ORM is active.
89
* **2015-09-30**: Add `cmf_seo_update_metadata` twig function for updating seo metadata from templates using
910
* **2015-09-08**: Add meta tag for language information

DependencyInjection/CmfSeoExtension.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ public function load(array $configs, ContainerBuilder $container)
5959

6060
$sonataBundles = array();
6161
if ($this->isConfigEnabled($container, $config['persistence']['phpcr'])) {
62-
$container->setParameter($this->getAlias().'.backend_type_phpcr', true);
62+
$container->setParameter('cmf_seo.backend_type_phpcr', true);
6363
$container->setParameter(
64-
$this->getAlias().'.persistence.phpcr.manager_name',
64+
'cmf_seo.persistence.phpcr.manager_name',
6565
$config['persistence']['phpcr']['manager_name']
6666
);
6767
$sonataBundles[] = 'SonataDoctrinePHPCRAdminBundle';
@@ -70,15 +70,15 @@ public function load(array $configs, ContainerBuilder $container)
7070
}
7171

7272
if ($this->isConfigEnabled($container, $config['persistence']['orm'])) {
73-
$container->setParameter($this->getAlias().'.backend_type_orm', true);
73+
$container->setParameter('cmf_seo.backend_type_orm', true);
7474
$container->setParameter(
75-
$this->getAlias().'.persistence.orm.manager_name',
75+
'cmf_seo.persistence.orm.manager_name',
7676
$config['persistence']['orm']['manager_name']
7777
);
7878
$sonataBundles[] = 'SonataDoctrineORMBundle';
7979
}
8080

81-
$container->setParameter($this->getAlias().'.form_mode_orm',
81+
$container->setParameter('cmf_seo.form_mode_orm',
8282
$this->isConfigEnabled($container, $config['persistence']['orm'])
8383
&& !$this->isConfigEnabled($container, $config['persistence']['phpcr'])
8484
);
@@ -139,7 +139,7 @@ public function loadSonataAdmin($config, XmlFileLoader $loader, ContainerBuilder
139139
}
140140
}
141141

142-
$container->setParameter($this->getAlias().'.sonata_admin_extension.form_group', $config['form_group']);
142+
$container->setParameter('cmf_seo.sonata_admin_extension.form_group', $config['form_group']);
143143
$loader->load('admin.xml');
144144
}
145145

@@ -155,7 +155,7 @@ public function loadSeoParameters(array $config, ContainerBuilder $container)
155155

156156
foreach ($params as $param) {
157157
$value = isset($config[$param]) ? $config[$param] : null;
158-
$container->setParameter($this->getAlias().'.'.$param, $value);
158+
$container->setParameter('cmf_seo.'.$param, $value);
159159
}
160160
}
161161

@@ -196,7 +196,7 @@ private function loadPhpcr($config, XmlFileLoader $loader, ContainerBuilder $con
196196

197197
private function loadContentListener(array $config, XmlFileLoader $loader, ContainerBuilder $container)
198198
{
199-
$container->setParameter($this->getAlias().'.content_key', $config['content_key']);
199+
$container->setParameter('cmf_seo.content_key', $config['content_key']);
200200

201201
$loader->load('content-listener.xml');
202202

@@ -222,19 +222,19 @@ private function loadAlternateLocaleProvider($config, ContainerBuilder $containe
222222
throw new InvalidConfigurationException('Alternate locale provider enabled but none defined. You need to enable PHPCR or configure alternate_locale.provider_id');
223223
}
224224

225-
if ($container->has($this->getAlias().'.event_listener.seo_content')) {
225+
if ($container->has('cmf_seo.event_listener.seo_content')) {
226226
$container
227-
->findDefinition($this->getAlias().'.event_listener.seo_content')
227+
->findDefinition('cmf_seo.event_listener.seo_content')
228228
->addMethodCall(
229229
'setAlternateLocaleProvider',
230230
array(new Reference($alternateLocaleProvider))
231231
)
232232
;
233233
}
234234

235-
if ($container->has($this->getAlias().'.sitemap.guesser.alternate_locales')) {
235+
if ($container->has('cmf_seo.sitemap.guesser.alternate_locales')) {
236236
$container
237-
->findDefinition($this->getAlias().'.sitemap.guesser.alternate_locales')
237+
->findDefinition('cmf_seo.sitemap.guesser.alternate_locales')
238238
->replaceArgument(0, new Reference($alternateLocaleProvider))
239239
;
240240
}
@@ -260,9 +260,9 @@ private function loadErrorHandling($config, ContainerBuilder $container)
260260

261261
$templates = isset($config['templates']) ? $config['templates'] : array();
262262
$exclusionRules = isset($config['exclusion_rules']) ? $config['exclusion_rules'] : array();
263-
$container->setParameter($this->getAlias().'.error.templates', $templates);
263+
$container->setParameter('cmf_seo.error.templates', $templates);
264264

265-
$exclusionMatcherDefinition = $container->getDefinition($this->getAlias().'.error.exclusion_matcher');
265+
$exclusionMatcherDefinition = $container->getDefinition('cmf_seo.error.exclusion_matcher');
266266
foreach ($exclusionRules as $rule) {
267267
$rule['host'] = !isset($rule['host']) ? null : $rule['host'];
268268
$rule['methods'] = !isset($rule['methods']) ? null : $rule['methods'];
@@ -282,11 +282,11 @@ private function createRequestMatcher(ContainerBuilder $container, $path = null,
282282
{
283283
$arguments = array($path, $host, $methods, $ips, $attributes);
284284
$serialized = serialize($arguments);
285-
$id = $this->getAlias().'.error.request_matcher.'.md5($serialized).sha1($serialized);
285+
$id = 'cmf_seo.error.request_matcher.'.md5($serialized).sha1($serialized);
286286

287287
if (!$container->hasDefinition($id)) {
288288
$container
289-
->setDefinition($id, new DefinitionDecorator($this->getAlias().'.error.request_matcher'))
289+
->setDefinition($id, new DefinitionDecorator('cmf_seo.error.request_matcher'))
290290
->setArguments($arguments)
291291
;
292292
}
@@ -311,7 +311,7 @@ private function loadSitemapHandling($config, XmlFileLoader $loader, ContainerBu
311311
$helperStatus[$helper] = array();
312312
$serviceDefinitionIds = $container->findTaggedServiceIds($tag);
313313
foreach ($serviceDefinitionIds as $id => $attributes) {
314-
if (0 === strncmp($this->getAlias(), $id, strlen($this->getAlias()))) {
314+
if (0 === strpos($id, 'cmf_seo')) {
315315
// avoid interfering with services that are not part of this bundle
316316
$helperStatus[$helper][$id] = array();
317317
}
@@ -327,7 +327,7 @@ private function loadSitemapHandling($config, XmlFileLoader $loader, ContainerBu
327327
'sitemap' => $sitemapName,
328328
'priority' => -1,
329329
));
330-
$container->setDefinition($this->getAlias().'.sitemap.guesser.'.$sitemapName.'.default_change_frequency', $definition);
330+
$container->setDefinition('cmf_seo.sitemap.guesser.'.$sitemapName.'.default_change_frequency', $definition);
331331
}
332332
unset($configurations[$sitemapName]['default_change_frequency']);
333333

@@ -353,17 +353,17 @@ private function loadSitemapHandling($config, XmlFileLoader $loader, ContainerBu
353353
}
354354
}
355355

356-
$container->setParameter($this->getAlias().'.sitemap.configurations', $configurations);
356+
$container->setParameter('cmf_seo.sitemap.configurations', $configurations);
357357

358358
$container->setParameter(
359-
$this->getAlias().'.sitemap.default_change_frequency',
359+
'cmf_seo.sitemap.default_change_frequency',
360360
$config['defaults']['default_change_frequency']
361361
);
362362

363363
$this->handleSitemapHelper($helperStatus, $container);
364364

365365
if (!$alternateLocale) {
366-
$container->removeDefinition($this->getAlias().'.sitemap.guesser.alternate_locales');
366+
$container->removeDefinition('cmf_seo.sitemap.guesser.alternate_locales');
367367
}
368368
}
369369

@@ -409,6 +409,6 @@ private function loadFormConfiguration($config, ContainerBuilder $container, $st
409409
$seoMetadataClass = 'Symfony\Cmf\Bundle\SeoBundle\Doctrine\Phpcr\SeoMetadata';
410410
}
411411

412-
$container->setParameter($this->getAlias().'.form.data_class.seo_metadata', $seoMetadataClass);
412+
$container->setParameter('cmf_seo.form.data_class.seo_metadata', $seoMetadataClass);
413413
}
414414
}

Doctrine/Phpcr/SitemapDocumentProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function load($sitemap)
5050
// the chain provider does not like collections as we array_merge in there
5151
foreach ($documentsCollection as $document) {
5252
$documents[] = $document;
53-
};
53+
}
5454

5555
return $documents;
5656
}

EventListener/LanguageListener.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2015 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Bundle\SeoBundle\EventListener;
13+
14+
use Symfony\Component\HttpKernel\KernelEvents;
15+
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
16+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17+
18+
/**
19+
* This listener adds a Content-Language header to the response.
20+
*
21+
* @author Wouter de Jong <[email protected]>
22+
*/
23+
class LanguageListener implements EventSubscriberInterface
24+
{
25+
public static function getSubscribedEvents()
26+
{
27+
return array(
28+
KernelEvents::RESPONSE => 'onKernelResponse',
29+
);
30+
}
31+
32+
public function onKernelResponse(FilterResponseEvent $event)
33+
{
34+
if ($event->getResponse()->headers->has('Content-Language')) {
35+
return;
36+
}
37+
38+
$locale = $event->getRequest()->getLocale();
39+
$language = current(explode('_', $locale, 2));
40+
$event->getResponse()->headers->set('Content-Language', $language);
41+
}
42+
}

Resources/config/services.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
<argument type="service" id="translator" />
4444
<argument type="service" id="cmf_seo.config_values" />
4545
<argument type="service" id="cmf_seo.cache" />
46-
<argument>%locale%</argument>
4746
</service>
4847

4948
<service id="cmf_seo.error.suggestion_provider.controller" class="%cmf_seo.error.suggestion_provider.controller.class%">
@@ -65,6 +64,10 @@
6564
<tag name="twig.extension" />
6665
<argument type="service" id="cmf_seo.presentation" />
6766
</service>
67+
68+
<service id="cmf_seo.language_listener" class="Symfony\Cmf\Bundle\SeoBundle\EventListener\LanguageListener">
69+
<tag name="kernel.event_subscriber" />
70+
</service>
6871
</services>
6972

7073
</container>

SeoPresentation.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,6 @@ class SeoPresentation implements SeoPresentationInterface
8484
*/
8585
private $cache;
8686

87-
/**
88-
* @var
89-
*/
90-
private $locale;
91-
9287
/**
9388
* The constructor will set the injected SeoPage - the service of
9489
* sonata which is responsible for storing the seo data.
@@ -97,20 +92,17 @@ class SeoPresentation implements SeoPresentationInterface
9792
* @param TranslatorInterface $translator
9893
* @param ConfigValues $configValues
9994
* @param CacheInterface $cache
100-
* @param string $locale
10195
*/
10296
public function __construct(
10397
SeoPage $sonataPage,
10498
TranslatorInterface $translator,
10599
ConfigValues $configValues,
106-
CacheInterface $cache = null,
107-
$locale = null
100+
CacheInterface $cache = null
108101
) {
109102
$this->sonataPage = $sonataPage;
110103
$this->translator = $translator;
111104
$this->configValues = $configValues;
112105
$this->cache = $cache;
113-
$this->locale = $locale;
114106
}
115107

116108
/**
@@ -288,10 +280,6 @@ public function updateSeoPage($content)
288280
break;
289281
}
290282
}
291-
292-
if ($this->locale) {
293-
$this->sonataPage->addMeta('http-equiv', 'language', $this->locale);
294-
}
295283
}
296284

297285
/**
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2015 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Bundle\SeoBundle\Tests\Unit\EventListener;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Cmf\Bundle\SeoBundle\EventListener\LanguageListener;
17+
18+
class LanguageListenerTest extends \PHPUnit_Framework_Testcase
19+
{
20+
private $listener;
21+
22+
protected function setUp()
23+
{
24+
$this->listener = new LanguageListener();
25+
}
26+
27+
/**
28+
* @dataProvider provideRequestLocales
29+
*/
30+
public function testSetsContentLanguageHeader($locale)
31+
{
32+
$request = Request::create('/');
33+
$request->setLocale($locale);
34+
35+
$response = new Response();
36+
37+
$event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\FilterResponseEvent')->disableOriginalConstructor()->getMock();
38+
$event->expects($this->any())->method('getRequest')->will($this->returnValue($request));
39+
$event->expects($this->any())->method('getResponse')->will($this->returnValue($response));
40+
41+
$this->listener->onKernelResponse($event);
42+
43+
$this->assertEquals('en', $response->headers->get('Content-Language'));
44+
}
45+
46+
public function provideRequestLocales()
47+
{
48+
return array(
49+
array('en'),
50+
array('en_US'),
51+
);
52+
}
53+
54+
public function testDoesNotOverridePreSetContentLanguage()
55+
{
56+
$request = Request::create('/');
57+
$request->setLocale('en');
58+
59+
$response = new Response();
60+
$response->headers->set('Content-Language', 'nl');
61+
62+
$event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\FilterResponseEvent')->disableOriginalConstructor()->getMock();
63+
$event->expects($this->any())->method('getRequest')->will($this->returnValue($request));
64+
$event->expects($this->any())->method('getResponse')->will($this->returnValue($response));
65+
66+
$this->listener->onKernelResponse($event);
67+
68+
$this->assertEquals('nl', $response->headers->get('Content-Language'));
69+
}
70+
}

Tests/Unit/SeoPresentationTest.php

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,7 @@ public function testCaching()
347347
$this->pageService,
348348
$this->translator,
349349
$this->configValues,
350-
$cache,
351-
null
350+
$cache
352351
);
353352

354353
// predictions
@@ -389,8 +388,7 @@ public function testCacheRefresh()
389388
$this->pageService,
390389
$this->translator,
391390
$this->configValues,
392-
$cache,
393-
null
391+
$cache
394392
);
395393

396394
// predictions
@@ -421,25 +419,4 @@ public function testSeoAwareWithoutCurrentMetadata()
421419

422420
$this->seoPresentation->updateSeoPage($content);
423421
}
424-
425-
public function testLocaleMetaTag()
426-
{
427-
$seoPresentation = new SeoPresentation(
428-
$this->pageService,
429-
$this->translator,
430-
$this->configValues,
431-
null,
432-
'en'
433-
);
434-
435-
// predictions
436-
$this->pageService
437-
->expects($this->once())
438-
->method('addMeta')
439-
->with('http-equiv', 'language', 'en')
440-
;
441-
442-
// test
443-
$seoPresentation->updateSeoPage(new \stdClass());
444-
}
445422
}

0 commit comments

Comments
 (0)