MarshallingSessionHandler.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. public function __construct(
  18. private AbstractSessionHandler $handler,
  19. private MarshallerInterface $marshaller,
  20. ) {
  21. }
  22. public function open(string $savePath, string $name): bool
  23. {
  24. return $this->handler->open($savePath, $name);
  25. }
  26. public function close(): bool
  27. {
  28. return $this->handler->close();
  29. }
  30. public function destroy(#[\SensitiveParameter] string $sessionId): bool
  31. {
  32. return $this->handler->destroy($sessionId);
  33. }
  34. public function gc(int $maxlifetime): int|false
  35. {
  36. return $this->handler->gc($maxlifetime);
  37. }
  38. public function read(#[\SensitiveParameter] string $sessionId): string
  39. {
  40. return $this->marshaller->unmarshall($this->handler->read($sessionId));
  41. }
  42. public function write(#[\SensitiveParameter] string $sessionId, string $data): bool
  43. {
  44. $failed = [];
  45. $marshalledData = $this->marshaller->marshall(['data' => $data], $failed);
  46. if (isset($failed['data'])) {
  47. return false;
  48. }
  49. return $this->handler->write($sessionId, $marshalledData['data']);
  50. }
  51. public function validateId(#[\SensitiveParameter] string $sessionId): bool
  52. {
  53. return $this->handler->validateId($sessionId);
  54. }
  55. public function updateTimestamp(#[\SensitiveParameter] string $sessionId, string $data): bool
  56. {
  57. return $this->handler->updateTimestamp($sessionId, $data);
  58. }
  59. }