123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- namespace Aws\DynamoDb;
- class SessionHandler implements \SessionHandlerInterface
- {
-
- private $connection;
-
- private $savePath;
-
- private $sessionName;
-
- private $openSessionId = '';
-
- private $dataRead = '';
-
- private $sessionWritten = false;
-
- public static function fromClient(DynamoDbClient $client, array $config = [])
- {
- $config += ['locking' => false];
- if ($config['locking']) {
- $connection = new LockingSessionConnection($client, $config);
- } else {
- $connection = new StandardSessionConnection($client, $config);
- }
- return new static($connection);
- }
-
- public function __construct(SessionConnectionInterface $connection)
- {
- $this->connection = $connection;
- }
-
- public function register()
- {
- return session_set_save_handler($this, true);
- }
-
-
- public function open($savePath, $sessionName)
- {
- $this->savePath = $savePath;
- $this->sessionName = $sessionName;
- return true;
- }
-
-
- public function close()
- {
- $id = session_id();
-
-
- if ($this->openSessionId !== $id || !$this->sessionWritten) {
- $result = $this->connection->write($this->formatId($id), '', false);
- $this->sessionWritten = (bool) $result;
- }
- return $this->sessionWritten;
- }
-
-
- public function read($id)
- {
- $this->openSessionId = $id;
-
-
- $this->dataRead = '';
-
- $item = $this->connection->read($this->formatId($id));
- $dataAttribute = $this->connection->getDataAttribute();
- $sessionLifetimeAttribute = $this->connection->getSessionLifetimeAttribute();
-
- if (isset($item[$sessionLifetimeAttribute]) && isset($item[$dataAttribute])) {
- $this->dataRead = $item[$dataAttribute];
- if ($item[$sessionLifetimeAttribute] <= time()) {
- $this->dataRead = '';
- $this->destroy($id);
- }
- }
- return $this->dataRead;
- }
-
-
- public function write($id, $data)
- {
- $changed = $id !== $this->openSessionId
- || $data !== $this->dataRead;
- $this->openSessionId = $id;
-
- $this->sessionWritten = $this->connection
- ->write($this->formatId($id), $data, $changed);
- return $this->sessionWritten;
- }
-
-
- public function destroy($id)
- {
- $this->openSessionId = $id;
-
- $this->sessionWritten
- = $this->connection->delete($this->formatId($id));
- return $this->sessionWritten;
- }
-
-
- public function gc($maxLifetime)
- {
-
- return true;
- }
-
- public function garbageCollect()
- {
- $this->connection->deleteExpired();
- }
-
- private function formatId($id)
- {
- return trim($this->sessionName . '_' . $id, '_');
- }
- }
|