Skip to content

Commit 1919717

Browse files
committed
Added Cache for serializer and annotation
1 parent c05def9 commit 1919717

File tree

7 files changed

+237
-1
lines changed

7 files changed

+237
-1
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,30 @@ cache:
139139
level: 'info' # Default logging level
140140
```
141141

142+
#### Annotation
143+
144+
To use a PSR-6 cache for your annotations, use the following confguration. This will overwrite the value at
145+
`framwork.annotations.cache`
146+
147+
```yml
148+
cache:
149+
annotation:
150+
enabled: true
151+
service_id: cache.provider.acme_apc_cache
152+
```
153+
154+
#### Serialization
155+
156+
To use a PSR-6 cache for the serialzer, use the following confguration. This will overwrite the value at
157+
`framwork.serializer.cache`
158+
159+
```yml
160+
cache:
161+
serializer:
162+
enabled: true
163+
service_id: cache.provider.acme_apc_cache
164+
```
165+
142166

143167
### Clearing the cache
144168

src/DependencyInjection/CacheExtension.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Cache\CacheBundle\DependencyInjection;
1313

14+
use Cache\Bridge\DoctrineCacheBridge;
1415
use Symfony\Component\Config\FileLocator;
1516
use Symfony\Component\DependencyInjection\ContainerBuilder;
1617
use Symfony\Component\DependencyInjection\Loader;
@@ -36,12 +37,29 @@ public function load(array $configs, ContainerBuilder $container)
3637
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
3738

3839
// Make sure config values are in the parameters
39-
foreach (['router', 'session', 'doctrine', 'logging'] as $section) {
40+
foreach (['router', 'session', 'doctrine', 'logging', 'annotation', 'serializer'] as $section) {
4041
if ($config[$section]['enabled']) {
4142
$container->setParameter('cache.'.$section, $config[$section]);
4243
}
4344
}
4445

46+
if ($config['annotation']['enabled']) {
47+
$this->verifyDoctrineBridgeExists('annotation');
48+
$container->register('cache.service.annotation', DoctrineCacheBridge::class)
49+
->setFactory('Cache\CacheBundle\Factory\AnnotationFactory::get')
50+
->addArgument(new Reference($config['annotation']['service_id']))
51+
->addArgument($config['annotation']);
52+
}
53+
54+
if ($config['serializer']['enabled']) {
55+
$this->verifyDoctrineBridgeExists('serializer');
56+
$container->register('cache.service.serializer', DoctrineCacheBridge::class)
57+
->setFactory('Cache\CacheBundle\Factory\SerializerFactory::get')
58+
->addArgument(new Reference($config['serializer']['service_id']))
59+
->addArgument($config['serializer']);
60+
}
61+
62+
4563
if ($config['router']['enabled']) {
4664
$loader->load('router.yml');
4765
$container->getDefinition('cache.router')
@@ -76,6 +94,22 @@ protected function findServiceIds(array $config, array &$serviceIds)
7694
}
7795
}
7896

97+
/**
98+
* Make sure the DoctrineBridge is installed
99+
*
100+
* @param string $name
101+
* @throws \Exception
102+
*/
103+
private function verifyDoctrineBridgeExists($name)
104+
{
105+
if (!class_exists('Cache\Bridge\DoctrineCacheBridge')) {
106+
throw new \Exception(sprintf(
107+
'You need the DoctrineBridge to be able to use "%s". Please run "composer require cache/psr-6-doctrine-bridge" to install the missing dependency.',
108+
$name
109+
));
110+
}
111+
}
112+
79113
/**
80114
* @return string
81115
*/

src/DependencyInjection/Compiler/DoctrineSupportCompilerPass.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
*/
2727
class DoctrineSupportCompilerPass implements CompilerPassInterface
2828
{
29+
/**
30+
* @type ContainerBuilder
31+
*/
32+
private $container;
33+
2934
/**
3035
* @throws \Exception
3136
*
@@ -34,6 +39,7 @@ class DoctrineSupportCompilerPass implements CompilerPassInterface
3439
public function process(ContainerBuilder $container)
3540
{
3641
$this->container = $container;
42+
3743
// If disabled, continue
3844
if (!$this->container->hasParameter('cache.doctrine')) {
3945
return;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
18+
/**
19+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
20+
*/
21+
class ServiceInjectorPass implements CompilerPassInterface
22+
{
23+
/**
24+
* @throws \Exception
25+
*/
26+
public function process(ContainerBuilder $container)
27+
{
28+
$this->injectAnnotationService($container);
29+
$this->injectSerializerService($container);
30+
}
31+
32+
/**
33+
* @param ContainerBuilder $container
34+
*
35+
* @throws \Exception
36+
*/
37+
private function injectAnnotationService(ContainerBuilder $container)
38+
{
39+
// If disabled, continue
40+
if (!$container->hasParameter('cache.annotation')) {
41+
return;
42+
}
43+
44+
$container
45+
->getDefinition('annotations.cached_reader')
46+
->replaceArgument(1, new Reference('cache.service.annotation'));
47+
}
48+
49+
/**
50+
* @param ContainerBuilder $container
51+
*
52+
* @throws \Exception
53+
*/
54+
private function injectSerializerService(ContainerBuilder $container)
55+
{
56+
// If disabled, continue
57+
if (!$container->hasParameter('cache.serializer')) {
58+
return;
59+
}
60+
61+
$container->getDefinition('serializer.mapping.class_metadata_factory')
62+
->replaceArgument(1, new Reference('cache.service.serializer'));
63+
}
64+
}

src/DependencyInjection/Configuration.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public function getConfigTreeBuilder()
3636
->append($this->addSessionSupportSection())
3737
->append($this->addDoctrineSection())
3838
->append($this->addRouterSection())
39+
->append($this->addAnnotationSection())
40+
->append($this->addSerializerSection())
3941
->append($this->addLoggingSection())
4042
->end();
4143

@@ -68,6 +70,52 @@ private function addSessionSupportSection()
6870
return $node;
6971
}
7072

73+
/**
74+
* Configure the "cache.serializer" section.
75+
*
76+
* @return ArrayNodeDefinition
77+
*/
78+
private function addSerializerSection()
79+
{
80+
$tree = new TreeBuilder();
81+
$node = $tree->root('serializer');
82+
83+
$node
84+
->addDefaultsIfNotSet()
85+
->children()
86+
->booleanNode('enabled')
87+
->defaultFalse()
88+
->end()
89+
->scalarNode('service_id')->isRequired()->end()
90+
->booleanNode('use_tagging')->defaultTrue()->end()
91+
->end();
92+
93+
return $node;
94+
}
95+
96+
/**
97+
* Configure the "cache.annotation" section.
98+
*
99+
* @return ArrayNodeDefinition
100+
*/
101+
private function addAnnotationSection()
102+
{
103+
$tree = new TreeBuilder();
104+
$node = $tree->root('annotation');
105+
106+
$node
107+
->addDefaultsIfNotSet()
108+
->children()
109+
->booleanNode('enabled')
110+
->defaultFalse()
111+
->end()
112+
->scalarNode('service_id')->isRequired()->end()
113+
->booleanNode('use_tagging')->defaultTrue()->end()
114+
->end();
115+
116+
return $node;
117+
}
118+
71119
/**
72120
* @return ArrayNodeDefinition
73121
*/

src/Factory/AnnotationFactory.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Cache\CacheBundle\Factory;
4+
5+
use Cache\Bridge\DoctrineCacheBridge;
6+
use Cache\CacheBundle\Cache\FixedTaggingCachePool;
7+
use Psr\Cache\CacheItemPoolInterface;
8+
9+
/**
10+
*
11+
*
12+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
13+
*/
14+
class AnnotationFactory
15+
{
16+
/**
17+
* @param CacheItemPoolInterface $pool
18+
* @param array $config
19+
*
20+
* @return DoctrineCacheBridge
21+
*/
22+
public static function get(CacheItemPoolInterface $pool, $config)
23+
{
24+
if ($config['use_tagging']) {
25+
$pool = new FixedTaggingCachePool($pool, 'annotation');
26+
}
27+
28+
return new DoctrineCacheBridge($pool);
29+
}
30+
}

src/Factory/SerializerFactory.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Cache\CacheBundle\Factory;
4+
5+
use Cache\Bridge\DoctrineCacheBridge;
6+
use Cache\CacheBundle\Cache\FixedTaggingCachePool;
7+
use Psr\Cache\CacheItemPoolInterface;
8+
9+
/**
10+
*
11+
*
12+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
13+
*/
14+
class SerializerFactory
15+
{
16+
/**
17+
* @param CacheItemPoolInterface $pool
18+
* @param array $config
19+
*
20+
* @return DoctrineCacheBridge
21+
*/
22+
public static function get(CacheItemPoolInterface $pool, $config)
23+
{
24+
if ($config['use_tagging']) {
25+
$pool = new FixedTaggingCachePool($pool, 'serializer');
26+
}
27+
28+
return new DoctrineCacheBridge($pool);
29+
}
30+
}

0 commit comments

Comments
 (0)