MarshallingSessionHandler.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
  11. use Symfony\Component\Cache\Marshaller\MarshallerInterface;
  12. /**
  13. * @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com>
  14. */
  15. class MarshallingSessionHandler implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
  16. {
  17. private AbstractSessionHandler $handler;
  18. private MarshallerInterface $marshaller;
  19. public function __construct(AbstractSessionHandler $handler, MarshallerInterface $marshaller)
  20. {
  21. $this->handler = $handler;
  22. $this->marshaller = $marshaller;
  23. }
  24. public function open(string $savePath, string $name): bool
  25. {
  26. return $this->handler->open($savePath, $name);
  27. }
  28. public function close(): bool
  29. {
  30. return $this->handler->close();
  31. }
  32. public function destroy(#[\SensitiveParameter] string $sessionId): bool
  33. {
  34. return $this->handler->destroy($sessionId);
  35. }
  36. public function gc(int $maxlifetime): int|false
  37. {
  38. return $this->handler->gc($maxlifetime);
  39. }
  40. public function read(#[\SensitiveParameter] string $sessionId): string
  41. {
  42. return $this->marshaller->unmarshall($this->handler->read($sessionId));
  43. }
  44. public function write(#[\SensitiveParameter] string $sessionId, string $data): bool
  45. {
  46. $failed = [];
  47. $marshalledData = $this->marshaller->marshall(['data' => $data], $failed);
  48. if (isset($failed['data'])) {
  49. return false;
  50. }
  51. return $this->handler->write($sessionId, $marshalledData['data']);
  52. }
  53. public function validateId(#[\SensitiveParameter] string $sessionId): bool
  54. {
  55. return $this->handler->validateId($sessionId);
  56. }
  57. public function updateTimestamp(#[\SensitiveParameter] string $sessionId, string $data): bool
  58. {
  59. return $this->handler->updateTimestamp($sessionId, $data);
  60. }
  61. }