Skip to content

Commit 2bd99f1

Browse files
committed
Merge pull request #36 from Nyholm/features
Refactored the session handler
2 parents ae71ff3 + e39ebaa commit 2bd99f1

File tree

5 files changed

+50
-48
lines changed

5 files changed

+50
-48
lines changed
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@
99
* with this source code in the file LICENSE.
1010
*/
1111

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

14-
use Cache\Taggable\TaggablePoolInterface;
1514
use Psr\Cache\CacheItemPoolInterface;
1615

1716
/**
1817
* Class SessionHandler.
1918
*
2019
* @author Aaron Scherer <aequasi@gmail.com>
2120
*/
22-
class SessionHandler implements \SessionHandlerInterface
21+
class SessionHandlerBridge implements \SessionHandlerInterface
2322
{
2423
/**
2524
* @type CacheItemPoolInterface Cache driver.
@@ -102,10 +101,6 @@ public function write($sessionId, $data)
102101
*/
103102
public function destroy($sessionId)
104103
{
105-
if ($this->cache instanceof TaggablePoolInterface) {
106-
return $this->cache->deleteItem($this->prefix.$sessionId, ['session']);
107-
}
108-
109104
return $this->cache->deleteItem($this->prefix.$sessionId);
110105
}
111106

@@ -125,10 +120,6 @@ public function gc($lifetime)
125120
*/
126121
private function getCacheItem($sessionId)
127122
{
128-
if ($this->cache instanceof TaggablePoolInterface) {
129-
return $this->cache->getItem($this->prefix.$sessionId, ['session']);
130-
}
131-
132123
return $this->cache->getItem($this->prefix.$sessionId);
133124
}
134125
}

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 & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,8 @@
1111

1212
namespace Cache\CacheBundle\DependencyInjection\Compiler;
1313

14-
use Cache\CacheBundle\Session\SessionHandler;
15-
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1614
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1715
use Symfony\Component\DependencyInjection\ContainerBuilder;
18-
use Symfony\Component\DependencyInjection\Definition;
19-
use Symfony\Component\DependencyInjection\Reference;
2016

2117
/**
2218
* Class SessionSupportCompilerPass.
@@ -25,11 +21,6 @@
2521
*/
2622
class SessionSupportCompilerPass implements CompilerPassInterface
2723
{
28-
/**
29-
* @type ContainerBuilder
30-
*/
31-
protected $container;
32-
3324
/**
3425
* @param ContainerBuilder $container
3526
*
@@ -47,30 +38,6 @@ public function process(ContainerBuilder $container)
4738
throw new \Exception('Session cache support cannot be enabled if there is no session.storage service');
4839
}
4940

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');
41+
$container->setAlias('session.handler', 'cache.service.session');
7542
}
7643
}

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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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\Cache\FixedTaggingCachePool;
17+
use Psr\Cache\CacheItemPoolInterface;
18+
19+
/**
20+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
21+
*/
22+
class SessionHandlerFactory
23+
{
24+
/**
25+
* @param CacheItemPoolInterface $pool
26+
* @param array $config
27+
*
28+
* @return DoctrineCacheBridge
29+
*/
30+
public static function get(CacheItemPoolInterface $pool, $config)
31+
{
32+
if ($config['use_tagging']) {
33+
$pool = new FixedTaggingCachePool($pool, ['session']);
34+
}
35+
36+
return new SessionHandlerBridge($pool, $config);
37+
}
38+
}

0 commit comments

Comments
 (0)