Skip to content

Commit 45f08b1

Browse files
committed
[HttpFoundation][Cache] Added MarshallingSessionHandler
1 parent cde44fc commit 45f08b1

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

src/Symfony/Component/Cache/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* added max-items + LRU + max-lifetime capabilities to `ArrayCache`
88
* added `CouchbaseBucketAdapter`
9+
* added `IdentityMarshaller`
910

1011
5.0.0
1112
-----
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
/**
15+
* @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com>
16+
*/
17+
class IdentityMarshaller implements MarshallerInterface
18+
{
19+
/**
20+
* {@inheritDoc}
21+
*/
22+
public function marshall(array $values, ?array &$failed): array
23+
{
24+
foreach ($values as $key => $value) {
25+
if (!\is_string($value)) {
26+
throw new \RuntimeException(sprintf('%s accepts only string as data.', __METHOD__));
27+
}
28+
}
29+
30+
return $values;
31+
}
32+
33+
/**
34+
* {@inheritDoc}
35+
*/
36+
public function unmarshall(string $value)
37+
{
38+
return $value;
39+
}
40+
}

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ CHANGELOG
2323
* made `Request::getSession()` throw if the session has not been set before
2424
* removed `Response::HTTP_RESERVED_FOR_WEBDAV_ADVANCED_COLLECTIONS_EXPIRED_PROPOSAL`
2525
* passing a null url when instantiating a `RedirectResponse` is not allowed
26+
* added `MarshallingSessionHandler`
2627

2728
4.4.0
2829
-----
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)