-
-
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.
feature #22081 [FrameworkBundle][Validator] Move Validator passes to …
…the component (chalasr) This PR was merged into the 3.3-dev branch. Discussion ---------- [FrameworkBundle][Validator] Move Validator passes to the component | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Commits ------- 0b741da Move AddValidatorInitializersrPass & AddConstraintValidatorsPass to the Validator
- Loading branch information
Showing
14 changed files
with
269 additions
and
56 deletions.
There are no files selected for viewing
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
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
62 changes: 62 additions & 0 deletions
62
src/Symfony/Component/Validator/DependencyInjection/AddConstraintValidatorsPass.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,62 @@ | ||
<?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\DependencyInjection; | ||
|
||
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\DependencyInjection\ServiceLocator; | ||
|
||
/** | ||
* @author Johannes M. Schmitt <schmittjoh@gmail.com> | ||
* @author Robin Chalas <robin.chalas@gmail.com> | ||
*/ | ||
class AddConstraintValidatorsPass implements CompilerPassInterface | ||
{ | ||
private $validatorFactoryServiceId; | ||
private $constraintValidatorTag; | ||
|
||
public function __construct($validatorFactoryServiceId = 'validator.validator_factory', $constraintValidatorTag = 'validator.constraint_validator') | ||
{ | ||
$this->validatorFactoryServiceId = $validatorFactoryServiceId; | ||
$this->constraintValidatorTag = $constraintValidatorTag; | ||
} | ||
|
||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasDefinition($this->validatorFactoryServiceId)) { | ||
return; | ||
} | ||
|
||
$validators = array(); | ||
foreach ($container->findTaggedServiceIds($this->constraintValidatorTag) as $id => $attributes) { | ||
$definition = $container->getDefinition($id); | ||
|
||
if ($definition->isAbstract()) { | ||
continue; | ||
} | ||
|
||
if (isset($attributes[0]['alias'])) { | ||
$validators[$attributes[0]['alias']] = new ServiceClosureArgument(new Reference($id)); | ||
} | ||
|
||
$validators[$definition->getClass()] = new ServiceClosureArgument(new Reference($id)); | ||
} | ||
|
||
$container | ||
->getDefinition('validator.validator_factory') | ||
->replaceArgument(0, (new Definition(ServiceLocator::class, array($validators)))->addTag('container.service_locator')) | ||
; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/Symfony/Component/Validator/DependencyInjection/AddValidatorInitializersPass.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,50 @@ | ||
<?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\DependencyInjection; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
|
||
/** | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
* @author Robin Chalas <robin.chalas@gmail.com> | ||
*/ | ||
class AddValidatorInitializersPass implements CompilerPassInterface | ||
{ | ||
private $builderService; | ||
private $initializerTag; | ||
|
||
public function __construct($builderService = 'validator.builder', $initializerTag = 'validator.initializer') | ||
{ | ||
$this->builderService = $builderService; | ||
$this->initializerTag = $initializerTag; | ||
} | ||
|
||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasDefinition($this->builderService)) { | ||
return; | ||
} | ||
|
||
$initializers = array(); | ||
foreach ($container->findTaggedServiceIds($this->initializerTag) as $id => $attributes) { | ||
if ($container->getDefinition($id)->isAbstract()) { | ||
continue; | ||
} | ||
|
||
$initializers[] = new Reference($id); | ||
} | ||
|
||
$container->getDefinition($this->builderService)->addMethodCall('addObjectInitializers', array($initializers)); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...Symfony/Component/Validator/Tests/DependencyInjection/AddConstraintValidatorsPassTest.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,64 @@ | ||
<?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\DependencyInjection; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass; | ||
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\DependencyInjection\ServiceLocator; | ||
|
||
class AddConstraintValidatorsPassTest extends TestCase | ||
{ | ||
public function testThatConstraintValidatorServicesAreProcessed() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$validatorFactory = $container->register('validator.validator_factory') | ||
->addArgument(array()); | ||
|
||
$container->register('my_constraint_validator_service1', Validator1::class) | ||
->addTag('validator.constraint_validator', array('alias' => 'my_constraint_validator_alias1')); | ||
$container->register('my_constraint_validator_service2', Validator2::class) | ||
->addTag('validator.constraint_validator'); | ||
$container->register('my_abstract_constraint_validator') | ||
->setAbstract(true) | ||
->addTag('validator.constraint_validator'); | ||
|
||
$addConstraintValidatorsPass = new AddConstraintValidatorsPass(); | ||
$addConstraintValidatorsPass->process($container); | ||
|
||
$this->assertEquals((new Definition(ServiceLocator::class, array(array( | ||
Validator1::class => new ServiceClosureArgument(new Reference('my_constraint_validator_service1')), | ||
'my_constraint_validator_alias1' => new ServiceClosureArgument(new Reference('my_constraint_validator_service1')), | ||
Validator2::class => new ServiceClosureArgument(new Reference('my_constraint_validator_service2')), | ||
))))->addTag('container.service_locator'), $validatorFactory->getArgument(0)); | ||
} | ||
|
||
public function testThatCompilerPassIsIgnoredIfThereIsNoConstraintValidatorFactoryDefinition() | ||
{ | ||
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock(); | ||
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock(); | ||
|
||
$container->expects($this->never())->method('findTaggedServiceIds'); | ||
$container->expects($this->never())->method('getDefinition'); | ||
$container->expects($this->atLeastOnce()) | ||
->method('hasDefinition') | ||
->with('validator.validator_factory') | ||
->will($this->returnValue(false)); | ||
$definition->expects($this->never())->method('replaceArgument'); | ||
|
||
$addConstraintValidatorsPass = new AddConstraintValidatorsPass(); | ||
$addConstraintValidatorsPass->process($container); | ||
} | ||
} |
Oops, something went wrong.