Skip to content

Commit f15405a

Browse files
author
David Wolter
committed
[FrameworkBundle] Configurable paths for validation files
1 parent e8080f4 commit f15405a

File tree

13 files changed

+114
-22
lines changed

13 files changed

+114
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CHANGELOG
1212
is disabled.
1313
* Added `GlobalVariables::getToken()`
1414
* Deprecated `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass`. Use `Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass` instead.
15+
* Added configurable paths for validation files
1516

1617
3.2.0
1718
-----

DependencyInjection/Configuration.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,15 @@ private function addValidationSection(ArrayNodeDefinition $rootNode)
635635
->end()
636636
->scalarNode('translation_domain')->defaultValue('validators')->end()
637637
->booleanNode('strict_email')->defaultFalse()->end()
638+
->arrayNode('mapping')
639+
->addDefaultsIfNotSet()
640+
->fixXmlConfig('path')
641+
->children()
642+
->arrayNode('paths')
643+
->prototype('scalar')->end()
644+
->end()
645+
->end()
646+
->end()
638647
->end()
639648
->end()
640649
->end()

DependencyInjection/FrameworkExtension.php

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -947,13 +947,16 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
947947

948948
$container->setParameter('validator.translation_domain', $config['translation_domain']);
949949

950-
list($xmlMappings, $yamlMappings) = $this->getValidatorMappingFiles($container);
951-
if (count($xmlMappings) > 0) {
952-
$validatorBuilder->addMethodCall('addXmlMappings', array($xmlMappings));
950+
$files = array('xml' => array(), 'yml' => array());
951+
$this->getValidatorMappingFiles($container, $files);
952+
$this->getValidatorMappingFilesFromConfig($config, $files);
953+
954+
if (!empty($files['xml'])) {
955+
$validatorBuilder->addMethodCall('addXmlMappings', array($files['xml']));
953956
}
954957

955-
if (count($yamlMappings) > 0) {
956-
$validatorBuilder->addMethodCall('addYamlMappings', array($yamlMappings));
958+
if (!empty($files['yml'])) {
959+
$validatorBuilder->addMethodCall('addYamlMappings', array($files['yml']));
957960
}
958961

959962
$definition = $container->findDefinition('validator.email');
@@ -987,41 +990,58 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
987990
}
988991
}
989992

