10000 [HttpFoundation][Cache] Added MarshallingSessionHandler · symfony/symfony@46b8c7b · GitHub
[go: up one dir, main page]

Skip to content

Commit 46b8c7b

Browse files
committed
[HttpFoundation][Cache] Added MarshallingSessionHandler
1 parent cde44fc commit 46b8c7b

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed
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 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\Cache\Marshaller;
13+
14+
use Symfony\Component\Cache\Exception\LogicException;
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+
}

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: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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\IdentityMarshaller;
15+
use Symfony\Component\Cache\Marshaller\MarshallerInterface;
16+
17+
/**
18+
* @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com>
19+
*/
20+
class MarshallingSessionHandler implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
21+
{
22+
private $handler;
23+
private $marshaller;
24+
25+
public function __construct(AbstractSessionHandler $handler, MarshallerInterface $marshaller = null)
26+
{
27+
$this->handler = $handler;
28+
$this->marshaller = $marshaller ?? new IdentityMarshaller();
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function close()
35+
{
36+
return $this->handler->close();
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function destroy($sessionId)
43+
{
44+
return $this->handler->destroy($sessionId);
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
public function gc($maxlifetime)
51+
{
52+
return $this->handler->gc($maxlifetime);
53+
}
54+
55+
/**
56+
* {@inheritdoc}
57+
*/
58+
public function open($savePath, $name)
59+
{
60+
return $this->handler->open($savePath, $name);
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
public function read($sessionId)
67+
{
68+
return $this->marshaller->unmarshall($this->handler->read($sessionId));
69+
}
70+
71+
/**
72+
* {@inheritdoc}
73+
*/
74+
public function write($sessionId, $data)
75+
{
76+
$failed = [];
77+
$marshalledData = $this->marshaller->marshall(['data' => $data], $failed);
78+
79+
if (!isset($failed['data'])) {
80+
$data = $marshalledData['data'];
81+
}
82+
83+
return $this->handler->write($sessionId, $data);
84+
}
85+
86+
/**
87+
* {@inheritdoc}
88+
*/
89+
public function validateId($sessionId)
90+
{
91+
return $this->handler->validateId($sessionId);
92+
}
93+
94+
/**
95+
* {@inheritdoc}
96+
*/
97+
public function updateTimestamp($sessionId, $data)
98+
{
99+
return $this->handler->updateTimestamp($sessionId, $data);
100+
}
101+
}

0 commit comments

Comments
 (0)
0