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

Skip to content

Commit b9a3c87

Browse files
bug #27714 [HttpFoundation] fix session tracking counter (nicolas-grekas, dmaicher)
This PR was merged into the 3.4 branch. Discussion ---------- [HttpFoundation] fix session tracking counter | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | no | Fixed tickets | - | License | MIT | Doc PR | - As just discussed with @nicolas-grekas I found this issue today while upgrading my app to 3.4.12. Somehow its not possible anymore to set caching headers correctly since this commit: 146e01c#diff-5350dc763df30ada9d00563c115f6652 Commits ------- 89ed756 failing test to reproduce session problem 26fc4e6 [HttpFoundation] fix session tracking counter
2 parents ad066bb + 89ed756 commit b9a3c87

File tree

5 files changed

+34
-9
lines changed

5 files changed

+34
-9
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ public function welcomeAction(Request $request, $name = null)
4343
return new Response(sprintf('Welcome back %s, nice to meet you.', $name));
4444
}
4545

46+
public function cacheableAction()
47+
{
48+
$response = new Response('all good');
49+
$response->setSharedMaxAge(100);
50+
51+
return $response;
52+
}
53+
4654
public function logoutAction(Request $request)
4755
{
4856
$request->getSession()->invalidate();

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ session_welcome:
22
path: /session
33
defaults: { _controller: TestBundle:Session:welcome }
44

5+
session_cacheable:
6+
path: /cacheable
7+
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::cacheableAction }
8+
59
session_welcome_name:
610
path: /session/{name}
711
defaults: { _controller: TestBundle:Session:welcome }

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/SessionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,22 @@ public function testTwoClients($config, $insulate)
126126
$this->assertContains('Welcome back client2, nice to meet you.', $crawler2->text());
127127
}
128128

129+
/**
130+
* @dataProvider getConfigs
131+
*/
132+
public function testCorrectCacheControlHeadersForCacheableAction($config, $insulate)
133+
{
134+
$client = $this->createClient(array('test_case' => 'Session', 'root_config' => $config));
135+
if ($insulate) {
136+
$client->insulate();
137+
}
138+
139+
$client->request('GET', '/cacheable');
140+
141+
$response = $client->getResponse();
142+
$this->assertSame('public, s-maxage=100', $response->headers->get('cache-control'));
143+
}
144+
129145
public function getConfigs()
130146
{
131147
return array(

src/Symfony/Component/HttpFoundation/Session/Session.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ public function __construct(SessionStorageInterface $storage = null, AttributeBa
5454
*/
5555
public function start()
5656
{
57-
++$this->usageIndex;
58-
5957
return $this->storage->start();
6058
}
6159

@@ -160,7 +158,9 @@ public function getUsageIndex()
160158
*/
161159
public function isEmpty()
162160
{
163-
++$this->usageIndex;
161+
if ($this->isStarted()) {
162+
++$this->usageIndex;
163+
}
164164
foreach ($this->data as &$data) {
165165
if (!empty($data)) {
166166
return false;
@@ -185,8 +185,6 @@ public function invalidate($lifetime = null)
185185
*/
186186
public function migrate($destroy = false, $lifetime = null)
187187
{
188-
++$this->usageIndex;
189-
190188
return $this->storage->regenerate($destroy, $lifetime);
191189
}
192190

@@ -195,8 +193,6 @@ public function migrate($destroy = false, $lifetime = null)
195193
*/
196194
public function save()
197195
{
198-
++$this->usageIndex;
199-
200196
$this->storage->save();
201197
}
202198

src/Symfony/Component/HttpFoundation/Session/SessionBagProxy.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public function getBag()
4444
*/
4545
public function isEmpty()
4646
{
47+
if (!isset($this->data[$this->bag->getStorageKey()])) {
48+
return true;
49+
}
4750
++$this->usageIndex;
4851

4952
return empty($this->data[$this->bag->getStorageKey()]);
@@ -81,8 +84,6 @@ public function getStorageKey()
8184
*/
8285
public function clear()
8386
{
84-
++$this->usageIndex;
85-
8687
return $this->bag->clear();
8788
}
8889
}

0 commit comments

Comments
 (0)