Skip to content

Commit 2c64d6b

Browse files
committed
Merge pull request #48 from php-cache/bugfix
Added comments and fixes routing issue
2 parents 29a3c02 + b5e79fd commit 2c64d6b

7 files changed

+63
-48
lines changed

src/DependencyInjection/CacheExtension.php

+55-40
Original file line numberDiff line numberDiff line change
@@ -53,57 +53,21 @@ public function load(array $configs, ContainerBuilder $container)
5353
$this->verifyDoctrineBridgeExists('doctrine');
5454
}
5555

56-
if ($config['annotation']['enabled']) {
57-
$this->verifyDoctrineBridgeExists('annotation');
58-
$container->register('cache.service.annotation', DoctrineCacheBridge::class)
59-
->setFactory([DoctrineBridgeFactory::class, 'get'])
60-
->addArgument(new Reference($config['annotation']['service_id']))
61-
->addArgument($config['annotation'])
62-
->addArgument('annotation');
63-
}
64-
65-
if ($config['serializer']['enabled']) {
66-
$this->verifyDoctrineBridgeExists('serializer');
67-
$container->register('cache.service.serializer', DoctrineCacheBridge::class)
68-
->setFactory([DoctrineBridgeFactory::class, 'get'])
69-
->addArgument(new Reference($config['serializer']['service_id']))
70-
->addArgument($config['serializer'])
71-
->addArgument('serializer');
72-
}
73-
74-
if ($config['validation']['enabled']) {
75-
$container->register('cache.service.validation', SymfonyValidatorBridge::class)
76-
->setFactory([ValidationFactory::class, 'get'])
77-
->addArgument(new Reference($config['validation']['service_id']))
78-
->addArgument($config['validation']);
79-
}
80-
81-
if ($config['session']['enabled']) {
82-
$container->register('cache.service.session', Psr6SessionHandler::class)
83-
->setFactory([SessionHandlerFactory::class, 'get'])
84-
->addArgument(new Reference($config['session']['service_id']))
85-
->addArgument($config['session']);
86-
}
87-
88-
if ($config['router']['enabled']) {
89-
$container->register('cache.service.router', CachingRouter::class)
90-
->setDecoratedService('router', null, 10)
91-
->addArgument(new Reference($config['router']['service_id']))
92-
->addArgument(new Reference('cache.service.router.inner'))
93-
->addArgument($config['router']);
94-
}
56+
$this->registerServices($container, $config);
9557

58+
// Add toolbar and data collector if we are debuging
9659
if ($container->getParameter('kernel.debug')) {
9760
$loader->load('data-collector.yml');
9861
}
9962

63+
// Get a list of the psr-6 services we are using.
10064
$serviceIds = [];
10165
$this->findServiceIds($config, $serviceIds);
10266
$container->setParameter('cache.provider_service_ids', $serviceIds);
10367
}
10468

10569
/**
106-
* Find service ids that we configured.
70+
* Find service ids that we configured. These services should be tagged so we can use them in the debug toolbar.
10771
*
10872
* @param array $config
10973
* @param array $serviceIds
@@ -143,4 +107,55 @@ public function getAlias()
143107
{
144108
return 'cache';
145109
}
110+
111+
/**
112+
* Register services. All service ids will start witn "cache.service.".
113+
*
114+
* @param ContainerBuilder $container
115+
* @param $config
116+
*
117+
* @throws \Exception
118+
*/
119+
private function registerServices(ContainerBuilder $container, $config)
120+
{
121+
if ($config['annotation']['enabled']) {
122+
$this->verifyDoctrineBridgeExists('annotation');
123+
$container->register('cache.service.annotation', DoctrineCacheBridge::class)
124+
->setFactory([DoctrineBridgeFactory::class, 'get'])
125+
->addArgument(new Reference($config['annotation']['service_id']))
126+
->addArgument($config['annotation'])
127+
->addArgument('annotation');
128+
}
129+
130+
if ($config['serializer']['enabled']) {
131+
$this->verifyDoctrineBridgeExists('serializer');
132+
$container->register('cache.service.serializer', DoctrineCacheBridge::class)
133+
->setFactory([DoctrineBridgeFactory::class, 'get'])
134+
->addArgument(new Reference($config['serializer']['service_id']))
135+
->addArgument($config['serializer'])
136+
->addArgument('serializer');
137+
}
138+
139+
if ($config['validation']['enabled']) {
140+
$container->register('cache.service.validation', SymfonyValidatorBridge::class)
141+
->setFactory([ValidationFactory::class, 'get'])
142+
->addArgument(new Reference($config['validation']['service_id']))
143+
->addArgument($config['validation']);
144+
}
145+
146+
if ($config['session']['enabled']) {
147+
$container->register('cache.service.session', Psr6SessionHandler::class)
148+
->setFactory([SessionHandlerFactory::class, 'get'])
149+
->addArgument(new Reference($config['session']['service_id']))
150+
->addArgument($config['session']);
151+
}
152+
153+
if ($config['router']['enabled']) {
154+
$container->register('cache.service.router', CachingRouter::class)
155+
->setDecoratedService('router', null, 10)
156+
->addArgument(new Reference($config['router']['service_id']))
157+
->addArgument(new Reference('cache.service.router.inner'))
158+
->addArgument($config['router']);
159+
}
160+
}
146161
}

