|
| 1 | +<?php |
| 2 | +namespace Ratchet\Session\Storage; |
| 3 | +use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; |
| 4 | +use Ratchet\Session\Storage\Proxy\VirtualProxy; |
| 5 | +use Ratchet\Session\Serialize\HandlerInterface; |
| 6 | + |
| 7 | +/** |
| 8 | + * [internal] VirtualSessionStorage for Symfony 6 on PHP 8+ using native types like `start(): bool` |
| 9 | + * |
| 10 | + * @internal used internally only, should not be referenced directly |
| 11 | + * @see VirtualSessionStorage |
| 12 | + */ |
| 13 | +class VirtualSessionStorageForSymfony6 extends NativeSessionStorage { |
| 14 | + /** |
| 15 | + * @var \Ratchet\Session\Serialize\HandlerInterface |
| 16 | + */ |
| 17 | + protected $_serializer; |
| 18 | + |
| 19 | + /** |
| 20 | + * @param \SessionHandlerInterface $handler |
| 21 | + * @param string $sessionId The ID of the session to retrieve |
| 22 | + * @param \Ratchet\Session\Serialize\HandlerInterface $serializer |
| 23 | + */ |
| 24 | + public function __construct(\SessionHandlerInterface $handler, $sessionId, HandlerInterface $serializer) { |
| 25 | + $this->setSaveHandler($handler); |
| 26 | + $this->saveHandler->setId($sessionId); |
| 27 | + $this->_serializer = $serializer; |
| 28 | + $this->setMetadataBag(null); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * {@inheritdoc} |
| 33 | + */ |
| 34 | + public function start(): bool { |
| 35 | + if ($this->started && !$this->closed) { |
| 36 | + return true; |
| 37 | + } |
| 38 | + |
| 39 | + // You have to call Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler::open() to use |
| 40 | + // pdo_sqlite (and possible pdo_*) as session storage, if you are using a DSN string instead of a \PDO object |
| 41 | + // in the constructor. The method arguments are filled with the values, which are also used by the symfony |
| 42 | + // framework in this case. This must not be the best choice, but it works. |
| 43 | + $this->saveHandler->open(session_save_path(), session_name()); |
| 44 | + |
| 45 | + $rawData = $this->saveHandler->read($this->saveHandler->getId()); |
| 46 | + $sessionData = $this->_serializer->unserialize($rawData); |
| 47 | + |
| 48 | + $this->loadSession($sessionData); |
| 49 | + |
| 50 | + if (!$this->saveHandler->isWrapper() && !$this->saveHandler->isSessionHandlerInterface()) { |
| 51 | + $this->saveHandler->setActive(false); |
| 52 | + } |
| 53 | + |
| 54 | + return true; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * {@inheritdoc} |
| 59 | + */ |
| 60 | + public function regenerate(bool $destroy = false, ?int $lifetime = null): bool { |
| 61 | + // .. ? |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * {@inheritdoc} |
| 67 | + */ |
| 68 | + public function save() { |
| 69 | + // get the data from the bags? |
| 70 | + // serialize the data |
| 71 | + // save the data using the saveHandler |
| 72 | +// $this->saveHandler->write($this->saveHandler->getId(), |
| 73 | + |
| 74 | + if (!$this->saveHandler->isWrapper() && !$this->getSaveHandler()->isSessionHandlerInterface()) { |
| 75 | + $this->saveHandler->setActive(false); |
| 76 | + } |
| 77 | + |
| 78 | + $this->closed = true; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * {@inheritdoc} |
| 83 | + */ |
| 84 | + public function setSaveHandler($saveHandler = null) { |
| 85 | + if (!($saveHandler instanceof \SessionHandlerInterface)) { |
| 86 | + throw new \InvalidArgumentException('Handler must be instance of SessionHandlerInterface'); |
| 87 | + } |
| 88 | + |
| 89 | + if (!($saveHandler instanceof VirtualProxy)) { |
| 90 | + $saveHandler = new VirtualProxy($saveHandler); |
| 91 | + } |
| 92 | + |
| 93 | + $this->saveHandler = $saveHandler; |
| 94 | + } |
| 95 | +} |
0 commit comments