990-
private function getValidatorMappingFiles(ContainerBuilder $container)
993+
private function getValidatorMappingFiles(ContainerBuilder $container, array &$files)
991994
{
992-
$files = array(array(), array());
993-
994995
if (interface_exists('Symfony\Component\Form\FormInterface')) {
995996
$reflClass = new \ReflectionClass('Symfony\Component\Form\FormInterface');
996-
$files[0][] = dirname($reflClass->getFileName()).'/Resources/config/validation.xml';
997-
$container->addResource(new FileResource($files[0][0]));
997+
$files['xml'][] = $file = dirname($reflClass->getFileName()).'/Resources/config/validation.xml';
998+
$container->addResource(new FileResource($file));
998999
}
9991000

10001001
foreach ($container->getParameter('kernel.bundles_metadata') as $bundle) {
10011002
$dirname = $bundle['path'];
1002-
if (is_file($file = $dirname.'/Resources/config/validation.xml')) {
1003-
$files[0][] = $file;
1003+
1004+
if (is_file($file = $dirname.'/Resources/config/validation.yml')) {
1005+
$files['yml'][] = $file;
10041006
$container->addResource(new FileResource($file));
10051007
}
10061008

1007-
if (is_file($file = $dirname.'/Resources/config/validation.yml')) {
1008-
$files[1][] = $file;
1009+
if (is_file($file = $dirname.'/Resources/config/validation.xml')) {
1010+
$files['xml'][] = $file;
10091011
$container->addResource(new FileResource($file));
10101012
}
10111013

10121014
if (is_dir($dir = $dirname.'/Resources/config/validation')) {
1013-
foreach (Finder::create()->followLinks()->files()->in($dir)->name('*.xml') as $file) {
1014-
$files[0][] = $file->getPathname();
1015-
}
1016-
foreach (Finder::create()->followLinks()->files()->in($dir)->name('*.yml') as $file) {
1017-
$files[1][] = $file->getPathname();
1018-
}
1019-
1015+
$this->getValidatorMappingFilesFromDir($dir, $files);
10201016
$container->addResource(new DirectoryResource($dir));
10211017
}
10221018
}
1019+
}
1020+
1021+
private function getValidatorMappingFilesFromDir($dir, array &$files)
1022+
{
1023+
foreach (Finder::create()->followLinks()->files()->in($dir)->name('/\.(xml|ya?ml)$/') as $file) {
1024+
$extension = $file->getExtension();
1025+
$files['yaml' === $extension ? 'yml' : $extension][] = $file->getRealpath();
1026+
}
1027+
}
10231028

1024-
return $files;
1029+
private function getValidatorMappingFilesFromConfig(array $config, array &$files)
1030+
{
1031+
foreach ($config['mapping']['paths'] as $path) {
1032+
if (is_dir($path)) {
1033+
$this->getValidatorMappingFilesFromDir($path, $files);
1034+
} elseif (is_file($path)) {
1035+
if (preg_match('/\.(xml|ya?ml)$/', $path, $matches)) {
1036+
$extension = $matches[1];
1037+
$files['yaml' === $extension ? 'yml' : $extension][] = $path;
1038+
} else {
1039+
throw new \RuntimeException(sprintf('Unsupported mapping type in "%s", supported types are XML & Yaml.', $path));
1040+
}
1041+
} else {
1042+
throw new \RuntimeException(sprintf('Could not open file or directory "%s".', $path));
1043+
}
1044+
}
10251045
}
10261046

10271047
private function registerAnnotationsConfiguration(array $config, ContainerBuilder $container, $loader)

Resources/config/schema/symfony-1.0.xsd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@
176176
<xsd:complexType name="validation">
177177
<xsd:choice minOccurs="0" maxOccurs="unbounded">
178178
<xsd:element name="static-method" type="xsd:string" />
179+
<xsd:element name="mapping" type="validation_mapping" />
179180
</xsd:choice>
180181

181182
<xsd:attribute name="enabled" type="xsd:boolean" />
@@ -184,6 +185,12 @@
184185
<xsd:attribute name="static-method" type="xsd:boolean" />
185186
</xsd:complexType>
186187

188+
<xsd:complexType name="validation_mapping">
189+
<xsd:sequence>
190+
<xsd:element name="path" type="xsd:string" minOccurs="1" maxOccurs="unbounded" />
191+
</xsd:sequence>
192+
</xsd:complexType>
193+
187194
<xsd:complexType name="annotations">
188195
<xsd:attribute name="cache" type="xsd:string" />
189196
<xsd:attribute name="debug" type="xsd:string" />

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ protected static function getBundleDefaultConfig()
212212
'static_method' => array('loadValidatorMetadata'),
213213
'translation_domain' => 'validators',
214214
'strict_email' => false,
215+
'mapping' => array(
216+
'paths' => array(),
217+
),
215218
),
216219
'annotations' => array(
217220
'cache' => 'php_array',

Tests/DependencyInjection/Fixtures/TestBundle/Resources/config/validation_mapping/files/foo.xml

Whitespace-only changes.

Tests/DependencyInjection/Fixtures/TestBundle/Resources/config/validation_mapping/files/foo.yml

Whitespace-only changes.

Tests/DependencyInjection/Fixtures/TestBundle/Resources/config/validation_mapping/validation.yaml

Whitespace-only changes.

Tests/DependencyInjection/Fixtures/TestBundle/Resources/config/validation_mapping/validation.yml

Whitespace-only changes.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', array(
4+
'validation' => array(
5+
'mapping' => array(
6+
'paths' => array(
7+
'%kernel.root_dir%/Fixtures/TestBundle/Resources/config/validation_mapping/files',
8+
'%kernel.root_dir%/Fixtures/TestBundle/Resources/config/validation_mapping/validation.yml',
9+
'%kernel.root_dir%/Fixtures/TestBundle/Resources/config/validation_mapping/validation.yaml',
10+
),
11+
),
12+
),
13+
));
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:framework="http://symfony.com/schema/dic/symfony">
6+
7+
<framework:config>
8+
<framework:validation>
9+
<framework:mapping>
10+
<framework:path>%kernel.root_dir%/Fixtures/TestBundle/Resources/config/validation_mapping/files</framework:path>
11+
<framework:path>%kernel.root_dir%/Fixtures/TestBundle/Resources/config/validation_mapping/validation.yml</framework:path>
12+
<framework:path>%kernel.root_dir%/Fixtures/TestBundle/Resources/config/validation_mapping/validation.yaml</framework:path>
13+
</framework:mapping>
14+
</framework:validation>
15+
</framework:config>
16+
</container>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
framework:
2+
validation:
3+
mapping:
4+
paths:
5+
- "%kernel.root_dir%/Fixtures/TestBundle/Resources/config/validation_mapping/files"
6+
- "%kernel.root_dir%/Fixtures/TestBundle/Resources/config/validation_mapping/validation.yml"
7+
- "%kernel.root_dir%/Fixtures/TestBundle/Resources/config/validation_mapping/validation.yaml"

Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,22 @@ public function testValidationNoStaticMethod()
584584
// no cache, no annotations, no static methods
585585
}
586586

587+
public function testValidationMapping()
588+
{
589+
$container = $this->createContainerFromFile('validation_mapping');
590+
591+
$calls = $container->getDefinition('validator.builder')->getMethodCalls();
592+
593+
$this->assertSame('addXmlMappings', $calls[3][0]);
594+
$this->assertCount(2, $calls[3][1][0]);
595+
596+
$this->assertSame('addYamlMappings', $calls[4][0]);
597+
$this->assertCount(3, $calls[4][1][0]);
598+
$this->assertContains('foo.yml', $calls[4][1][0][0]);
599+
$this->assertContains('validation.yml', $calls[4][1][0][1]);
600+
$this->assertContains('validation.yaml', $calls[4][1][0][2]);
601+
}
602+
587603
public function testFormsCanBeEnabledWithoutCsrfProtection()
588604
{
589605
$container = $this->createContainerFromFile('form_no_csrf');

0 commit comments

Comments
 (0)