-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpFoundation] Added MarshallingSessionHandler #35804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fabpot
merged 1 commit into
symfony:master
from
atailouloute:http-foundation-marshalling-session-handler
Feb 25, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
...rameworkBundle/DependencyInjection/Compiler/RemoveUnusedSessionMarshallingHandlerPass.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com> | ||
*/ | ||
class RemoveUnusedSessionMarshallingHandlerPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasDefinition('session.marshalling_handler')) { | ||
return; | ||
} | ||
|
||
$isMarshallerDecorated = false; | ||
|
||
foreach ($container->getDefinitions() as $definition) { | ||
$decorated = $definition->getDecoratedService(); | ||
if (null !== $decorated && 'session.marshaller' === $decorated[0]) { | ||
$isMarshallerDecorated = true; | ||
|
||
break; | ||
} | ||
} | ||
|
||
if (!$isMarshallerDecorated) { | ||
$container->removeDefinition('session.marshalling_handler'); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/Symfony/Component/HttpFoundation/Session/Storage/Handler/IdentityMarshaller.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpFoundation\Session\Storage\Handler; | ||
|
||
use Symfony\Component\Cache\Marshaller\MarshallerInterface; | ||
|
||
/** | ||
* @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com> | ||
*/ | ||
class IdentityMarshaller implements MarshallerInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function marshall(array $values, ?array &$failed): array | ||
{ | ||
foreach ($values as $key => $value) { | ||
if (!\is_string($value)) { | ||
throw new \LogicException(sprintf('%s accepts only string as data.', __METHOD__)); | ||
} | ||
} | ||
|
||
return $values; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function unmarshall(string $value): string | ||
{ | ||
return $value; | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MarshallingSessionHandler.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpFoundation\Session\Storage\Handler; | ||
|
||
use Symfony\Component\Cache\Marshaller\MarshallerInterface; | ||
|
||
/** | ||
* @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com> | ||
*/ | ||
class MarshallingSessionHandler implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface | ||
{ | ||
private $handler; | ||
private $marshaller; | ||
|
||
public function __construct(AbstractSessionHandler $handler, MarshallerInterface $marshaller) | ||
{ | ||
$this->handler = $handler; | ||
$this->marshaller = $marshaller; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function open($savePath, $name) | ||
{ | ||
return $this->handler->open($savePath, $name); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function close() | ||
{ | ||
return $this->handler->close(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function destroy($sessionId) | ||
{ | ||
return $this->handler->destroy($sessionId); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function gc($maxlifetime) | ||
{ | ||
return $this->handler->gc($maxlifetime); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function read($sessionId) | ||
{ | ||
return $this->marshaller->unmarshall($this->handler->read($sessionId)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function write($sessionId, $data) | ||
{ | ||
$failed = []; | ||
$marshalledData = $this->marshaller->marshall(['data' => $data], $failed); | ||
|
||
if (isset($failed['data'])) { | ||
return false; | ||
} | ||
|
||
return $this->handler->write($sessionId, $marshalledData['data']); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function validateId($sessionId) | ||
{ | ||
return $this->handler->validateId($sessionId); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function updateTimestamp($sessionId, $data) | ||
{ | ||
return $this->handler->updateTimestamp($sessionId, $data); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/IdentityMarshallerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\IdentityMarshaller; | ||
|
||
/** | ||
* @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com> | ||
*/ | ||
class IdentityMarshallerTest extends Testcase | ||
{ | ||
public function testMarshall() | ||
{ | ||
$marshaller = new IdentityMarshaller(); | ||
$values = ['data' => 'string_data']; | ||
$failed = []; | ||
|
||
$this->assertSame($values, $marshaller->marshall($values, $failed)); | ||
} | ||
|
||
/** | ||
* @dataProvider invalidMarshallDataProvider | ||
*/ | ||
public function testMarshallInvalidData($values) | ||
{ | ||
$marshaller = new IdentityMarshaller(); | ||
$failed = []; | ||
|
||
$this->expectException(\LogicException::class); | ||
$this->expectExceptionMessage('Symfony\Component\HttpFoundation\Session\Storage\Handler\IdentityMarshaller::marshall accepts only string as data'); | ||
|
||
$marshaller->marshall($values, $failed); | ||
} | ||
|
||
public function testUnmarshall() | ||
{ | ||
$marshaller = new IdentityMarshaller(); | ||
|
||
$this->assertEquals('data', $marshaller->unmarshall('data')); | ||
} | ||
|
||
public function invalidMarshallDataProvider(): iterable | ||
{ | ||
return [ | ||
[['object' => new \stdClass()]], | ||
[['foo' => ['bar']]], | ||
]; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.