How to split validation yaml files in Symfony 2.5?
Asked Answered
G

3

9

This code worked for Symfony 2.4.5 but it is not working for Symfony 2.5:

This is my HRAPI Extension class that was working in Symfony 2.4.5 but not in Symfony 2.5

<?php

namespace HardCoreMore\HRAPIBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
 * This is the class that loads and manages your bundle configuration
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
 */
class HRAPIExtension extends Extension
{
    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');

        // load validation configuration
        $yamlMappingFiles = $container->getParameter('validator.mapping.loader.yaml_files_loader.mapping_files');
        $yamlMappingFiles[] = __DIR__.'/../Resources/config/validation/site_user.yml';
        $yamlMappingFiles[] = __DIR__.'/../Resources/config/validation/human_resource.yml';
        $yamlMappingFiles[] = __DIR__.'/../Resources/config/validation/tag.yml';
        $yamlMappingFiles[] = __DIR__.'/../Resources/config/validation/company.yml';
        $yamlMappingFiles[] = __DIR__.'/../Resources/config/validation/programming_language.yml';
        $yamlMappingFiles[] = __DIR__.'/../Resources/config/validation/technology.yml';
        $yamlMappingFiles[] = __DIR__.'/../Resources/config/validation/language.yml';
        $yamlMappingFiles[] = __DIR__.'/../Resources/config/validation/management.yml';
        $yamlMappingFiles[] = __DIR__.'/../Resources/config/validation/calendar_entry.yml';
        $container->setParameter('validator.mapping.loader.yaml_files_loader.mapping_files', $yamlMappingFiles);
    }
}

It displays error:

ParameterNotFoundException: You have requested a non-existent parameter "validator.mapping.loader.yaml_files_loader.mapping_files".

Any ideas how to do it for Symfony 2.5?

Gharry answered 5/6, 2014 at 15:55 Comment(2)
Can you elaborate on "not working"?Celandine
I have updated the question. It says 'ParameterNotFoundException: You have requested a non-existent parameter "validator.mapping.loader.yaml_files_loader.mapping_files".'Gharry
P
20

Symfony 2.5 changed the way validation files were loaded. Here is how to do it now (using the Finder component to dynamically load):

Create a compiler pass:

namespace MyBundle\DependencyInjection\Compiler;

use Symfony\Component\Finder\Finder;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\Config\Resource\DirectoryResource;

class ValidatorPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $validatorBuilder = $container->getDefinition('validator.builder');
        $validatorFiles = array();
        $finder = new Finder();

        foreach ($finder->files()->in(__DIR__ . '/../../Resources/config/validation') as $file) {
            $validatorFiles[] = $file->getRealPath();
        }

        $validatorBuilder->addMethodCall('addYamlMappings', array($validatorFiles));

        // add resources to the container to refresh cache after updating a file
        $container->addResource(new DirectoryResource(__DIR__ . '/../../Resources/config/validation'));
    }
}

Then, add this compiler pass in your Bundle:

namespace MyBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use MyBundle\DependencyInjection\Compiler\ValidatorPass;

class MyBundle extends Bundle
{
    // . . . .

    public function build(ContainerBuilder $container)
    {
        parent::build($container);

        $container->addCompilerPass(new ValidatorPass());
    }

    // . . . .      
}
Promotive answered 13/6, 2014 at 17:6 Comment(0)
E
2

You should use ValidatorBuilder#addYamlMappings/ValidatorBuilder#addYamlMapping:

$builderDefinition = $container->getDefinition('validator.builder');
$builderDefinition->addMethodCall('addYamlMappings', array(
    array(
        __DIR__.'/../Resources/config/validation/site_user.yml';
        __DIR__.'/../Resources/config/validation/human_resource.yml';
        __DIR__.'/../Resources/config/validation/tag.yml';
        __DIR__.'/../Resources/config/validation/company.yml';
        __DIR__.'/../Resources/config/validation/programming_language.yml';
        __DIR__.'/../Resources/config/validation/technology.yml';
        __DIR__.'/../Resources/config/validation/language.yml';
        __DIR__.'/../Resources/config/validation/management.yml';
        __DIR__.'/../Resources/config/validation/calendar_entry.yml'
    )
));
Empiricism answered 5/6, 2014 at 16:29 Comment(5)
I tried but it says: "InvalidArgumentException: The service definition "validator.builder" does not exist."Gharry
@CaslavSabani is your bundle registered after the FrameworkBundle? Otherwise, try to use a compiler passEmpiricism
What do you mean "is my bundle registered after the FrameworkBundle"? I have registered my bundle in the AppKernel.Gharry
He means the order that you register them in AppKernel.Promotive
Yeah I assumed, but the order is not important nor it does anything.Gharry
J
2

This works out-of-the box with Symfony 2.7.

https://github.com/symfony/symfony/pull/13855

Jot answered 9/3, 2015 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.