Skip to content

Commit bde9f04

Browse files
committed
Refactored the session handler
1 parent ae71ff3 commit bde9f04

File tree

5 files changed

+53
-38
lines changed

5 files changed

+53
-38
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* with this source code in the file LICENSE.
1010
*/
1111

12-
namespace Cache\CacheBundle\Session;
12+
namespace Cache\CacheBundle\Bridge;
1313

1414
use Cache\Taggable\TaggablePoolInterface;
1515
use Psr\Cache\CacheItemPoolInterface;
@@ -19,7 +19,7 @@
1919
*
2020
* @author Aaron Scherer <aequasi@gmail.com>
2121
*/
22-
class SessionHandler implements \SessionHandlerInterface
22+
class SessionHandlerBridge implements \SessionHandlerInterface
2323
{
2424
/**
2525
* @type CacheItemPoolInterface Cache driver.
@@ -103,7 +103,7 @@ public function write($sessionId, $data)
103103
public function destroy($sessionId)
104104
{
105105
if ($this->cache instanceof TaggablePoolInterface) {
106-
return $this->cache->deleteItem($this->prefix.$sessionId, ['session']);
106+
return $this->cache->deleteItem($this->prefix.$sessionId);
107107
}
108108

109109
return $this->cache->deleteItem($this->prefix.$sessionId);
@@ -126,7 +126,7 @@ public function gc($lifetime)
126126
private function getCacheItem($sessionId)
127127
{
128128
if ($this->cache instanceof TaggablePoolInterface) {
129-
return $this->cache->getItem($this->prefix.$sessionId, ['session']);
129+
return $this->cache->getItem($this->prefix.$sessionId);
130130
}
131131

132132
return $this->cache->getItem($this->prefix.$sessionId);

src/DependencyInjection/CacheExtension.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Cache\CacheBundle\DependencyInjection;
1313

1414
use Cache\Bridge\DoctrineCacheBridge;
15+
use Cache\CacheBundle\Bridge\SessionHandlerBridge;
1516
use Cache\CacheBundle\Bridge\SymfonyValidatorBridge;
1617
use Cache\CacheBundle\Factory\AnnotationFactory;
1718
use Cache\CacheBundle\Factory\SerializerFactory;
@@ -70,6 +71,13 @@ public function load(array $configs, ContainerBuilder $container)
7071
->addArgument($config['validation']);
7172
}
7273

74+
if ($config['session']['enabled']) {
75+
$container->register('cache.service.session', SymfonyValidatorBridge::class)
76+
->setFactory([SessionHandlerBridge::class, 'get'])
77+
->addArgument(new Reference($config['session']['service_id']))
78+
->addArgument($config['session']);
79+
}
80+
7381
if ($config['router']['enabled']) {
7482
$loader->load('router.yml');
7583
$container->getDefinition('cache.router')

src/DependencyInjection/Compiler/SessionSupportCompilerPass.php

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Cache\CacheBundle\DependencyInjection\Compiler;
1313

14-
use Cache\CacheBundle\Session\SessionHandler;
1514
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1615
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1716
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -25,11 +24,6 @@
2524
*/
2625
class SessionSupportCompilerPass implements CompilerPassInterface
2726
{
28-
/**
29-
* @type ContainerBuilder
30-
*/
31-
protected $container;
32-
3327
/**
3428
* @param ContainerBuilder $container
3529
*
@@ -47,30 +41,6 @@ public function process(ContainerBuilder $container)
4741
throw new \Exception('Session cache support cannot be enabled if there is no session.storage service');
4842
}
4943

50-
$this->enableSessionSupport($container, $container->getParameter('cache.session'));
51-
}
52-
53-
/**
54-
* Enables session support for memcached.
55-
*
56-
* @param array $config Configuration for bundle
57-
*
58-
* @throws InvalidConfigurationException
59-
*/
60-
private function enableSessionSupport(ContainerBuilder $container, array $config)
61-
{
62-
// calculate options
63-
$sessionOptions = $container->getParameter('session.storage.options');
64-
if (isset($sessionOptions['cookie_lifetime']) && !isset($config['cookie_lifetime'])) {
65-
$config['cookie_lifetime'] = $sessionOptions['cookie_lifetime'];
66-
}
67-
// load the session handler
68-
$definition = new Definition(SessionHandler::class);
69-
$definition->addArgument(new Reference($config['service_id']))
70-
->addArgument($config);
71-
72-
$container->setDefinition('cache.session_handler', $definition);
73-
74-
$container->setAlias('session.handler', 'cache.session_handler');
44+
$container->setAlias('session.handler', 'cache.servcice.session');
7545
}
7646
}

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ private function addSessionSupportSection()
6060
->addDefaultsIfNotSet()
6161
->children()
6262
->scalarNode('service_id')->isRequired()->end()
63-
->scalarNode('prefix')
64-
->defaultValue('session_')
65-
->end()
63+
->scalarNode('prefix')->defaultValue('session_')->end()
6664
->scalarNode('ttl')->end()
6765
->end();
6866

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\cache-bundle package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
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\CacheBundle\Factory;
13+
14+
use Cache\Bridge\DoctrineCacheBridge;
15+
use Cache\CacheBundle\Bridge\SessionHandlerBridge;
16+
use Cache\CacheBundle\Bridge\SymfonyValidatorBridge;
17+
use Cache\CacheBundle\Cache\FixedTaggingCachePool;
18+
use Psr\Cache\CacheItemPoolInterface;
19+
20+
/**
21+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
22+
*/
23+
class SessionHandlerFactory
24+
{
25+
/**
26+
* @param CacheItemPoolInterface $pool
27+
* @param array $config
28+
*
29+
* @return DoctrineCacheBridge
30+
*/
31+
public static function get(CacheItemPoolInterface $pool, $config)
32+
{
33+
if ($config['use_tagging']) {
34+
$pool = new FixedTaggingCachePool($pool, ['session']);
35+
}
36+
37+
return new SessionHandlerBridge($pool, $config);
38+
}
39+
}

0 commit comments

Comments
 (0)