8000 moved management of the locale from the Session class to the Request … · sammanjac/symfony@74bc699 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 74bc699

Browse files
committed
moved management of the locale from the Session class to the Request class
The locale management does not require sessions anymore. In the Symfony2 spirit, the locale should be part of your URLs. If this is the case (via the special _locale request attribute), Symfony will store it in the request (getLocale()). This feature is now also configurable/replaceable at will as everything is now managed by the new LocaleListener event listener. How to upgrade: The default locale configuration has been moved from session to the main configuration: Before: framework: session: default_locale: en After: framework: default_locale: en Whenever you want to get the current locale, call getLocale() on the request (was on the session before).
1 parent 8b55541 commit 74bc699

File tree

26 files changed

+264
-152
lines changed

26 files changed

+264
-152
lines changed

CHANGELOG-2.1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
5959

6060
### HttpFoundation
6161

62+
* [BC BREAK] moved management of the locale from the Session class to the Request class
6263
* added a generic access to the PHP built-in filter mechanism: ParameterBag::filter()
6364
* made FileBinaryMimeTypeGuesser command configurable
6465
* added Request::getUser() and Request::getPassword()

UPGRADE-2.1.md

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,40 @@ UPGRADE FROM 2.0 to 2.1
33

44
* assets_base_urls and base_urls merging strategy has changed
55

6-
Unlike most configuration blocks, successive values for
7-
``assets_base_urls`` will overwrite each other instead of being merged.
8-
This behavior was chosen because developers will typically define base
9-
URL's for each environment. Given that most projects tend to inherit
10-
configurations (e.g. ``config_test.yml`` imports ``config_dev.yml``)
11-
and/or share a common base configuration (i.e. ``config.yml``), merging
12-
could yield a set of base URL's for multiple environments.
6+
Unlike most configuration blocks, successive values for
7+
``assets_base_urls`` will overwrite each other instead of being merged.
8+
This behavior was chosen because developers will typically define base
9+
URL's for each environment. Given that most projects tend to inherit
10+
configurations (e.g. ``config_test.yml`` imports ``config_dev.yml``)
11+
and/or share a common base configuration (i.e. ``config.yml``), merging
12+
could yield a set of base URL's for multiple environments.
1313

14+
* moved management of the locale from the Session class to the Request class
15+
16+
Configuring the default locale:
17+
18+
Before:
19+
20+
framework:
21+
session:
22+
default_locale: fr
23+
24+
After:
25+
26+
framework:
27+
default_locale: fr
28+
29+
Retrieving the locale from a Twig template:
30+
31+
Before: {{ app.request.session.locale }}
32+
After: {{ app.request.locale }}
33+
34+
Retrieving the locale from a PHP template:
35+
36+
Before: $view['session']->getLocale()
37+
After: $view['request']->getLocale()
38+
39+
Retrieving the locale from PHP code:
40+
41+
Before: $session->getLocale()
42+
After: $request->getLocale()

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function getConfigTreeBuilder()
5151
->scalarNode('secret')->isRequired()->end()
5252
->scalarNode('ide')->defaultNull()->end()
5353
->booleanNode('test')->end()
54+
->scalarNode('default_locale')->defaultValue('en')->end()
5455
->end()
5556
;
5657

@@ -161,7 +162,6 @@ private function addSessionSection(ArrayNodeDefinition $rootNode)
161162
->canBeUnset()
162163
->children()
163164
->booleanNode('auto_start')->defaultFalse()->end()
164-
->scalarNode('default_locale')->defaultValue('en')->end()
165165
->scalarNode('storage_id')->defaultValue('session.storage.native')->end()
166166
->scalarNode('name')->end()
167167
->scalarNode('lifetime')->end()

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public function load(array $configs, ContainerBuilder $container)
6565

6666
$container->setParameter('kernel.trust_proxy_headers', $config['trust_proxy_headers']);
6767

