8000 [HttpFoundation][Cache] Added MarshallingSessionHandler · symfony/symfony@155d980 · GitHub
[go: up one dir, main page]

Skip to content

Commit 155d980

Browse files
committed
[HttpFoundation][Cache] Added MarshallingSessionHandler
1 parent cde44fc commit 155d980

File tree

9 files changed

+383
-0
lines changed

9 files changed

+383
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
17+
/**
18+
* @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com>
19+
*/
20+
class RemoveUnusedSessionMarshallingHandlerPass implements CompilerPassInterface
21+
{
22+
public function process(ContainerBuilder $container)
23+
{
24+
if (!$container->hasDefinition('session.marshalling_handler')) {
25+
return;
26+
}
27+
28+
$isMarshallerDecorated = false;
29+
30+
foreach ($container->getDefinitions() as $definition) {
31+
$decorated = $definition->getDecoratedService();
32+
if (null !== $decorated && 'session.marshaller' === $decorated[0]) {
33+
$isMarshallerDecorated = true;
34+
35+
break;
36+
}
37+
}
38+
39+
if (!$isMarshallerDecorated) {
40+
$container->removeDefinition('session.marshalling_handler');
41+
}
42+
}
43+
}

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\DataCollectorTranslatorPass;
1919
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggingTranslatorPass;
2020
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
21+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RemoveUnusedSessionMarshallingHandlerPass;
2122
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerRealRefPass;
2223
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerWeakRefPass;
2324
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\UnusedTagsPass;
@@ -130,6 +131,7 @@ public function build(ContainerBuilder $container)
130131
$this->addCompilerPassIfExists($container, AddAutoMappingConfigurationPass::class);
131132
$container->addCompilerPass(new RegisterReverseContainerPass(true));
132133
$container->addCompilerPass(new RegisterReverseContainerPass(false), PassConfig::TYPE_AFTER_REMOVING);
134+
$container->addCompilerPass(new RemoveUnusedSessionMarshallingHandlerPass());
133135

134136
if ($container->getParameter('kernel.debug')) {
135137
$container->addCompilerPass(new AddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 2);

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,12 @@
7171

7272
<!-- for BC -->
7373
<service id="session.storage.filesystem" alias="session.storage.mock_file" />
74+
75+
<service id="session.marshaller" class="Symfony\Component\HttpFoundation\Session\Storage\Handler\IdentityMarshaller" />
76+
77+
<service id="session.marshalling_handler" decorates="session.handler" class="Symfony\Component\HttpFoundation\Session\Storage\Handler\MarshallingSessionHandler">
78+
<argument type="service" id="session.marshalling_handler.inner" />
79+
<argument type="service" id="session.marshaller" />
80+
</service>
7481
</services>
7582
</container>

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CHANGELOG
1313
* added `Request::preferSafeContent()` and `Response::setContentSafe()` to handle "safe" HTTP preference
1414
according to [RFC 8674](https://tools.ietf.org/html/rfc8674)
1515
* made the Mime component an optional dependency
16+
* added `MarshallingSessionHandler`, `IdentityMarshaller`
1617

1718
5.0.0
1819
-----
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php 10000
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\Component\HttpFoundation\Session\Storage\Handler;
13+
14+
use Symfony\Component\Cache\Marshaller\MarshallerInterface;
15+
16+
/**
17+
* @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com>
18+
*/
19+
class IdentityMarshaller implements MarshallerInterface
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function marshall(array $values, ?array &$failed): array
25+
{
26+
foreach ($values as $key => $value) {
27+
if (!\is_string($value)) {
28+
throw new \LogicException(sprintf('%s accepts only string as data.', __METHOD__));
29+
}
30+
}
31+
32+
return $values;
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function unmarshall(string $value): string
39+
{
40+
return $value;
41+
}
42+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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\Component\HttpFoundation\Session\Storage\Handler;
13+
14+
use Symfony\Component\Cache\Marshaller\MarshallerInterface;
15+
16+
/**
17+
* @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com>
18+
*/
19+
class MarshallingSessionHandler implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
20+
{
21+
private $handler;
22+
private $marshaller;
23+
24+
public function __construct(AbstractSessionHandler $handler, MarshallerInterface $marshaller)
25+
{
26+
$this->handler = $handler;
27+
$this->marshaller = $marshaller;
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function open($savePath, $name)
34+
{
35+
return $this->handler->open($savePath, $name);
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function close()
42+
{
43+
return $this->handler->close();
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function destroy($sessionId)
50+
{
51+
return $this->handler->destroy($sessionId);
52+
}
53+
54+
/**
55+
* {@inheritdoc}
56+
*/
57+
public function gc($maxlifetime)
58+
{
59+
return $this->handler->gc($maxlifetime);
60+
}
61+
62+
/**
63+
* {@inheritdoc}
64+
*/
65+
public function read($sessionId)
66+
{
67+
return $this->marshaller->unmarshall($this->handler->read($sessionId));
68+
}
69+
70+
/**
71+
* {@inheritdoc}
72+
*/
73+
public function write($sessionId, $data)
74+
{
75+
$failed = [];
76+
$marshalledData = $this->marshaller->marshall(['data' => $data], $failed);
77+
78+
if (isset($failed['data'])) {
79+
return false;
80+
}
81+
82+
return $this->handler->write($sessionId, $marshalledData['data']);
83+
}
84+
85+
/**
86+
* {@inheritdoc}
87+
*/
88+
public function validateId($sessionId)
89+
{
90+
return $this->handler->validateId($sessionId);
91+
}
92+
93+
/**
94+
* {@inheritdoc}
95+
*/
96+
public function updateTimestamp($sessionId, $data)
97+
{
98+
return $this->handler->updateTimestamp($sessionId, $data);
99+
}
100+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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\Component\HttpFoundation\Tests\Session\Storage\Handler;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Session\Storage\Handler\IdentityMarshaller;
16+
17+
/**
18+
* @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com>
19+
*/
20+
class IdentityMarshallerTest extends Testcase
21+
{
22+
public function testMarshall()
23+
{
24+
$marshaller = new IdentityMarshaller();
25+
$values = ['data' => 'string_data'];
26+
$failed = [];
27+
28+
$this->assertSame($values, $marshaller->marshall($values, $failed));
29+
}
30+
31+
/**
32+
* @dataProvider invalidMarshallDataProvider
33+
*/
34+
public function testMarshallInvalidData($values)
35+
{
36+
$marshaller = new IdentityMarshaller();
37+
$failed = [];
38+
39+
$this->expectException(\LogicException::class);
40+
$this->expectExceptionMessage('Symfony\Component\HttpFoundation\Session\Storage\Handler\IdentityMarshaller::marshall accepts only string as data');
41+
42+
$marshaller->marshall($values, $failed);
43+
}
44+
45+
public function testUnmarshall()
46+
{
47+
$marshaller = new IdentityMarshaller();
48+
49+
$this->assertEquals('data', $marshaller->unmarshall('data'));
50+
}
51+
52+
public function invalidMarshallDataProvider(): iterable
53+
{
54+
return [
55+
[['object' => new \stdClass()]],
56+
[['foo' => ['bar']]],
57+
];
58+
}
59+
}

0 commit comments

Comments
 (0)
0