src/DependencyInjection/Compiler/CacheTaggingPass.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616

1717
/**
18-
* Make sure to tag all cache provider used.
18+
* Make sure to tag all cache services we can find.
1919
*
2020
* @author Tobias Nyholm <[email protected]>
2121
*/

src/DependencyInjection/Compiler/DataCollectorCompilerPass.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Symfony\Component\DependencyInjection\Reference;
1818

1919
/**
20-
* Class DataCollectorCompilerPass.
20+
* Inject a data collector to all the cache services to be able to get detailed statistics.
2121
*
2222
* @author Aaron Scherer <[email protected]>
2323
* @author Tobias Nyholm <[email protected]>

src/DependencyInjection/Compiler/DoctrineCompilerPass.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use Symfony\Component\DependencyInjection\Reference;
2020

2121
/**
22-
* Class DoctrineSupportCompilerPass.
22+
* Add the doctrine bridge around the PSR-6 cache services.
2323
*
2424
* @author Aaron Scherer <[email protected]>
2525
* @author Tobias Nyholm <[email protected]>

src/DependencyInjection/Compiler/LoggingCompilerPass.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Symfony\Component\DependencyInjection\Reference;
1818

1919
/**
20-
* Decorate our cache.provider with a logger.
20+
* Decorate our cache.providers with a logger.
2121
*
2222
* @author Tobias Nyholm <[email protected]>
2323
*/

src/DependencyInjection/Compiler/SessionSupportCompilerPass.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616

1717
/**
18-
* Class SessionSupportCompilerPass.
18+
* Enable the session support by rewriting the "session.handler" alias.
1919
*
2020
* @author Aaron Scherer <[email protected]>
2121
*/

src/Routing/CachingRouter.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private function getCacheItemMatch($pathinfo)
125125
{
126126
/** @type RequestContext $c */
127127
$c = $this->getContext();
128-
$key = sprintf('%s__%s__%s__%s', $c->getMethod(), $c->getHost(), $pathinfo, $c->getQueryString());
128+
$key = sprintf('%s__%s__%s__%s', $c->getHost(), str_replace('/', '.', $pathinfo), $c->getMethod(), $c->getQueryString());
129129

130130
return $this->getCacheItemFromKey($key, 'match');
131131
}
@@ -141,8 +141,8 @@ private function getCacheItemMatch($pathinfo)
141141
*/
142142
private function getCacheItemGenerate($name, array $parameters, $referenceType)
143143
{
144-
sort($parameters);
145-
$key = sprintf('%s.%s.%s', $name, $referenceType ? 'true' : 'false', json_encode($parameters));
144+
asort($parameters);
145+
$key = sprintf('%s.%s.%s', $name, $referenceType ? 'true' : 'false', http_build_query($parameters));
146146

147147
return $this->getCacheItemFromKey($key, 'generate');
148148
}

0 commit comments

Comments
 (0)