From 34b141c4eb903c3b4cb31c2697435844ce31e3be Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Wed, 4 Sep 2019 20:28:45 +0200 Subject: [PATCH] [Validator] Removed CacheInterface in favor of PSR-6. --- .../Resources/config/validator.xml | 5 -- .../Mapping/Cache/CacheInterface.php | 45 ----------- .../Validator/Mapping/Cache/DoctrineCache.php | 63 --------------- .../Validator/Mapping/Cache/Psr6Cache.php | 80 ------------------- .../Factory/LazyLoadingMetadataFactory.php | 41 ++-------- .../Tests/Mapping/Cache/AbstractCacheTest.php | 79 ------------------ .../Tests/Mapping/Cache/DoctrineCacheTest.php | 26 ------ .../Tests/Mapping/Cache/Psr6CacheTest.php | 36 --------- .../LazyLoadingMetadataFactoryTest.php | 79 ------------------ .../Component/Validator/ValidatorBuilder.php | 1 - 10 files changed, 8 insertions(+), 447 deletions(-) delete mode 100644 src/Symfony/Component/Validator/Mapping/Cache/CacheInterface.php delete mode 100644 src/Symfony/Component/Validator/Mapping/Cache/DoctrineCache.php delete mode 100644 src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php delete mode 100644 src/Symfony/Component/Validator/Tests/Mapping/Cache/AbstractCacheTest.php delete mode 100644 src/Symfony/Component/Validator/Tests/Mapping/Cache/DoctrineCacheTest.php delete mode 100644 src/Symfony/Component/Validator/Tests/Mapping/Cache/Psr6CacheTest.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml index f2d0e9e2a603b..070908f3db351 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml @@ -38,11 +38,6 @@ - - - The "%service_id%" service is deprecated since Symfony 4.4. Use validator.mapping.cache.adapter instead. - - %validator.mapping.cache.file% diff --git a/src/Symfony/Component/Validator/Mapping/Cache/CacheInterface.php b/src/Symfony/Component/Validator/Mapping/Cache/CacheInterface.php deleted file mode 100644 index 8821b9ddd13bd..0000000000000 --- a/src/Symfony/Component/Validator/Mapping/Cache/CacheInterface.php +++ /dev/null @@ -1,45 +0,0 @@ - - * - * 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 Symfony\Component\Validator\Mapping\ClassMetadata; - -@trigger_error(sprintf('The "%s" interface is deprecated since Symfony 4.4.', CacheInterface::class), E_USER_DEPRECATED); - -/** - * Persists ClassMetadata instances in a cache. - * - * @author Bernhard Schussek - * - * @deprecated since Symfony 4.4. - */ -interface CacheInterface -{ - /** - * Returns whether metadata for the given class exists in the cache. - */ - public function has(string $class); - - /** - * Returns the metadata for the given class from the cache. - * - * @param string $class Class Name - * - * @return ClassMetadata|false A ClassMetadata instance or false on miss - */ - public function read(string $class); - - /** - * Stores a class metadata in the cache. - */ - public function write(ClassMetadata $metadata); -} diff --git a/src/Symfony/Component/Validator/Mapping/Cache/DoctrineCache.php b/src/Symfony/Component/Validator/Mapping/Cache/DoctrineCache.php deleted file mode 100644 index f25e374ae9a86..0000000000000 --- a/src/Symfony/Component/Validator/Mapping/Cache/DoctrineCache.php +++ /dev/null @@ -1,63 +0,0 @@ - - * - * 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 Doctrine\Common\Cache\Cache; -use Symfony\Component\Validator\Mapping\ClassMetadata; - -@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.4.', DoctrineCache::class), E_USER_DEPRECATED); - -/** - * Adapts a Doctrine cache to a CacheInterface. - * - * @author Florian Voutzinos - * - * @deprecated since Symfony 4.4. - */ -final class DoctrineCache implements CacheInterface -{ - private $cache; - - public function __construct(Cache $cache) - { - $this->cache = $cache; - } - - public function setCache(Cache $cache) - { - $this->cache = $cache; - } - - /** - * {@inheritdoc} - */ - public function has(string $class): bool - { - return $this->cache->contains($class); - } - - /** - * {@inheritdoc} - */ - public function read(string $class) - { - return $this->cache->fetch($class); - } - - /** - * {@inheritdoc} - */ - public function write(ClassMetadata $metadata) - { - $this->cache->save($metadata->getClassName(), $metadata); - } -} diff --git a/src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php b/src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php deleted file mode 100644 index 4ac33f5267fc5..0000000000000 --- a/src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php +++ /dev/null @@ -1,80 +0,0 @@ - - * - * 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; - -@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.4.', Psr6Cache::class), E_USER_DEPRECATED); - -/** - * PSR-6 adapter. - * - * @author Kévin Dunglas - * - * @deprecated since Symfony 4.4. - */ -class Psr6Cache implements CacheInterface -{ - private $cacheItemPool; - - public function __construct(CacheItemPoolInterface $cacheItemPool) - { - $this->cacheItemPool = $cacheItemPool; - } - - /** - * {@inheritdoc} - */ - public function has(string $class) - { - return $this->cacheItemPool->hasItem($this->escapeClassName($class)); - } - - /** - * {@inheritdoc} - */ - public function read(string $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 dots in a class name. - */ - private function escapeClassName(string $class): string - { - if (false !== strpos($class, '@')) { - // anonymous class: replace all PSR6-reserved characters - return str_replace(["\0", '\\', '/', '@', ':', '{', '}', '(', ')'], '.', $class); - } - - return str_replace('\\', '.', $class); - } -} diff --git a/src/Symfony/Component/Validator/Mapping/Factory/LazyLoadingMetadataFactory.php b/src/Symfony/Component/Validator/Mapping/Factory/LazyLoadingMetadataFactory.php index 3b62b8661f150..219b8c5a11ade 100644 --- a/src/Symfony/Component/Validator/Mapping/Factory/LazyLoadingMetadataFactory.php +++ b/src/Symfony/Component/Validator/Mapping/Factory/LazyLoadingMetadataFactory.php @@ -13,7 +13,6 @@ use Psr\Cache\CacheItemPoolInterface; use Symfony\Component\Validator\Exception\NoSuchMetadataException; -use Symfony\Component\Validator\Mapping\Cache\CacheInterface; use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Mapping\Loader\LoaderInterface; @@ -49,20 +48,8 @@ class LazyLoadingMetadataFactory implements MetadataFactoryInterface */ protected $loadedClasses = []; - /** - * Creates a new metadata factory. - * - * @param CacheItemPoolInterface|null $cache The cache for persisting metadata - * between multiple PHP requests - */ - public function __construct(LoaderInterface $loader = null, $cache = null) + public function __construct(LoaderInterface $loader = null, CacheItemPoolInterface $cache = null) { - if ($cache instanceof CacheInterface) { - @trigger_error(sprintf('Passing a "%s" to "%s" is deprecated in Symfony 4.4 and will trigger a TypeError in 5.0. Please pass an implementation of "%s" instead.', \get_class($cache), __METHOD__, CacheItemPoolInterface::class), E_USER_DEPRECATED); - } elseif (!$cache instanceof CacheItemPoolInterface && null !== $cache) { - throw new \TypeError(sprintf('Expected an instance of %s, got %s.', CacheItemPoolInterface::class, \is_object($cache) ? \get_class($cache) : \gettype($cache))); - } - $this->loader = $loader; $this->cache = $cache; } @@ -98,24 +85,14 @@ public function getMetadataFor($value) throw new NoSuchMetadataException(sprintf('The class or interface "%s" does not exist.', $class)); } - $cacheItem = null; - if ($this->cache instanceof CacheInterface) { - if ($metadata = $this->cache->read($class)) { - // Include constraints from the parent class - $this->mergeConstraints($metadata); - - return $this->loadedClasses[$class] = $metadata; - } - } elseif (null !== $this->cache) { - $cacheItem = $this->cache->getItem($this->escapeClassName($class)); - if ($cacheItem->isHit()) { - $metadata = $cacheItem->get(); + $cacheItem = null === $this->cache ? null : $this->cache->getItem($this->escapeClassName($class)); + if ($cacheItem && $cacheItem->isHit()) { + $metadata = $cacheItem->get(); - // Include constraints from the parent class - $this->mergeConstraints($metadata); + // Include constraints from the parent class + $this->mergeConstraints($metadata); - return $this->loadedClasses[$class] = $metadata; - } + return $this->loadedClasses[$class] = $metadata; } $metadata = new ClassMetadata($class); @@ -124,9 +101,7 @@ public function getMetadataFor($value) $this->loader->loadClassMetadata($metadata); } - if ($this->cache instanceof CacheInterface) { - $this->cache->write($metadata); - } elseif (null !== $cacheItem) { + if (null !== $cacheItem) { $this->cache->save($cacheItem->set($metadata)); } diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Cache/AbstractCacheTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Cache/AbstractCacheTest.php deleted file mode 100644 index bd088e0f093d7..0000000000000 --- a/src/Symfony/Component/Validator/Tests/Mapping/Cache/AbstractCacheTest.php +++ /dev/null @@ -1,79 +0,0 @@ - - * - * 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 PHPUnit\Framework\TestCase; -use Symfony\Component\Validator\Mapping\Cache\CacheInterface; -use Symfony\Component\Validator\Mapping\ClassMetadata; - -abstract class AbstractCacheTest extends TestCase -{ - /** - * @var CacheInterface - */ - protected $cache; - - public function testWrite() - { - $meta = $this->getMockBuilder(ClassMetadata::class) - ->disableOriginalConstructor() - ->setMethods(['getClassName']) - ->getMock(); - - $meta->expects($this->once()) - ->method('getClassName') - ->willReturn('Foo\\Bar'); - - $this->cache->write($meta); - - $this->assertInstanceOf( - ClassMetadata::class, - $this->cache->read('Foo\\Bar'), - 'write() stores metadata' - ); - } - - public function testHas() - { - $meta = $this->getMockBuilder(ClassMetadata::class) - ->disableOriginalConstructor() - ->setMethods(['getClassName']) - ->getMock(); - - $meta->expects($this->once()) - ->method('getClassName') - ->willReturn('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(ClassMetadata::class) - ->disableOriginalConstructor() - ->setMethods(['getClassName']) - ->getMock(); - - $meta->expects($this->once()) - ->method('getClassName') - ->willReturn('Foo\\Bar'); - - $this->assertFalse($this->cache->read('Foo\\Bar'), 'read() returns false when there is no entry'); - - $this->cache->write($meta); - - $this->assertInstanceOf(ClassMetadata::class, $this->cache->read('Foo\\Bar'), 'read() returns metadata'); - } -} diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Cache/DoctrineCacheTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Cache/DoctrineCacheTest.php deleted file mode 100644 index e73b0d99668ec..0000000000000 --- a/src/Symfony/Component/Validator/Tests/Mapping/Cache/DoctrineCacheTest.php +++ /dev/null @@ -1,26 +0,0 @@ - - * - * 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 Doctrine\Common\Cache\ArrayCache; -use Symfony\Component\Validator\Mapping\Cache\DoctrineCache; - -/** - * @group legacy - */ -class DoctrineCacheTest extends AbstractCacheTest -{ - protected function setUp(): void - { - $this->cache = new DoctrineCache(new ArrayCache()); - } -} diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Cache/Psr6CacheTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Cache/Psr6CacheTest.php deleted file mode 100644 index bf9bf5d4478b2..0000000000000 --- a/src/Symfony/Component/Validator/Tests/Mapping/Cache/Psr6CacheTest.php +++ /dev/null @@ -1,36 +0,0 @@ - - * - * @group legacy - */ -class Psr6CacheTest extends AbstractCacheTest -{ - protected function setUp(): void - { - $this->cache = new Psr6Cache(new ArrayAdapter()); - } - - public function testNameCollision() - { - $metadata = new ClassMetadata('Foo\\Bar'); - - $this->cache->write($metadata); - $this->assertFalse($this->cache->has('Foo_Bar')); - } - - public function testNameWithInvalidChars() - { - $metadata = new ClassMetadata('class@anonymous/path/file'); - - $this->cache->write($metadata); - $this->assertTrue($this->cache->has('class@anonymous/path/file')); - } -} diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php index 6e9b7302db181..2cda5c6310b21 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php @@ -104,85 +104,6 @@ public function testCachedMetadata() $this->assertEquals($expectedConstraints, $metadata->getConstraints()); } - /** - * @group legacy - */ - public function testWriteMetadataToLegacyCache() - { - $cache = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Cache\CacheInterface')->getMock(); - $factory = new LazyLoadingMetadataFactory(new TestLoader(), $cache); - - $parentClassConstraints = [ - new ConstraintA(['groups' => ['Default', 'EntityParent']]), - new ConstraintA(['groups' => ['Default', 'EntityInterfaceA', 'EntityParent']]), - ]; - $interfaceAConstraints = [ - new ConstraintA(['groups' => ['Default', 'EntityInterfaceA']]), - ]; - - $cache->expects($this->never()) - ->method('has'); - $cache->expects($this->exactly(2)) - ->method('read') - ->withConsecutive( - [$this->equalTo(self::PARENT_CLASS)], - [$this->equalTo(self::INTERFACE_A_CLASS)] - ) - ->willReturn(false); - $cache->expects($this->exactly(2)) - ->method('write') - ->withConsecutive( - $this->callback(function ($metadata) use ($interfaceAConstraints) { - return $interfaceAConstraints == $metadata->getConstraints(); - }), - $this->callback(function ($metadata) use ($parentClassConstraints) { - return $parentClassConstraints == $metadata->getConstraints(); - }) - ); - - $metadata = $factory->getMetadataFor(self::PARENT_CLASS); - - $this->assertEquals(self::PARENT_CLASS, $metadata->getClassName()); - $this->assertEquals($parentClassConstraints, $metadata->getConstraints()); - } - - /** - * @group legacy - */ - public function testReadMetadataFromLegacyCache() - { - $loader = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Loader\LoaderInterface')->getMock(); - $cache = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Cache\CacheInterface')->getMock(); - $factory = new LazyLoadingMetadataFactory($loader, $cache); - - $metadata = new ClassMetadata(self::PARENT_CLASS); - $metadata->addConstraint(new ConstraintA()); - - $parentClass = self::PARENT_CLASS; - $interfaceClass = self::INTERFACE_A_CLASS; - - $loader->expects($this->never()) - ->method('loadClassMetadata'); - - $cache->expects($this->never()) - ->method('has'); - $cache->expects($this->exactly(2)) - ->method('read') - ->withConsecutive( - [self::PARENT_CLASS], - [self::INTERFACE_A_CLASS] - ) - ->willReturnCallback(function ($name) use ($metadata, $parentClass, $interfaceClass) { - if ($parentClass == $name) { - return $metadata; - } - - return new ClassMetadata($interfaceClass); - }); - - $this->assertEquals($metadata, $factory->getMetadataFor(self::PARENT_CLASS)); - } - public function testNonClassNameStringValues() { $this->expectException('Symfony\Component\Validator\Exception\NoSuchMetadataException'); diff --git a/src/Symfony/Component/Validator/ValidatorBuilder.php b/src/Symfony/Component/Validator/ValidatorBuilder.php index eb190ec234406..5599ff6be68e2 100644 --- a/src/Symfony/Component/Validator/ValidatorBuilder.php +++ b/src/Symfony/Component/Validator/ValidatorBuilder.php @@ -19,7 +19,6 @@ use Symfony\Component\Validator\Context\ExecutionContextFactory; use Symfony\Component\Validator\Exception\LogicException; use Symfony\Component\Validator\Exception\ValidatorException; -use Symfony\Component\Validator\Mapping\Cache\CacheInterface; use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory; use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface; use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;