8

このコードは Symfony 2.4.5 では機能しましたが、Symfony 2.5 では機能しません:

これは、Symfony 2.4.5 では機能していたが Symfony 2.5 では機能していなかった私の HRAPI 拡張クラスです。

<?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);
    }
}

エラーが表示されます:

ParameterNotFoundException: 存在しないパラメーター「validator.mapping.loader.yaml_files_loader.mapping_files」を要求しました。

Symfony 2.5でそれを行う方法はありますか?

4

3 に答える 3

20

Symfony 2.5 では、検証ファイルのロード方法が変更されました。これを行う方法は次のとおりです (Finder コンポーネントを使用して動的にロードします)。

コンパイラ パスを作成します。

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'));
    }
}

次に、このコンパイラ パスをバンドルに追加します。

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());
    }

    // . . . .      
}
于 2014-06-13T17:06:27.120 に答える
2

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'
    )
));
于 2014-06-05T16:29:02.500 に答える
2

これは、Symfony 2.7 ですぐに使用できます。

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

于 2015-03-09T11:54:20.440 に答える