Skip to content

Commit ae71ff3

Browse files
committed
Merge pull request #35 from Nyholm/features
Add cache for annotation, serializer and validation
2 parents c05def9 + 95cf28d commit ae71ff3

File tree

9 files changed

+365
-17
lines changed

9 files changed

+365
-17
lines changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,51 @@ 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.
145+
146+
```yml
147+
cache:
148+
annotation:
149+
enabled: true
150+
service_id: cache.provider.acme_apc_cache
151+
152+
framwork:
153+
annotations:
154+
cache: cache.service.annotation
155+
```
156+
157+
#### Serialization
158+
159+
To use a PSR-6 cache for the serialzer, use the following confguration.
160+
161+
```yml
162+
cache:
163+
serializer:
164+
enabled: true
165+
service_id: cache.provider.acme_apc_cache
166+
167+
framwork:
168+
serializer:
169+
cache: cache.service.serializer
170+
```
171+
172+
#### Validation
173+
174+
To use a PSR-6 cache for the validation, use the following confguration.
175+
176+
```yml
177+
cache:
178+
validation:
179+
enabled: true
180+
service_id: cache.provider.acme_apc_cache
181+
182+
framwork:
183+
validation:
184+
cache: cache.service.validation
185+
```
186+
142187

143188
### Clearing the cache
144189

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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\Bridge;
13+
14+
use Psr\Cache\CacheItemPoolInterface;
15+
use Symfony\Component\Validator\Mapping\Cache\CacheInterface;
16+
use Symfony\Component\Validator\Mapping\ClassMetadata;
17+
18+
/**
19+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
20+
*/
21+
class SymfonyValidatorBridge implements CacheInterface
22+
{
23+
/**
24+
* @type CacheItemPoolInterface
25+
*/
26+
private $pool;
27+
28+
/**
29+
* DoctrineCacheBridge constructor.
30+
*
31+
* @param CacheItemPoolInterface $cachePool
32+
*/
33+
public function __construct(CacheItemPoolInterface $cachePool)
34+
{
35+
$this->pool = $cachePool;
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function has($class)
42+
{
43+
return $this->pool->hasItem($this->normalizeKey($class));
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function read($class)
50+
{
51+
$item = $this->pool->getItem($this->normalizeKey($class));
52+
53+
if (!$item->isHit()) {
54+
return false;
55+
}
56+
57+
return $item->get();
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function write(ClassMetadata $metadata)
64+
{
65+
$item = $this->pool->getItem($this->normalizeKey($metadata->getClassName()));
66+
$item->set($metadata);
67+
$this->pool->save($item);
68+
}
69+
70+
/**
71+
* @param string $key
72+
*
73+
* @return string
74+
*/
75+
private function normalizeKey($key)
76+
{
77+
return preg_replace('|[\\\/]|', '.', $key);
78+
}
79+
}

src/Command/CacheFlushCommand.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*/
2727
class CacheFlushCommand extends ContainerAwareCommand
2828
{
29-
const VALID_TYPES = ['all', 'session', 'router', 'doctrine', 'symfony', 'provider'];
29+
const VALID_TYPES = ['all', 'annotation', 'session', 'serializer', 'router', 'doctrine', 'symfony', 'validation', 'provider'];
3030

3131
/**
3232
* {@inheritdoc}
@@ -41,11 +41,14 @@ protected function configure()
4141
4242
Types and their description
4343
all Clear all types of caches
44+
annotation Clear annotation cache
4445
doctrine Clear doctrine cache for query, result and metadata
4546
privider cache.acme Clear all the cache for the provider with service id "cache.acme"
4647
router Clear router cache
48+
serializer Clear serializer cache
4749
session Clear session cache. All your logged in users will be logged out.
4850
symfony Clear Symfony cache. This is the same as cache:clear
51+
validation Clear validation cache
4952
5053
EOD
5154
);
@@ -60,7 +63,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6063
return 0;
6164
}
6265

63-
$builtInTypes = ['session', 'router', 'doctrine'];
66+
$builtInTypes = ['annotation', 'doctrine', 'serializer', 'session', 'router', 'validation'];
6467
if (in_array($type, $builtInTypes)) {
6568
return $this->clearCacheForBuiltInType($type) ? 0 : 1;
6669
}

src/DependencyInjection/CacheExtension.php

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

1212
namespace Cache\CacheBundle\DependencyInjection;
1313

14+
use Cache\Bridge\DoctrineCacheBridge;
15+
use Cache\CacheBundle\Bridge\SymfonyValidatorBridge;
16+
use Cache\CacheBundle\Factory\AnnotationFactory;
17+
use Cache\CacheBundle\Factory\SerializerFactory;
18+
use Cache\CacheBundle\Factory\ValidationFactory;
1419
use Symfony\Component\Config\FileLocator;
1520
use Symfony\Component\DependencyInjection\ContainerBuilder;
1621
use Symfony\Component\DependencyInjection\Loader;
@@ -36,12 +41,35 @@ public function load(array $configs, ContainerBuilder $container)
3641
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
3742

3843
// Make sure config values are in the parameters
39-
foreach (['router', 'session', 'doctrine', 'logging'] as $section) {
44+
foreach (['router', 'session', 'doctrine', 'logging', 'annotation', 'serializer', 'validation'] as $section) {
4045
if ($config[$section]['enabled']) {
4146
$container->setParameter('cache.'.$section, $config[$section]);
4247
}
4348
}
4449

50+
if ($config['annotation']['enabled']) {
51+
$this->verifyDoctrineBridgeExists('annotation');
52+
$container->register('cache.service.annotation', DoctrineCacheBridge::class)
53+
->setFactory([AnnotationFactory::class, 'get'])
54+
->addArgument(new Reference($config['annotation']['service_id']))
55+
->addArgument($config['annotation']);
56+
}
57+
58+
if ($config['serializer']['enabled']) {
59+
$this->verifyDoctrineBridgeExists('serializer');
60+
$container->register('cache.service.serializer', DoctrineCacheBridge::class)
61+
->setFactory([SerializerFactory::class, 'get'])
62+
->addArgument(new Reference($config['serializer']['service_id']))
63+
->addArgument($config['serializer']);
64+
}
65+
66+
if ($config['validation']['enabled']) {
67+
$container->register('cache.service.validation', SymfonyValidatorBridge::class)
68+
->setFactory([ValidationFactory::class, 'get'])
69+
->addArgument(new Reference($config['validation']['service_id']))
70+
->addArgument($config['validation']);
71+
}
72+
4573
if ($config['router']['enabled']) {
4674
$loader->load('router.yml');
4775
$container->getDefinition('cache.router')
@@ -76,6 +104,23 @@ protected function findServiceIds(array $config, array &$serviceIds)
76104
}
77105
}
78106

107+
/**
108+
* Make sure the DoctrineBridge is installed.
109+
*
110+
* @param string $name
111+
*
112+
* @throws \Exception
113+
*/
114+
private function verifyDoctrineBridgeExists($name)
115+
{
116+
if (!class_exists('Cache\Bridge\DoctrineCacheBridge')) {
117+
throw new \Exception(sprintf(
118+
'You need the DoctrineBridge to be able to use "%s". Please run "composer require cache/psr-6-doctrine-bridge" to install the missing dependency.',
119+
$name
120+
));
121+
}
122+
}
123+
79124
/**
80125
* @return string
81126
*/

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;

src/DependencyInjection/Configuration.php

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public function getConfigTreeBuilder()
3636
->append($this->addSessionSupportSection())
3737
->append($this->addDoctrineSection())
3838
->append($this->addRouterSection())
39+
->append($this->addAnnotationSection())
40+
->append($this->addSerializerSection())
41+
->append($this->addValidationSection())
3942
->append($this->addLoggingSection())
4043
->end();
4144

@@ -53,11 +56,9 @@ private function addSessionSupportSection()
5356
$node = $tree->root('session');
5457

5558
$node
59+
->canBeEnabled()
5660
->addDefaultsIfNotSet()
5761
->children()
58-
->booleanNode('enabled')
59-
->defaultFalse()
60-
->end()
6162
->scalarNode('service_id')->isRequired()->end()
6263
->scalarNode('prefix')
6364
->defaultValue('session_')
@@ -68,6 +69,69 @@ private function addSessionSupportSection()
6869
return $node;
6970
}
7071

72+
/**
73+
* Configure the "cache.serializer" section.
74+
*
75+
* @return ArrayNodeDefinition
76+
*/
77+
private function addSerializerSection()
78+
{
79+
$tree = new TreeBuilder();
80+
$node = $tree->root('serializer');
81+
82+
$node
83+
->canBeEnabled()
84+
->addDefaultsIfNotSet()
85+
->children()
86+
->scalarNode('service_id')->isRequired()->end()
87+
->booleanNode('use_tagging')->defaultTrue()->end()
88+
->end();
89+
90+
return $node;
91+
}
92+
93+
/**
94+
* Configure the "cache.serializer" section.
95+
*
96+
* @return ArrayNodeDefinition
97+
*/
98+
private function addValidationSection()
99+
{
100+
$tree = new TreeBuilder();
101+
$node = $tree->root('validation');
102+
103+
$node
104+
->canBeEnabled()
105+
->addDefaultsIfNotSet()
106+
->children()
107+
->scalarNode('service_id')->isRequired()->end()
108+
->booleanNode('use_tagging')->defaultTrue()->end()
109+
->end();
110+
111+
return $node;
112+
}
113+
114+
/**
115+
* Configure the "cache.annotation" section.
116+
*
117+
* @return ArrayNodeDefinition
118+
*/
119+
private function addAnnotationSection()
120+
{
121+
$tree = new TreeBuilder();
122+
$node = $tree->root('annotation');
123+
124+
$node
125+
->canBeEnabled()
126+
->addDefaultsIfNotSet()
127+
->children()
128+
->scalarNode('service_id')->isRequired()->end()
129+
->booleanNode('use_tagging')->defaultTrue()->end()
130+
->end();
131+
132+
return $node;
133+
}
134+
71135
/**
72136
* @return ArrayNodeDefinition
73137
*/
@@ -77,11 +141,9 @@ private function addLoggingSection()
77141
$node = $tree->root('logging');
78142

79143
$node
144+
->canBeEnabled()
80145
->addDefaultsIfNotSet()
81146
->children()
82-
->booleanNode('enabled')
83-
->defaultFalse()
84-
->end()
85147
->scalarNode('logger')->defaultValue('logger')->end()
86148
->scalarNode('level')->defaultValue('info')->end()
87149
->end();
@@ -100,12 +162,9 @@ private function addDoctrineSection()
100162
$node = $tree->root('doctrine');
101163

102164
$node
165+
->canBeEnabled()
103166
->addDefaultsIfNotSet()
104167
->children()
105-
->booleanNode('enabled')
106-
->defaultFalse()
107-
->isRequired()
108-
->end()
109168
->booleanNode('use_tagging')
110169
->defaultTrue()
111170
->end()
@@ -159,11 +218,10 @@ private function addRouterSection()
159218
$tree = new TreeBuilder();
160219
$node = $tree->root('router');
161220

162-
$node->addDefaultsIfNotSet()
221+
$node
222+
->canBeEnabled()
223+
->addDefaultsIfNotSet()
163224
->children()
164-
->booleanNode('enabled')
165-
->defaultFalse()
166-
->end()
167225
->integerNode('ttl')
168226
->defaultValue(604800)
169227
->end()

0 commit comments

Comments
 (0)