diff --git a/composer.json b/composer.json index 0c6e296..6c217b4 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,8 @@ "require": { "php": "^5.5|^7", "symfony/framework-bundle": "^2.7|^3.0", - "cache/taggable-cache": "^0.3" + "cache/taggable-cache": "^0.3", + "cache/session-handler": "^0.1" }, "require-dev": { "phpunit/phpunit": "^5.1|^4.0", diff --git a/src/Bridge/SessionHandlerBridge.php b/src/Bridge/SessionHandlerBridge.php deleted file mode 100644 index b230c7f..0000000 --- a/src/Bridge/SessionHandlerBridge.php +++ /dev/null @@ -1,125 +0,0 @@ -, Tobias Nyholm - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -namespace Cache\CacheBundle\Bridge; - -use Psr\Cache\CacheItemPoolInterface; - -/** - * Class SessionHandler. - * - * @author Aaron Scherer - */ -class SessionHandlerBridge implements \SessionHandlerInterface -{ - /** - * @type CacheItemPoolInterface Cache driver. - */ - private $cache; - - /** - * @type int Time to live in seconds - */ - private $ttl; - - /** - * @type string Key prefix for shared environments. - */ - private $prefix; - - /** - * Constructor. - * - * List of available options: - * * prefix: The prefix to use for the cache keys in order to avoid collision - * * expiretime: The time to live in seconds - * - * @param CacheItemPoolInterface $cache A Cache instance - * @param array $options An associative array of cache options - * - * @throws \InvalidArgumentException When unsupported options are passed - */ - public function __construct(CacheItemPoolInterface $cache, array $options = []) - { - $this->cache = $cache; - - $this->ttl = isset($options['ttl']) ? (int) $options['ttl'] : 86400; - $this->prefix = isset($options['prefix']) ? $options['prefix'] : 'sf2ses_'; - } - - /** - * {@inheritdoc} - */ - public function open($savePath, $sessionName) - { - return true; - } - - /** - * {@inheritdoc} - */ - public function close() - { - return true; - } - - /** - * {@inheritdoc} - */ - public function read($sessionId) - { - $item = $this->getCacheItem($sessionId); - if ($item->isHit()) { - return $item->get(); - } - - return ''; - } - - /** - * {@inheritdoc} - */ - public function write($sessionId, $data) - { - $item = $this->getCacheItem($sessionId); - $item->set($data) - ->expiresAfter($this->ttl); - - return $this->cache->save($item); - } - - /** - * {@inheritdoc} - */ - public function destroy($sessionId) - { - return $this->cache->deleteItem($this->prefix.$sessionId); - } - - /** - * {@inheritdoc} - */ - public function gc($lifetime) - { - // not required here because cache will auto expire the records anyhow. - return true; - } - - /** - * @param $sessionId - * - * @return \Psr\Cache\CacheItemInterface - */ - private function getCacheItem($sessionId) - { - return $this->cache->getItem($this->prefix.$sessionId); - } -} diff --git a/src/DependencyInjection/CacheExtension.php b/src/DependencyInjection/CacheExtension.php index f3ba21c..620ae07 100644 --- a/src/DependencyInjection/CacheExtension.php +++ b/src/DependencyInjection/CacheExtension.php @@ -12,12 +12,12 @@ namespace Cache\CacheBundle\DependencyInjection; use Cache\Bridge\DoctrineCacheBridge; -use Cache\CacheBundle\Bridge\SessionHandlerBridge; use Cache\CacheBundle\Bridge\SymfonyValidatorBridge; use Cache\CacheBundle\Factory\DoctrineBridgeFactory; use Cache\CacheBundle\Factory\SessionHandlerFactory; use Cache\CacheBundle\Factory\ValidationFactory; use Cache\CacheBundle\Routing\CachingRouter; +use Cache\SessionHandler\Psr6SessionHandler; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader; @@ -79,7 +79,7 @@ public function load(array $configs, ContainerBuilder $container) } if ($config['session']['enabled']) { - $container->register('cache.service.session', SessionHandlerBridge::class) + $container->register('cache.service.session', Psr6SessionHandler::class) ->setFactory([SessionHandlerFactory::class, 'get']) ->addArgument(new Reference($config['session']['service_id'])) ->addArgument($config['session']); diff --git a/src/Factory/SessionHandlerFactory.php b/src/Factory/SessionHandlerFactory.php index 8dfda8d..734b469 100644 --- a/src/Factory/SessionHandlerFactory.php +++ b/src/Factory/SessionHandlerFactory.php @@ -11,9 +11,8 @@ namespace Cache\CacheBundle\Factory; -use Cache\Bridge\DoctrineCacheBridge; -use Cache\CacheBundle\Bridge\SessionHandlerBridge; use Cache\CacheBundle\Cache\FixedTaggingCachePool; +use Cache\SessionHandler\Psr6SessionHandler; use Psr\Cache\CacheItemPoolInterface; /** @@ -25,7 +24,7 @@ class SessionHandlerFactory * @param CacheItemPoolInterface $pool * @param array $config * - * @return DoctrineCacheBridge + * @return Psr6SessionHandler */ public static function get(CacheItemPoolInterface $pool, $config) { @@ -33,6 +32,6 @@ public static function get(CacheItemPoolInterface $pool, $config) $pool = new FixedTaggingCachePool($pool, ['session']); } - return new SessionHandlerBridge($pool, $config); + return new Psr6SessionHandler($pool, $config); } } diff --git a/src/Factory/ValidationFactory.php b/src/Factory/ValidationFactory.php index 76c5bd8..7c7e218 100644 --- a/src/Factory/ValidationFactory.php +++ b/src/Factory/ValidationFactory.php @@ -11,7 +11,6 @@ namespace Cache\CacheBundle\Factory; -use Cache\Bridge\DoctrineCacheBridge; use Cache\CacheBundle\Bridge\SymfonyValidatorBridge; use Cache\CacheBundle\Cache\FixedTaggingCachePool; use Psr\Cache\CacheItemPoolInterface; @@ -25,7 +24,7 @@ class ValidationFactory * @param CacheItemPoolInterface $pool * @param array $config * - * @return DoctrineCacheBridge + * @return SymfonyValidatorBridge */ public static function get(CacheItemPoolInterface $pool, $config) { diff --git a/src/Routing/CachingRouter.php b/src/Routing/CachingRouter.php index c8dbd8e..0288bea 100644 --- a/src/Routing/CachingRouter.php +++ b/src/Routing/CachingRouter.php @@ -125,7 +125,7 @@ private function getCacheItemMatch($pathinfo) { /** @type RequestContext $c */ $c = $this->getContext(); - $key = sprintf('routing:%s:%s:%s:%s', $c->getMethod(), $c->getHost(), $pathinfo, $c->getQueryString()); + $key = sprintf('%s__%s__%s__%s', $c->getMethod(), $c->getHost(), $pathinfo, $c->getQueryString()); return $this->getCacheItemFromKey($key, 'match'); } @@ -142,7 +142,7 @@ private function getCacheItemMatch($pathinfo) private function getCacheItemGenerate($name, array $parameters, $referenceType) { sort($parameters); - $key = sprintf('generate:%s:%s:%s', $name, json_encode($parameters), $referenceType ? 'true' : 'false'); + $key = sprintf('%s.%s.%s', $name, $referenceType ? 'true' : 'false', json_encode($parameters)); return $this->getCacheItemFromKey($key, 'generate'); }