Skip to content

Commit 89aae32

Browse files
committed
Bugfixes and improvements
1 parent de1d479 commit 89aae32

File tree

11 files changed

+49
-56
lines changed

11 files changed

+49
-56
lines changed

LICENSE

100644100755
File mode changed.

src/Cache/LoggingCachePool.php

100644100755
File mode changed.

src/DataCollector/CacheDataCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class CacheDataCollector extends DataCollector
2929
*
3030
* @type string
3131
*/
32-
const TEMPLATE = 'CacheCacheBundle:Collector:cache.html.twig';
32+
const TEMPLATE = 'CacheBundle:Collector:cache.html.twig';
3333

3434
/**
3535
* @var LoggingCachePool[]

src/DependencyInjection/CacheExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public function load(array $configs, ContainerBuilder $container)
4040
}
4141

4242
foreach (['router', 'session', 'doctrine'] as $section) {
43-
if ($container[$section]['enabled']) {
44-
$container->setParameter($this->getAlias().'.'.$section, $config[$section]);
43+
if ($config[$section]['enabled']) {
44+
$container->setParameter('cache.'.$section, $config[$section]);
4545
}
4646
}
4747

src/DependencyInjection/Compiler/DataCollectorCompilerPass.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111

1212
namespace Cache\CacheBundle\DependencyInjection\Compiler;
1313

14+
use Cache\CacheBundle\Cache\LoggingCachePool;
1415
use Symfony\Component\DependencyInjection\Reference;
1516

1617
/**
1718
* Class DataCollectorCompilerPass
1819
*
1920
* @author Aaron Scherer <[email protected]>
21+
* @author Tobias Nyholm <[email protected]>
2022
*/
2123
class DataCollectorCompilerPass extends BaseCompilerPass
2224
{
@@ -27,18 +29,19 @@ protected function prepare()
2729
{
2830
$this->transformLoggableCachePools();
2931

30-
$instances = $this->container->getParameter($this->getAlias() . '.instance');
32+
$collectorDefinition = $this->container->getDefinition('data_collector.cache');
3133

32-
$definition = $this->container->getDefinition('data_collector.cache');
33-
34-
foreach (array_keys($instances) as $name) {
35-
$instance = new Reference(sprintf("%s.instance.%s", $this->getAlias(), $name));
36-
$definition->addMethodCall('addInstance', array($name, $instance));
34+
$serviceIds = $this->container->findTaggedServiceIds('cache.provider');
35+
foreach (array_keys($serviceIds) as $id) {
36+
$collectorDefinition->addMethodCall('addInstance', [$id, new Reference($id)]);
3737
}
3838

39-
$this->container->setDefinition('data_collector.cache', $definition);
39+
$this->container->setDefinition('data_collector.cache', $collectorDefinition);
4040
}
4141

42+
/**
43+
* Make all cache providers loggable.
44+
*/
4245
private function transformLoggableCachePools()
4346
{
4447
$serviceIds = $this->container->findTaggedServiceIds('cache.provider');

src/DependencyInjection/Compiler/DoctrineSupportCompilerPass.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111

1212
namespace Cache\CacheBundle\DependencyInjection\Compiler;
1313

14+
use Cache\Bridge\DoctrineCacheBridge;
1415
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
16+
use Symfony\Component\DependencyInjection\Definition;
17+
use Symfony\Component\DependencyInjection\Reference;
1518

1619
/**
1720
* Class DoctrineSupportCompilerPass
1821
*
1922
* @author Aaron Scherer <[email protected]>
23+
* @author Tobias Nyholm <[email protected]>
2024
*/
2125
class DoctrineSupportCompilerPass extends BaseCompilerPass
2226
{
@@ -27,7 +31,7 @@ class DoctrineSupportCompilerPass extends BaseCompilerPass
2731
protected function prepare()
2832
{
2933
// If disabled, continue
30-
if (!$this->container->hasParameter($this->getAlias().'.doctrine')) {
34+
if (!$this->container->hasParameter('cache.doctrine')) {
3135
return;
3236
}
3337

@@ -37,7 +41,7 @@ protected function prepare()
3741
);
3842
}
3943

40-
$this->enableDoctrineSupport($this->container->getParameter($this->getAlias().'.doctrine'));
44+
$this->enableDoctrineSupport($this->container->getParameter('cache.doctrine'));
4145
}
4246

4347
/**
@@ -56,15 +60,10 @@ protected function enableDoctrineSupport(array $config)
5660
continue;
5761
}
5862

59-
if (!isset($cacheData['instance'])) {
60-
throw new InvalidConfigurationException(
61-
sprintf(
62-
"There was no instance passed. Please specify a instance under the %s type",
63-
$cacheType
64-
)
65-
);
66-
}
67-
$cacheDefinitionName = sprintf('%s.instance.%s.bridge', $this->getAlias(), $cacheData['instance']);
63+
$bridgeServiceId = sprintf('cache.provider.doctrine.%s.bridge', $cacheType);
64+
$bridgeDef = $this->container->register($bridgeServiceId, DoctrineCacheBridge::class);
65+
$bridgeDef->addArgument(0, new Reference($cacheData['service_id']))
66+
->setPublic(false);
6867

6968
foreach ($cacheData[$type] as $manager) {
7069
$doctrineDefinitionName =
@@ -76,7 +75,7 @@ protected function enableDoctrineSupport(array $config)
7675
);
7776

7877
// Replace the doctrine entity manager cache with our bridge
79-
$this->container->setAlias($doctrineDefinitionName, $cacheDefinitionName);
78+
$this->container->setAlias($doctrineDefinitionName, $bridgeServiceId);
8079
}
8180
}
8281
}

src/DependencyInjection/Compiler/RouterCompilerPass.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ protected function prepare()
3232

3333
$def = clone $this->container->findDefinition('router');
3434
$def->setClass('Cache\CacheBundle\Routing\Router');
35-
$def->addMethodCall('setCache', [new Reference(sprintf('cache.instance.%s', $router['instance']))]);
35+
$def->addMethodCall('setCache', [new Reference($router['service_id'])]);
3636
$def->addMethodCall('setTtl', [$router['ttl']]);
3737

3838
$this->container->setDefinition('cache.router', $def);
3939
$this->container->setAlias('router.alias', 'cache.router');
4040

4141
if ($router['auto-register']) {
42-
$this->container->setAlias('router', 'router.cache');
42+
$this->container->setAlias('router', 'cache.router');
4343
}
4444
}
4545
}

src/DependencyInjection/Compiler/SessionSupportCompilerPass.php

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Cache\CacheBundle\DependencyInjection\Compiler;
1313

14+
use Cache\CacheBundle\Session\SessionHandler;
1415
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1516
use Symfony\Component\DependencyInjection\Definition;
1617
use Symfony\Component\DependencyInjection\Reference;
@@ -49,35 +50,17 @@ protected function prepare()
4950
*/
5051
private function enableSessionSupport(array $config)
5152
{
52-
if (empty($config['instance'])) {
53-
throw new InvalidConfigurationException("Instance must be passed under the `session` config.");
54-
}
55-
56-
$instance = $config['instance'];
57-
$instances = $this->container->getParameter($this->getAlias().'.instance');
58-
59-
if (!isset($instances[$instance])) {
60-
throw new InvalidConfigurationException(
61-
sprintf(
62-
"Failed to hook into the session. The instance \"%s\" doesn't exist!",
63-
$instance
64-
)
65-
);
66-
}
67-
6853
// calculate options
6954
$sessionOptions = $this->container->getParameter('session.storage.options');
7055
if (isset($sessionOptions['cookie_lifetime']) && !isset($config['cookie_lifetime'])) {
7156
$config['cookie_lifetime'] = $sessionOptions['cookie_lifetime'];
7257
}
7358
// load the session handler
74-
$definition =
75-
new Definition($this->container->getParameter(sprintf('%s.session.handler.class', $this->getAlias())));
76-
$definition->addArgument(new Reference(sprintf('%s.instance.%s.bridge', $this->getAlias(), $instance)))
59+
$definition = new Definition(SessionHandler::class);
60+
$definition->addArgument(new Reference($config['service_id']))
7761
->addArgument($config);
7862

79-
$this->container->setDefinition(sprintf('%s.session_handler', $this->getAlias()), $definition);
80-
$this->container->setAlias('cache.session_handler', sprintf('%s.session_handler', $this->getAlias()));
63+
$this->container->setDefinition('cache.session_handler', $definition);
8164

8265
$this->container->setAlias('session.handler', 'cache.session_handler');
8366
}

src/DependencyInjection/Configuration.php

100644100755
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private function addSessionSupportSection()
5757
->booleanNode('enabled')
5858
->defaultFalse()
5959
->end()
60-
->scalarNode('instance')->end()
60+
->scalarNode('service_id')->isRequired()->end()
6161
->scalarNode('prefix')
6262
->defaultValue("session_")
6363
->end()
@@ -94,7 +94,7 @@ private function addDoctrineSection()
9494
->arrayNode($type)
9595
->canBeUnset()
9696
->children()
97-
->scalarNode('instance')->end()
97+
->scalarNode('service_id')->isRequired()->end()
9898
->arrayNode('entity_managers')
9999
->defaultValue(array())
100100
->beforeNormalization()
@@ -142,13 +142,13 @@ private function addRouterSection()
142142
->defaultFalse()
143143
->end()
144144
->integerNode('ttl')
145-
->isRequired()
145+
->defaultValue(86400)
146146
->end()
147147
->booleanNode('auto-register')
148148
->defaultTrue()
149149
->end()
150-
->scalarNode('instance')
151-
->defaultNull()
150+
->scalarNode('service_id')
151+
->isRequired()
152152
->end()
153153
->end();
154154

src/Routing/Router.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
*/
2424
class Router extends BaseRouter
2525
{
26-
const CACHE_LIFETIME = 604800; // a week
27-
2826
/**
2927
* @var ContainerInterface
3028
*/
@@ -92,8 +90,20 @@ public function setCache(CacheItemPoolInterface $cache)
9290
/**
9391
* @return int
9492
*/
95-
public function getTtl()
93+
protected function getTtl()
9694
{
9795
return $this->ttl;
9896
}
97+
98+
/**
99+
* @param int $ttl
100+
*
101+
* @return Router
102+
*/
103+
public function setTtl($ttl)
104+
{
105+
$this->ttl = $ttl;
106+
107+
return $this;
108+
}
99109
}

0 commit comments

Comments
 (0)