gpt4 book ai didi

php - 如何在 Symfony 2.5 中拆分验证 yaml 文件?

转载 作者:可可西里 更新时间:2023-11-01 12:42:12 26 4
gpt4 key购买 nike

此代码适用于 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: You have requested a non-existent parameter "validator.mapping.loader.yaml_files_loader.mapping_files".

对于 Symfony 2.5 有什么想法吗?

最佳答案

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

然后,将此编译器传递添加到您的 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());
}

// . . . .
}

关于php - 如何在 Symfony 2.5 中拆分验证 yaml 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24064813/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com