68+
$container->setParameter('kernel.default_locale', $config['default_locale']);
69+
6870
if (!empty($config['test'])) {
6971
$loader->load('test.xml');
7072
}
@@ -280,7 +282,6 @@ private function registerSessionConfiguration(array $config, ContainerBuilder $c
280282

281283
// session
282284
$container->getDefinition('session_listener')->addArgument($config['auto_start']);
283-
$container->setParameter('session.default_locale', $config['default_locale']);
284285

285286
// session storage
286287
$container->setAlias('session.storage', $config['storage_id']);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Bundle\FrameworkBundle\EventListener;
13+
14+
use Symfony\Component\HttpKernel\HttpKernelInterface;
15+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
16+
use Symfony\Component\Routing\RouterInterface;
17+
18+
/**
19+
* Initializes the locale based on the current request.
20+
*
21+
* @author Fabien Potencier <fabien@symfony.com>
22+
*/
23+
class LocaleListener
24+
{
25+
private $router;
26+
private $defaultLocale;
27+
28+
public function __construct($defaultLocale = 'en', RouterInterface $router = null)
29+
{
30+
$this->defaultLocale = $defaultLocale;
31+
$this->router = $router;
32+
}
33+
34+
public function onEarlyKernelRequest(GetResponseEvent $event)
35+
{
36+
$request = $event->getRequest();
37+
if ($request->hasPreviousSession()) {
38+
$request F438 ->setDefaultLocale($request->getSession()->get('_locale', $this->defaultLocale));
39+
} else {
40+
$request->setDefaultLocale($this->defaultLocale);
41+
}
42+
}
43+
44+
public function onKernelRequest(GetResponseEvent $event)
45+
{
46+
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
47+
return;
48+
}
49+
50+
$request = $event->getRequest();
51+
if ($locale = $request->attributes->get('_locale')) {
52+
$request->setLocale($locale);
53+
54+
if ($request->hasPreviousSession()) {
55+
$request->getSession()->set('_locale', $request->getLocale());
56+
}
57+
}
58+
59+
if (null !== $this->router) {
60+
$this->router->getContext()->setParameter('_locale', $request->getLocale());
61+
}
62+
}
63+
}

src/Symfony/Bundle/FrameworkBundle/EventListener/RouterListener.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,6 @@ public function onKernelRequest(GetResponseEvent $event)
8888

8989
throw new MethodNotAllowedHttpException($e->getAllowedMethods(), $message, $e);
9090
}
91-
92-
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
93-
$context = $this->router->getContext();
94-
$session = $request->getSession();
95-
if ($locale = $request->attributes->get('_locale')) {
96-
if ($session) {
97-
$session->setLocale($locale);
98-
}
99-
$context->setParameter('_locale', $locale);
100-
} elseif ($session) {
101-
$context->setParameter('_locale', $session->getLocale());
102-
}
103-
}
10491
}
10592

10693
private function parametersToString(array $parameters)

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<xsd:attribute name="trust-proxy-headers" type="xsd:string" />
2626
<xsd:attribute name="ide" type="xsd:string" />
2727
<xsd:attribute name="secret" type="xsd:string" />
28+
<xsd:attribute name="default-locale" type="xsd:string" />
2829
</xsd:complexType>
2930

3031
<xsd:complexType name="form">
@@ -72,7 +73,6 @@
7273

7374
<xsd:complexType name="session">
7475
<xsd:attribute name="storage-id" type="xsd:string" />
75-
<xsd:attribute name="default-locale" type="xsd:string" />
7676
<xsd:attribute name="name" type="xsd:string" />
7777
<xsd:attribute name="lifetime" type="xsd:integer" />
7878
<xsd:attribute name="path" type="xsd:string" />

src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
<services>
1515
<service id="session" class="%session.class%">
1616
<argument type="service" id="session.storage" />
17-
<argument>%session.default_locale%</argument>
1817
</service>
1918

2019
<service id="session.storage.native" class="%session.storage.native.class%" public="false">

src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
<argument key="debug">%kernel.debug%</argument>
3838
<argument key="charset">%kernel.charset%</argument>
3939
</argument>
40-
<argument type="service" id="session" on-invalid="ignore" />
4140
</service>
4241

4342
<service id="translator" class="%translator.identity.class%">

src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<parameter key="controller_resolver.class">Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver</parameter>
99
<parameter key="controller_name_converter.class">Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser</parameter>
1010
<parameter key="response_listener.class">Symfony\Component\HttpKernel\EventListener\ResponseListener</parameter>
11+
<parameter key="locale_listener.class">Symfony\Bundle\FrameworkBundle\EventListener\LocaleListener</parameter>
1112
</parameters>
1213

