Skip to content

Commit a757bd2

Browse files
committed
Merge pull request #1 from Nyholm/patch
Bugfixes and improvements
2 parents de1d479 + 224e625 commit a757bd2

12 files changed

+49
-69
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: 5 additions & 16 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
{
@@ -25,23 +27,9 @@ class DataCollectorCompilerPass extends BaseCompilerPass
2527
*/
2628
protected function prepare()
2729
{
28-
$this->transformLoggableCachePools();
29-
30-
$instances = $this->container->getParameter($this->getAlias() . '.instance');
31-
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));
37-
}
38-
39-
$this->container->setDefinition('data_collector.cache', $definition);
40-
}
41-
42-
private function transformLoggableCachePools()
43-
{
30+
$collectorDefinition = $this->container->getDefinition('data_collector.cache');
4431
$serviceIds = $this->container->findTaggedServiceIds('cache.provider');
32+
4533
foreach (array_keys($serviceIds) as $id) {
4634

4735
// Duplicating definition to $originalServiceId.logged
@@ -53,6 +41,7 @@ private function transformLoggableCachePools()
5341

5442
// Overwrite the original service id with the new LoggingCachePool instance
5543
$this->container->setAlias($id, $id.'.logger');
44+
$collectorDefinition->addMethodCall('addInstance', [$id, new Reference($id.'.logger')]);
5645
}
5746
}
5847
}

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/Matcher/CacheUrlMatcher.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,15 @@ public function match($pathInfo)
6363
$method = strtolower($this->context->getMethod());
6464
$key = 'route_'.$method.'_'.$host.'_'.$pathInfo;
6565

66-
if ($this->cachePool->hasItem($key)) {
67-
return $this->cachePool->getItem($key)->get();
66+
$cacheItem = $this->cachePool->getItem($key);
67+
if ($cacheItem->isHit()) {
68+
return $cacheItem->get();
6869
}
6970

7071
$match = parent::match($pathInfo);
71-
$item = $this->cachePool->getItem($key);
72-
$item->set($match)
72+
$cacheItem->set($match)
7373
->expiresAfter($this->ttl);
74+
$this->cachePool->save($cacheItem);
7475

7576
return $match;
7677
}

0 commit comments

Comments
 (0)