Skip to content

[Validator] Add a PSR-6 adapter #17440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Mapping\Cache;

use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata;

/**
* PSR-6 adapter.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class Psr6Cache implements CacheInterface
{
/**
* @var CacheItemPoolInterface
*/
private $cacheItemPool;

public function __construct(CacheItemPoolInterface $cacheItemPool)
{
$this->cacheItemPool = $cacheItemPool;
}

/**
* {@inheritdoc}
*/
public function has($class)
{
$item = $this->cacheItemPool->getItem($this->escapeClassName($class));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simply use return $this->cacheItemPool->hasItem($this->escapeClassName($class)); ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the PSR-6, hasItem can in be unreliable:

     * Note: This method MAY avoid retrieving the cached value for performance reasons.
     * This could result in a race condition with CacheItemInterface::get(). To avoid
     * such situation use CacheItemInterface::isHit() instead.

It looks better to me to keep it as is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance doesn't matter here, the metadata caching occurs at "compile" time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dunglas that PSR-6 quote is correct but in this context here it is prone to race conditions anyway if used incorrectly as read() and has() are seperate methods working independently. So your code to retrieve the cache data first for no reason makes no sense and @dmaicher proposal is valid.


return $item->isHit();
}

/**
* {@inheritdoc}
*/
public function read($class)
{
$item = $this->cacheItemPool->getItem($this->escapeClassName($class));

if (!$item->isHit()) {
return false;
}

return $item->get();
}

/**
* {@inheritdoc}
*/
public function write(ClassMetadata $metadata)
{
$item = $this->cacheItemPool->getItem($this->escapeClassName($metadata->getClassName()));
$item->set($metadata);

$this->cacheItemPool->save($item);
}

/**
* Replaces backslashes by underscores in a class name.
*
* @param string $class
*
* @return string
*/
private function escapeClassName($class)
{
return strtr($class, '\\', '_');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we normalized this sort of operations to str_replace before

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Tests\Mapping\Cache;

use Symfony\Component\Validator\Mapping\Cache\CacheInterface;

abstract class AbstractCacheTest extends \PHPUnit_Framework_TestCase
{
/**
* @var CacheInterface
*/
protected $cache;

public function testWrite()
{
$meta = $this->getMockBuilder('Symfony\\Component\\Validator\\Mapping\\ClassMetadata')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for new code we should really use ::class

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I'll open another PR.

->disableOriginalConstructor()
->setMethods(array('getClassName'))
->getMock();

$meta->expects($this->once())
->method('getClassName')
->will($this->returnValue('Foo\\Bar'));

$this->cache->write($meta);

$this->assertInstanceOf(
'Symfony\\Component\\Validator\\Mapping\\ClassMetadata',
$this->cache->read('Foo\\Bar'),
'write() stores metadata'
);
}

public function testHas()
{
$meta = $this->getMockBuilder('Symfony\\Component\\Validator\\Mapping\\ClassMetadata')
->disableOriginalConstructor()
->setMethods(array('getClassName'))
->getMock();

$meta->expects($this->once())
->method('getClassName')
->will($this->returnValue('Foo\\Bar'));

$this->assertFalse($this->cache->has('Foo\\Bar'), 'has() returns false when there is no entry');

$this->cache->write($meta);
$this->assertTrue($this->cache->has('Foo\\Bar'), 'has() returns true when the is an entry');
}

public function testRead()
{
$meta = $this->getMockBuilder('Symfony\\Component\\Validator\\Mapping\\ClassMetadata')
->disableOriginalConstructor()
->setMethods(array('getClassName'))
->getMock();

$meta->expects($this->once())
->method('getClassName')
->will($this->returnValue('Foo\\Bar'));

$this->assertFalse($this->cache->read('Foo\\Bar'), 'read() returns false when there is no entry');

$this->cache->write($meta);

$this->assertInstanceOf(
'Symfony\\Component\\Validator\\Mapping\\ClassMetadata',
$this->cache->read('Foo\\Bar'),
'read() returns metadata'
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,69 +14,8 @@
use Doctrine\Common\Cache\ArrayCache;
use Symfony\Component\Validator\Mapping\Cache\DoctrineCache;

class DoctrineCacheTest extends \PHPUnit_Framework_TestCase
class DoctrineCacheTest extends AbstractCacheTest
{
private $cache;

public function testWrite()
{
$meta = $this->getMockBuilder('Symfony\\Component\\Validator\\Mapping\\ClassMetadata')
->disableOriginalConstructor()
->setMethods(array('getClassName'))
->getMock();

$meta->expects($this->once())
->method('getClassName')
->will($this->returnValue('bar'));

$this->cache->write($meta);

$this->assertInstanceOf(
'Symfony\\Component\\Validator\\Mapping\\ClassMetadata',
$this->cache->read('bar'),
'write() stores metadata'
);
}

public function testHas()
{
$meta = $this->getMockBuilder('Symfony\\Component\\Validator\\Mapping\\ClassMetadata')
->disableOriginalConstructor()
->setMethods(array('getClassName'))
->getMock();

$meta->expects($this->once())
->method('getClassName')
->will($this->returnValue('bar'));

$this->assertFalse($this->cache->has('bar'), 'has() returns false when there is no entry');

$this->cache->write($meta);
$this->assertTrue($this->cache->has('bar'), 'has() returns true when the is an entry');
}

public function testRead()
{
$meta = $this->getMockBuilder('Symfony\\Component\\Validator\\Mapping\\ClassMetadata')
->disableOriginalConstructor()
->setMethods(array('getClassName'))
->getMock();

$meta->expects($this->once())
->method('getClassName')
->will($this->returnValue('bar'));

$this->assertFalse($this->cache->read('bar'), 'read() returns false when there is no entry');

$this->cache->write($meta);

$this->assertInstanceOf(
'Symfony\\Component\\Validator\\Mapping\\ClassMetadata',
$this->cache->read('bar'),
'read() returns metadata'
);
}

protected function setUp()
{
$this->cache = new DoctrineCache(new ArrayCache());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Symfony\Component\Validator\Tests\Mapping\Cache;

use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Validator\Mapping\Cache\Psr6Cache;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class Psr6CacheTest extends AbstractCacheTest
{
protected function setUp()
{
$this->cache = new Psr6Cache(new ArrayAdapter());
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
"symfony/config": "~2.8|~3.0",
"symfony/property-access": "~2.8|~3.0",
"symfony/expression-language": "~2.8|~3.0",
"symfony/cache": "~3.1",
"doctrine/annotations": "~1.0",
"doctrine/cache": "~1.0",
"egulias/email-validator": "~1.2,>=1.2.1"
},
"suggest": {
"psr/cache-implementation": "For using the metadata cache.",
"doctrine/annotations": "For using the annotation mapping. You will also need doctrine/cache.",
"doctrine/cache": "For using the default cached annotation reader and metadata cache.",
"symfony/http-foundation": "",
Expand Down
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