Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

php - How to split validation yaml files in Symfony 2.5?

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 HardCoreMoreHRAPIBundleDependencyInjection;

use SymfonyComponentDependencyInjectionContainerBuilder;
use SymfonyComponentConfigFileLocator;
use SymfonyComponentHttpKernelDependencyInjectionExtension;
use SymfonyComponentDependencyInjectionLoader;

/**
 * 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 LoaderYamlFileLoader($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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 MyBundleDependencyInjectionCompiler;

use SymfonyComponentFinderFinder;
use SymfonyComponentDependencyInjectionContainerBuilder;
use SymfonyComponentDependencyInjectionCompilerCompilerPassInterface;
use SymfonyComponentConfigResourceDirectoryResource;

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 SymfonyComponentHttpKernelBundleBundle;
use SymfonyComponentDependencyInjectionContainerBuilder;
use MyBundleDependencyInjectionCompilerValidatorPass;

class MyBundle extends Bundle
{
    // . . . .

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

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

    // . . . .      
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...