Skip to content

Commit 37f5264

Browse files
committed
feature #17440 [Validator] Add a PSR-6 adapter (dunglas)
This PR was squashed before being merged into the 3.1-dev branch (closes #17440). Discussion ---------- [Validator] Add a PSR-6 adapter | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | todo Commits ------- 1f0ba00 [Validator] Add a PSR-6 adapter
2 parents 420989f + 1f0ba00 commit 37f5264

File tree

5 files changed

+181
-62
lines changed

5 files changed

+181
-62
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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 Psr\Cache\CacheItemPoolInterface;
15+
use Symfony\Component\Validator\Mapping\ClassMetadata;
16+
17+
/**
18+
* PSR-6 adapter.
19+
*
20+
* @author Kévin Dunglas <dunglas@gmail.com>
21+
*/
22+
class Psr6Cache implements CacheInterface
23+
{
24+
/**
25+
* @var CacheItemPoolInterface
26+
*/
27+
private $cacheItemPool;
28+
29+
public function __construct(CacheItemPoolInterface $cacheItemPool)
30+
{
31+
$this->cacheItemPool = $cacheItemPool;
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function has($class)
38+
{
39+
$item = $this->cacheItemPool->getItem($this->escapeClassName($class));
40+
41+
return $item->isHit();
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function read($class)
48+
{
49+
$item = $this->cacheItemPool->getItem($this->escapeClassName($class));
50+
51+
if (!$item->isHit()) {
52+
return false;
53+
}
54+
55+
return $item->get();
56+
}
57+
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
public function write(ClassMetadata $metadata)
62+
{
63+
$item = $this->cacheItemPool->getItem($this->escapeClassName($metadata->getClassName()));
64+
$item->set($metadata);
65+
66+
$this->cacheItemPool->save($item);
67+
}
68+
69+
/**
70+
* Replaces backslashes by underscores in a class name.
71+
*
72+
* @param string $class
73+
*
74+
* @return string
75+
*/
76+
private function escapeClassName($class)
77+
{
78+
return strtr($class, '\\', '_');
79+
}
80+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\CacheInterface;
15+
16+
abstract class AbstractCacheTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* @var CacheInterface
20+
*/
21+
protected $cache;
22+
23+
public function testWrite()
24+
{
25+
$meta = $this->getMockBuilder('Symfony\\Component\\Validator\\Mapping\\ClassMetadata')
26+
->disableOriginalConstructor()
27+
->setMethods(array('getClassName'))
28+
->getMock();
29+
30+
$meta->expects($this->once())
31+
->method('getClassName')
32+
->will($this->returnValue('Foo\\Bar'));
33+
34+
$this->cache->write($meta);
35+
36+
$this->assertInstanceOf(
37+
'Symfony\\Component\\Validator\\Mapping\\ClassMetadata',
38+
$this->cache->read('Foo\\Bar'),
39+
'write() stores metadata'
40+
);
41+
}
42+
43+
public function testHas()
44+
{
45+
$meta = $this->getMockBuilder('Symfony\\Component\\Validator\\Mapping\\ClassMetadata')
46+
->disableOriginalConstructor()
47+
->setMethods(array('getClassName'))
48+
->getMock();
49+
50+
$meta->expects($this->once())
51+
->method('getClassName')
52+
->will($this->returnValue('Foo\\Bar'));
53+
54+
$this->assertFalse($this->cache->has('Foo\\Bar'), 'has() returns false when there is no entry');
55+
56+
$this->cache->write($meta);
57+
$this->assertTrue($this->cache->has('Foo\\Bar'), 'has() returns true when the is an entry');
58+
}
59+
60+
public function testRead()
61+
{
62+
$meta = $this->getMockBuilder('Symfony\\Component\\Validator\\Mapping\\ClassMetadata')
63+
->disableOriginalConstructor()
64+
->setMethods(array('getClassName'))
65+
->getMock();
66+
67+
$meta->expects($this->once())
68+
->method('getClassName')
69+
->will($this->returnValue('Foo\\Bar'));
70+
71+
$this->assertFalse($this->cache->read('Foo\\Bar'), 'read() returns false when there is no entry');
72+
73+
$this->cache->write($meta);
74+
75+
$this->assertInstanceOf(
76+
'Symfony\\Component\\Validator\\Mapping\\ClassMetadata',
77+
$this->cache->read('Foo\\Bar'),
78+
'read() returns metadata'
79+
);
80+
}
81+
}

src/Symfony/Component/Validator/Tests/Mapping/Cache/DoctrineCacheTest.php

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -14,69 +14,8 @@
1414
use Doctrine\Common\Cache\ArrayCache;
1515
use Symfony\Component\Validator\Mapping\Cache\DoctrineCache;
1616

17-
class DoctrineCacheTest extends \PHPUnit_Framework_TestCase
17+
class DoctrineCacheTest extends AbstractCacheTest
1818
{
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-
8019
protected function setUp()
8120
{
8221
$this->cache = new DoctrineCache(new ArrayCache());
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Symfony\Component\Validator\Tests\Mapping\Cache;
4+
5+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
6+
use Symfony\Component\Validator\Mapping\Cache\Psr6Cache;
7+
8+
/**
9+
* @author Kévin Dunglas <dunglas@gmail.com>
10+
*/
11+
class Psr6CacheTest extends AbstractCacheTest
12+
{
13+
protected function setUp()
14+
{
15+
$this->cache = new Psr6Cache(new ArrayAdapter());
16+
}
17+
}

src/Symfony/Component/Validator/composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
"symfony/config": "~2.8|~3.0",
2727
"symfony/property-access": "~2.8|~3.0",
2828
"symfony/expression-language": "~2.8|~3.0",
29+
"symfony/cache": "~3.1",
2930
"doctrine/annotations": "~1.0",
3031
"doctrine/cache": "~1.0",
3132
"egulias/email-validator": "~1.2,>=1.2.1"
3233
},
3334
"suggest": {
35+
"psr/cache-implementation": "For using the metadata cache.",
3436
"doctrine/annotations": "For using the annotation mapping. You will also need doctrine/cache.",
3537
"doctrine/cache": "For using the default cached annotation reader and metadata cache.",
3638
"symfony/http-foundation": "",

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy