Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 47f787f

Browse files
committed
Disable guessing for a given query pattern
1 parent d0bf99e commit 47f787f

5 files changed

Lines changed: 60 additions & 1 deletion

File tree

DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public function getConfigTreeBuilder()
4242
->requiresAtLeastOneElement()
4343
->prototype('scalar')->end()
4444
->end()
45+
->scalarNode('guessing_excluded_pattern')
46+
->defaultNull()
47+
->end()
4548
->arrayNode('guessing_order')
4649
->isRequired()
4750
->requiresAtLeastOneElement()

EventListener/LocaleListener.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ class LocaleListener implements EventSubscriberInterface
6060
*/
6161
private $disableVaryHeader = false;
6262

63+
/**
64+
* @var string
65+
*/
66+
private $excludedPattern;
67+
6368
/**
6469
* Construct the guessermanager
6570
*
@@ -89,6 +94,10 @@ public function onKernelRequest(GetResponseEvent $event)
8994
{
9095
$request = $event->getRequest();
9196

97+
if ($this->excludedPattern && preg_match(sprintf('#%s#', $this->excludedPattern), $request->getPathInfo())) {
98+
return;
99+
}
100+
92101
$request->setDefaultLocale($this->defaultLocale);
93102

94103
$manager = $this->guesserManager;
@@ -143,6 +152,13 @@ public function setDisableVaryHeader ($disableVaryHeader) {
143152
$this->disableVaryHeader = $disableVaryHeader;
144153
}
145154

155+
/**
156+
* @param string $excludedPattern
157+
*/
158+
public function setExcludedPattern($excludedPattern) {
159+
$this->excludedPattern = $excludedPattern;
160+
}
161+
146162
/**
147163
* Log detection events
148164
*

Resources/config/services.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
<call method="setDisableVaryHeader">
5757
<argument>%lunetics_locale.disable_vary_header%</argument>
5858
</call>
59+
<call method="setExcludedPattern">
60+
<argument>%lunetics_locale.guessing_excluded_pattern%</argument>
61+
</call>
5962
<tag name="kernel.event_subscriber"/>
6063
</service>
6164

Resources/doc/index.markdown

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,14 @@ lunetics_locale:
115115
- browser
116116
```
117117

118-
### Locale Cookies / Session
118+
Note that you can disable the guessing for a given query pattern :
119+
120+
``` yaml
121+
lunetics_locale:
122+
guessing_excluded_pattern: ^/api
123+
```
124+
125+
### Locale Cookies / Session
119126

120127
The session and cookie guesser is usually used when you do not use locales in the uri's and you guess it from the user browser preferences. When doing this,
121128

Tests/EventListener/LocaleListenerTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,36 @@ public function testOnLocaleDetectedDisabledVaryHeader () {
234234
$listener->onLocaleDetectedSetVaryHeader($filterResponseEvent);
235235
}
236236

237+
public function excludedPatternDataProvider()
238+
{
239+
return array(
240+
array(null, true),
241+
array('.*', false),
242+
array('/api$', true),
243+
array('^/api', false),
244+
);
245+
}
246+
247+
/**
248+
* @dataProvider excludedPatternDataProvider
249+
*/
250+
public function testRunLocaleGuessingIsNotFiredIfPatternMatches ($pattern, $called)
251+
{
252+
$request = new Request(array(), array(), array(), array(), array(), array('REQUEST_URI' => '/api/users'));
253+
254+
$guesserManager = $this->getMockGuesserManager();
255+
$guesserManager
256+
->expects($this->exactly((int) $called))
257+
->method('runLocaleGuessing');
258+
259+
$listener = $this->getListener('en', $guesserManager);
260+
$listener->setExcludedPattern($pattern);
261+
262+
$event = $this->getEvent($request);
263+
264+
$listener->onKernelRequest($event);
265+
}
266+
237267
public function testLogEvent()
238268
{
239269
$message = 'Setting [ 1 ] as locale for the (Sub-)Request';

0 commit comments

Comments
 (0)