-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FrameworkBundle] Introduce a cache warmer for Validator based on Php…
…ArrayAdapter
- Loading branch information
Showing
18 changed files
with
349 additions
and
76 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?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\Bundle\FrameworkBundle\CacheWarmer; | ||
|
||
use Psr\Cache\CacheItemPoolInterface; | ||
use Symfony\Component\Cache\Adapter\AdapterInterface; | ||
use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
use Symfony\Component\Cache\Adapter\PhpArrayAdapter; | ||
use Symfony\Component\Cache\Adapter\ProxyAdapter; | ||
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; | ||
use Symfony\Component\Validator\Mapping\Cache\Psr6Cache; | ||
use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory; | ||
use Symfony\Component\Validator\Mapping\Loader\LoaderChain; | ||
use Symfony\Component\Validator\Mapping\Loader\LoaderInterface; | ||
use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader; | ||
use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader; | ||
use Symfony\Component\Validator\ValidatorBuilderInterface; | ||
|
||
/** | ||
* Warms up XML and YAML validator metadata. | ||
* | ||
* @author Titouan Galopin <galopintitouan@gmail.com> | ||
*/ | ||
class ValidatorCacheWarmer implements CacheWarmerInterface | ||
{ | ||
private $validatorBuilder; | ||
private $phpArrayFile; | ||
private $fallbackPool; | ||
|
||
/** | ||
* @param ValidatorBuilderInterface $validatorBuilder | ||
* @param string $phpArrayFile The PHP file where metadata are cached. | ||
* @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered metadata are cached. | ||
*/ | ||
public function __construct(ValidatorBuilderInterface $validatorBuilder, $phpArrayFile, CacheItemPoolInterface $fallbackPool) | ||
{ | ||
$this->validatorBuilder = $validatorBuilder; | ||
$this->phpArrayFile = $phpArrayFile; | ||
if (!$fallbackPool instanceof AdapterInterface) { | ||
$fallbackPool = new ProxyAdapter($fallbackPool); | ||
} | ||
$this->fallbackPool = $fallbackPool; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function warmUp($cacheDir) | ||
{ | ||
if (!method_exists($this->validatorBuilder, 'getLoaders')) { | ||
return; | ||
} | ||
|
||
$adapter = new PhpArrayAdapter($this->phpArrayFile, $this->fallbackPool); | ||
$arrayPool = new ArrayAdapter(0, false); | ||
|
||
$loaders = $this->validatorBuilder->getLoaders(); | ||
$metadataFactory = new LazyLoadingMetadataFactory(new LoaderChain($loaders), new Psr6Cache($arrayPool)); | ||
|
||
foreach ($this->extractSupportedLoaders($loaders) as $loader) { | ||
foreach ($loader->getMappedClasses() as $mappedClass) { | ||
$metadataFactory->getMetadataFor($mappedClass); | ||
} | ||
} | ||
|
||
$values = $arrayPool->getValues(); | ||
$adapter->warmUp($values); | ||
|
||
foreach ($values as $k => $v) { | ||
$item = $this->fallbackPool->getItem($k); | ||
$this->fallbackPool->saveDeferred($item->set($v)); | ||
} | ||
$this->fallbackPool->commit(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isOptional() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @param LoaderInterface[] $loaders | ||
* | ||
* @return XmlFileLoader[]|YamlFileLoader[] | ||
*/ | ||
private function extractSupportedLoaders(array $loaders) | ||
{ | ||
$supportedLoaders = array(); | ||
|
||
foreach ($loaders as $loader) { | ||
if ($loader instanceof XmlFileLoader || $loader instanceof YamlFileLoader) { | ||
$supportedLoaders[] = $loader; | ||
} elseif ($loader instanceof LoaderChain) { | ||
$supportedLoaders = array_merge($supportedLoaders, $this->extractSupportedLoaders($loader->getDelegatedLoaders())); | ||
} | ||
} | ||
|
||
return $supportedLoaders; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/ValidatorCacheWarmerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?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\Bundle\FrameworkBundle\Tests\CacheWarmer; | ||
|
||
use Symfony\Bundle\FrameworkBundle\CacheWarmer\ValidatorCacheWarmer; | ||
use Symfony\Bundle\FrameworkBundle\Tests\TestCase; | ||
use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
use Symfony\Component\Validator\ValidatorBuilder; | ||
|
||
class ValidatorCacheWarmerTest extends TestCase | ||
{ | ||
public function testWarmUp() | ||
{ | ||
$validatorBuilder = new ValidatorBuilder(); | ||
$validatorBuilder->addXmlMapping(__DIR__.'/../Fixtures/Validation/Resources/person.xml'); | ||
$validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/author.yml'); | ||
$validatorBuilder->addMethodMapping('loadValidatorMetadata'); | ||
$validatorBuilder->enableAnnotationMapping(); | ||
|
||
$file = sys_get_temp_dir().'/cache-validator.php'; | ||
@unlink($file); | ||
|
||
$fallbackPool = new ArrayAdapter(); | ||
|
||
$warmer = new ValidatorCacheWarmer($validatorBuilder, $file, $fallbackPool); | ||
$warmer->warmUp(dirname($file)); | ||
|
||
$this->assertFileExists($file); | ||
|
||
$values = require $file; | ||
|
||
$this->assertInternalType('array', $values); | ||
$this->assertCount(2, $values); | ||
$this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Person', $values); | ||
$this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Person', $values); | ||
|
||
$values = $fallbackPool->getValues(); | ||
|
||
$this->assertInternalType('array', $values); | ||
$this->assertCount(2, $values); | ||
$this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Person', $values); | ||
$this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Person', $values); | ||
} | ||
|
||
public function testWarmUpWithoutLoader() | ||
{ | ||
$validatorBuilder = new ValidatorBuilder(); | ||
|
||
$file = sys_get_temp_dir().'/cache-validator.php'; | ||
@unlink($file); | ||
|
||
$fallbackPool = new ArrayAdapter(); | ||
|
||
$warmer = new ValidatorCacheWarmer($validatorBuilder, $file, $fallbackPool); | ||
$warmer->warmUp(dirname($file)); | ||
|
||
$this->assertFileExists($file); | ||
|
||
$values = require $file; | ||
|
||
$this->assertInternalType('array', $values); | ||
$this->assertCount(0, $values); | ||
|
||
$values = $fallbackPool->getValues(); | ||
|
||
$this->assertInternalType('array', $values); | ||
$this->assertCount(0, $values); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Validation/Author.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation; | ||
|
||
class Author | ||
{ | ||
public $gender; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Validation/Person.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation; | ||
|
||
class Person | ||
{ | ||
public $gender; | ||
} |
4 changes: 4 additions & 0 deletions
4
src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Validation/Resources/author.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation\Author: | ||
properties: | ||
gender: | ||
- Choice: { choices: [male, female, other], message: Choose a valid gender. } |
18 changes: 18 additions & 0 deletions
18
src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Validation/Resources/person.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> | ||
|
||
<class name="Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation\Person"> | ||
<property name="gender"> | ||
<constraint name="Choice"> | ||
<option name="choices"> | ||
<value>male</value> | ||
<value>female</value> | ||
<value>other</value> | ||
</option> | ||
<option name="message">Choose a valid gender.</option> | ||
</constraint> | ||
</property> | ||
</class> | ||
</constraint-mapping> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.