Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5079f34

Browse files
committed
feature #9892 [Validator] Added Doctrine cache (florianv)
This PR was merged into the 2.5-dev branch. Discussion ---------- [Validator] Added Doctrine cache | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #9887 | License | MIT | Doc PR | I propose to keep the `CacheInterface` and deprecate only the `ApcCache`. It will leave the classes depending on a `CacheInterface` unchanged and will allow to adapt new cache providers in the future. Commits ------- 3c4de45 [Validator] Added Doctrine cache
2 parents c15175a + 3c4de45 commit 5079f34

File tree

6 files changed

+187
-1
lines changed

6 files changed

+187
-1
lines changed

UPGRADE-3.0.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,29 @@ UPGRADE FROM 2.x to 3.0
443443

444444
### Validator
445445

446+
* The class `Symfony\Component\Validator\Mapping\Cache\ApcCache` has been removed in favor
447+
of `Symfony\Component\Validator\Mapping\Cache\DoctrineCache`.
448+
449+
Before:
450+
451+
```
452+
use Symfony\Component\Validator\Mapping\Cache\ApcCache;
453+
454+
$cache = new ApcCache('symfony.validator');
455+
```
456+
457+
After:
458+
459+
```
460+
use Symfony\Component\Validator\Mapping\Cache\DoctrineCache;
461+
use Doctrine\Common\Cache\ApcCache;
462+
463+
$apcCache = new ApcCache();
464+
$apcCache->setNamespace('symfony.validator');
465+
466+
$cache = new DoctrineCache($apcCache);
467+
```
468+
446469
* The constraints `Optional` and `Required` were moved to the
447470
`Symfony\Component\Validator\Constraints\` namespace. You should adapt
448471
the path wherever you used them.

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
2.5.0
5+
-----
6+
7+
* deprecated `ApcCache` in favor of `DoctrineCache`
8+
* added `DoctrineCache` to adapt any Doctrine cache
9+
410
2.4.0
511
-----
612

src/Symfony/Component/Validator/Mapping/Cache/ApcCache.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
use Symfony\Component\Validator\Mapping\ClassMetadata;
1515

16+
/**
17+
* @deprecated Deprecated since version 2.5, to be removed in 3.0.
18+
* Use DoctrineCache with Doctrine\Common\Cache\ApcCache instead.
19+
*/
1620
class ApcCache implements CacheInterface
1721
{
1822
private $prefix;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Mapping\Cache;
13+
14+
use Symfony\Component\Validator\Mapping\ClassMetadata;
15+
use Doctrine\Common\Cache\Cache;
16+
17+
/**
18+
* Adapts a Doctrine cache to a CacheInterface.
19+
*
20+
* @author Florian Voutzinos <[email protected]>
21+
*/
22+
final class DoctrineCache implements CacheInterface
23+
{
24+
private $cache;
25+
26+
/**
27+
* Creates a new Doctrine cache.
28+
*
29+
* @param Cache $cache The cache to adapt
30+
*/
31+
public function __construct(Cache $cache)
32+
{
33+
$this->cache = $cache;
34+
}
35+
36+
/**
37+
* Sets the cache to adapt.
38+
*
39+
* @param Cache $cache The cache to adapt
40+
*/
41+
public function setCache(Cache $cache)
42+
{
43+
$this->cache = $cache;
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function has($class)
50+
{
51+
return $this->cache->contains($class);
52+
}
53+
54+
/**
55+
* {@inheritdoc}
56+
*/
57+
public function read($class)
58+
{
59+
return $this->cache->fetch($class);
60+
}
61+
62+
/**
63+
* {@inheritdoc}
64+
*/
65+
public function write(ClassMetadata $metadata)
66+
{
67+
$this->cache->save($metadata->getClassName(), $metadata);
68+
}
69+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Mapping\Cache;
13+
14+
use Symfony\Component\Validator\Mapping\Cache\DoctrineCache;
15+
use Doctrine\Common\Cache\ArrayCache;
16+
17+
class DoctrineCacheTest extends \PHPUnit_Framework_TestCase
18+
{
19+
private $cache;
20+
21+
public function testWrite()
22+
{
23+
$meta = $this->getMockBuilder('Symfony\\Component\\Validator\\Mapping\\ClassMetadata')
24+
->disableOriginalConstructor()
25+
->setMethods(array('getClassName'))
26+
->getMock();
27+
28+
$meta->expects($this->once())
29+
->method('getClassName')
30+
->will($this->returnValue('bar'));
31+
32+
$this->cache->write($meta);
33+
34+
$this->assertInstanceOf(
35+
'Symfony\\Component\\Validator\\Mapping\\ClassMetadata',
36+
$this->cache->read('bar'),
37+
'write() stores metadata'
38+
);
39+
}
40+
41+
public function testHas()
42+
{
43+
$meta = $this->getMockBuilder('Symfony\\Component\\Validator\\Mapping\\ClassMetadata')
44+
->disableOriginalConstructor()
45+
->setMethods(array('getClassName'))
46+
->getMock();
47+
48+
$meta->expects($this->once())
49+
->method('getClassName')
50+
->will($this->returnValue('bar'));
51+
52+
$this->assertFalse($this->cache->has('bar'), 'has() returns false when there is no entry');
53+
54+
$this->cache->write($meta);
55+
$this->assertTrue($this->cache->has('bar'), 'has() returns true when the is an entry');
56+
}
57+
58+
public function testRead()
59+
{
60+
$meta = $this->getMockBuilder('Symfony\\Component\\Validator\\Mapping\\ClassMetadata')
61+
->disableOriginalConstructor()
62+
->setMethods(array('getClassName'))
63+
->getMock();
64+
65+
$meta->expects($this->once())
66+
->method('getClassName')
67+
->will($this->returnValue('bar'));
68+
69+
$this->assertFalse($this->cache->read('bar'), 'read() returns false when there is no entry');
70+
71+
$this->cache->write($meta);
72+
73+
$this->assertInstanceOf(
74+
'Symfony\\Component\\Validator\\Mapping\\ClassMetadata',
75+
$this->cache->read('bar'),
76+
'read() returns metadata'
77+
);
78+
}
79+
80+
protected function setUp()
81+
{
82+
$this->cache = new DoctrineCache(new ArrayCache);
83+
}
84+
}

src/Symfony/Component/Validator/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
},
3131
"suggest": {
3232
"doctrine/annotations": "For using the annotation mapping. You will also need doctrine/cache.",
33-
"doctrine/cache": "For using the default cached annotation reader",
33+
"doctrine/cache": "For using the default cached annotation reader and metadata cache.",
3434
"symfony/http-foundation": "",
3535
"symfony/intl": "",
3636
"symfony/yaml": "",

0 commit comments

Comments
 (0)