Skip to content

Commit 00dbc21

Browse files
committed
Added Validation
1 parent 1919717 commit 00dbc21

File tree

6 files changed

+153
-1
lines changed

6 files changed

+153
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,18 @@ cache:
163163
service_id: cache.provider.acme_apc_cache
164164
```
165165

166+
#### Validation
167+
168+
To use a PSR-6 cache for the validation, use the following confguration. This will overwrite the value at
169+
`framwork.validation.cache`
170+
171+
```yml
172+
cache:
173+
validation:
174+
enabled: true
175+
service_id: cache.provider.acme_apc_cache
176+
```
177+
166178

167179
### Clearing the cache
168180

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Cache\CacheBundle\Bridge;
4+
5+
use Psr\Cache\CacheItemPoolInterface;
6+
use Symfony\Component\Validator\Mapping\Cache\CacheInterface;
7+
use Symfony\Component\Validator\Mapping\ClassMetadata;
8+
9+
/**
10+
*
11+
*
12+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
13+
*/
14+
class SymfonyValidatorBridge implements CacheInterface
15+
{
16+
/**
17+
* @type CacheItemPoolInterface
18+
*/
19+
private $pool;
20+
21+
/**
22+
* DoctrineCacheBridge constructor.
23+
*
24+
* @param CacheItemPoolInterface $cachePool
25+
*/
26+
public function __construct(CacheItemPoolInterface $cachePool)
27+
{
28+
$this->pool = $cachePool;
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function has($class)
35+
{
36+
return $this->pool->hasItem($class);
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function read($class)
43+
{
44+
$item = $this->pool->getItem($class);
45+
46+
if (!$item->isHit()) {
47+
return false;
48+
}
49+
50+
return $item->get();
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function write(ClassMetadata $metadata)
57+
{
58+
$item = $this->pool->getItem($metadata->getClassName());
59+
$item->set($metadata);
60+
$this->pool->save($item);
61+
}
62+
}

src/DependencyInjection/CacheExtension.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function load(array $configs, ContainerBuilder $container)
3737
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
3838

3939
// Make sure config values are in the parameters
40-
foreach (['router', 'session', 'doctrine', 'logging', 'annotation', 'serializer'] as $section) {
40+
foreach (['router', 'session', 'doctrine', 'logging', 'annotation', 'serializer', 'validation'] as $section) {
4141
if ($config[$section]['enabled']) {
4242
$container->setParameter('cache.'.$section, $config[$section]);
4343
}
@@ -59,6 +59,13 @@ public function load(array $configs, ContainerBuilder $container)
5959
->addArgument($config['serializer']);
6060
}
6161

62+
if ($config['validation']['enabled']) {
63+
$container->register('cache.service.validation', DoctrineCacheBridge::class)
64+
->setFactory('Cache\CacheBundle\Factory\ValidationFactory::get')
65+
->addArgument(new Reference($config['validation']['service_id']))
66+
->addArgument($config['validation']);
67+
}
68+
6269

6370
if ($config['router']['enabled']) {
6471
$loader->load('router.yml');

src/DependencyInjection/Compiler/ServiceInjectorPass.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function process(ContainerBuilder $container)
2727
{
2828
$this->injectAnnotationService($container);
2929
$this->injectSerializerService($container);
30+
$this->injectValidationService($container);
3031
}
3132

3233
/**
@@ -61,4 +62,19 @@ private function injectSerializerService(ContainerBuilder $container)
6162
$container->getDefinition('serializer.mapping.class_metadata_factory')
6263
->replaceArgument(1, new Reference('cache.service.serializer'));
6364
}
65+
/**
66+
* @param ContainerBuilder $container
67+
*
68+
* @throws \Exception
69+
*/
70+
private function injectValidationService(ContainerBuilder $container)
71+
{
72+
// If disabled, continue
73+
if (!$container->hasParameter('cache.validation')) {
74+
return;
75+
}
76+
77+
$container->getDefinition('validator.builder')
78+
->addMethodCall('setMetadataCache', [new Reference('cache.service.serializer')]);
79+
}
6480
}

src/DependencyInjection/Configuration.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function getConfigTreeBuilder()
3838
->append($this->addRouterSection())
3939
->append($this->addAnnotationSection())
4040
->append($this->addSerializerSection())
41+
->append($this->addValidationSection())
4142
->append($this->addLoggingSection())
4243
->end();
4344

@@ -93,6 +94,29 @@ private function addSerializerSection()
9394
return $node;
9495
}
9596

97+
/**
98+
* Configure the "cache.serializer" section.
99+
*
100+
* @return ArrayNodeDefinition
101+
*/
102+
private function addValidationSection()
103+
{
104+
$tree = new TreeBuilder();
105+
$node = $tree->root('validation');
106+
107+
$node
108+
->addDefaultsIfNotSet()
109+
->children()
110+
->booleanNode('enabled')
111+
->defaultFalse()
112+
->end()
113+
->scalarNode('service_id')->isRequired()->end()
114+
->booleanNode('use_tagging')->defaultTrue()->end()
115+
->end();
116+
117+
return $node;
118+
}
119+
96120
/**
97121
* Configure the "cache.annotation" section.
98122
*

src/Factory/ValidationFactory.php

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

0 commit comments

Comments
 (0)