|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of php-cache\cache-bundle package. |
| 5 | + * |
| 6 | + * (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]> |
| 7 | + * |
| 8 | + * This source file is subject to the MIT license that is bundled |
| 9 | + * with this source code in the file LICENSE. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Cache\SessionHandler; |
| 13 | + |
| 14 | +use Psr\Cache\CacheItemPoolInterface; |
| 15 | + |
| 16 | +/** |
| 17 | + * Class SessionHandler. |
| 18 | + * |
| 19 | + * @author Aaron Scherer <[email protected]> |
| 20 | + */ |
| 21 | +class Psr6SessionHandler implements \SessionHandlerInterface |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @type CacheItemPoolInterface Cache driver. |
| 25 | + */ |
| 26 | + private $cache; |
| 27 | + |
| 28 | + /** |
| 29 | + * @type int Time to live in seconds |
| 30 | + */ |
| 31 | + private $ttl; |
| 32 | + |
| 33 | + /** |
| 34 | + * @type string Key prefix for shared environments. |
| 35 | + */ |
| 36 | + private $prefix; |
| 37 | + |
| 38 | + /** |
| 39 | + * Constructor. |
| 40 | + * |
| 41 | + * List of available options: |
| 42 | + * * prefix: The prefix to use for the cache keys in order to avoid collision |
| 43 | + * * expiretime: The time to live in seconds |
| 44 | + * |
| 45 | + * @param CacheItemPoolInterface $cache A Cache instance |
| 46 | + * @param array $options An associative array of cache options |
| 47 | + * |
| 48 | + * @throws \InvalidArgumentException When unsupported options are passed |
| 49 | + */ |
| 50 | + public function __construct(CacheItemPoolInterface $cache, array $options = []) |
| 51 | + { |
| 52 | + $this->cache = $cache; |
| 53 | + |
| 54 | + $this->ttl = isset($options['ttl']) ? (int) $options['ttl'] : 86400; |
| 55 | + $this->prefix = isset($options['prefix']) ? $options['prefix'] : 'psr6ses_'; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * {@inheritdoc} |
| 60 | + */ |
| 61 | + public function open($savePath, $sessionName) |
| 62 | + { |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * {@inheritdoc} |
| 68 | + */ |
| 69 | + public function close() |
| 70 | + { |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * {@inheritdoc} |
| 76 | + */ |
| 77 | + public function read($sessionId) |
| 78 | + { |
| 79 | + $item = $this->getCacheItem($sessionId); |
| 80 | + if ($item->isHit()) { |
| 81 | + return $item->get(); |
| 82 | + } |
| 83 | + |
| 84 | + return ''; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * {@inheritdoc} |
| 89 | + */ |
| 90 | + public function write($sessionId, $data) |
| 91 | + { |
| 92 | + $item = $this->getCacheItem($sessionId); |
| 93 | + $item->set($data) |
| 94 | + ->expiresAfter($this->ttl); |
| 95 | + |
| 96 | + return $this->cache->save($item); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * {@inheritdoc} |
| 101 | + */ |
| 102 | + public function destroy($sessionId) |
| 103 | + { |
| 104 | + return $this->cache->deleteItem($this->prefix.$sessionId); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * {@inheritdoc} |
| 109 | + */ |
| 110 | + public function gc($lifetime) |
| 111 | + { |
| 112 | + // not required here because cache will auto expire the records anyhow. |
| 113 | + return true; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @param $sessionId |
| 118 | + * |
| 119 | + * @return \Psr\Cache\CacheItemInterface |
| 120 | + */ |
| 121 | + private function getCacheItem($sessionId) |
| 122 | + { |
| 123 | + return $this->cache->getItem($this->prefix.$sessionId); |
| 124 | + } |
| 125 | +} |
0 commit comments