1314
<services>
@@ -27,5 +28,12 @@
2728
<tag name="kernel.event_listener" event="kernel.response" method="onKernelResponse" />
2829
<argument>%kernel.charset%</argument>
2930
</service>
31+
32+
<service id="locale_listener" class="%locale_listener.class%">
33+
<tag name="kernel.event_listener" event="kernel.request" method="onEarlyKernelRequest" priority="255" />
34+
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="-1" />
35+
<argument>%kernel.default_locale%</argument>
36+
<argument type="service" id="router" on-invalid="ignore" />
37+
</service>
3038
</services>
3139
</container>

src/Symfony/Bundle/FrameworkBundle/Templating/Helper/RequestHelper.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ public function getParameter($key, $default = null)
4646
return $this->request->get($key, $default);
4747
}
4848

49+
/**
50+
* Returns the locale
51+
*
52+
* @return string
53+
*/
54+
public function getLocale()
55+
{
56+
return $this->request->getLocale();
57+
}
58+
4959
/**
5060
* Returns the canonical name of this helper.
5161
*

src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,6 @@ public function get($name, $default = null)
4646
return $this->session->get($name, $default);
4747
}
4848

49-
/**
50-
* Returns the locale
51-
*
52-
* @return string
53-
*/
54-
public function getLocale()
55-
{
56-
return $this->session->getLocale();
57-
}
58-
5949
public function getFlash($name, $default = null)
6050
{
6151
return $this->session->getFlash($name, $default);

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
$container->loadFromExtension('framework', array(
44
'secret' => 's3cr3t',
5+
'default_locale' => 'fr',
56
'form' => null,
67
'csrf_protection' => array(
78
'enabled' => true,
@@ -19,7 +20,6 @@
1920
),
2021
'session' => array(
2122
'auto_start' => true,
22-
'default_locale' => 'fr',
2323
'storage_id' => 'session.storage.native',
2424
'name' => '_SYMFONY',
2525
'lifetime' => 86400,

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
77
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
88

9-
<framework:config secret="s3cr3t" ide="file%%link%%format">
9+
<framework:config secret="s3cr3t" ide="file%%link%%format" default-locale="fr">
1010
<framework:csrf-protection enabled="true" field-name="_csrf" />
1111
<framework:form />
1212
<framework:esi enabled="true" />
1313
<framework:profiler only-exceptions="true" />
1414
<framework:router resource="%kernel.root_dir%/config/routing.xml" type="xml" />
15-
<framework:session auto-start="true" default-locale="fr" storage-id="session.storage.native" name="_SYMFONY" lifetime="86400" path="/" domain="example.com" secure="true" httponly="true" />
15+
<framework:session auto-start="true" storage-id="session.storage.native" name="_SYMFONY" lifetime="86400" path="/" domain="example.com" secure="true" httponly="true" />
1616
<framework:templating assets-version="SomeVersionScheme" cache="/path/to/cache" >
1717
<framework:loader>loader.foo</framework:loader>
1818
<framework:loader>loader.bar</framework:loader>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
framework:
22
secret: s3cr3t
3+
default_locale: fr
34
form: ~
45
csrf_protection:
56
enabled: true
@@ -13,7 +14,6 @@ framework:
1314
type: xml
1415
session:
1516
auto_start: true
16-
default_locale: fr
1717
storage_id: session.storage.native
1818
name: _SYMFONY
1919
lifetime: 86400

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ public function testSession()
7676
$container = $this->createContainerFromFile('full');
7777

7878
$this->assertTrue($container->hasDefinition('session'), '->registerSessionConfiguration() loads session.xml');
79-
$this->assertEquals('fr', $container->getParameter('session.default_locale'));
80-
$this->assertEquals('%session.default_locale%', $container->getDefinition('session')->getArgument(1));
79+
$this->assertEquals('fr', $container->getParameter('kernel.default_locale'));
8180
$this->assertTrue($container->getDefinition('session_listener')->getArgument(1));
8281
$this->assertEquals('session.storage.native', (string) $container->getAlias('session.storage'));
8382

0 commit comments

Comments
 (0)
0