我尝试通过编译器遍历覆盖服务列表上的参数。
这对于我所有的服务执行都很好。该服务使用服务继承:
pim_connector.array_converter.flat_to_standard.product.value_converter.abstract:
class: '%pim_connector.array_converter.flat_to_standard.product.value_converter.abstract.class%'
arguments:
- '@pim_connector.array_converter.flat_to_standard.product.field_splitter'
pim_connector.array_converter.flat_to_standard.product.value_converter.simpleselect:
class: '%pim_connector.array_converter.flat_to_standard.product.value_converter.simpleselect.class%'
parent: pim_connector.array_converter.flat_to_standard.product.value_converter.abstract
arguments:
- ['pim_catalog_simpleselect', 'pim_reference_data_simpleselect']
tags:
- { name: 'pim_connector.array_converter.flat_to_standard.product.value_converter' }
在我的编译器中,如果我这样转储参数,则:
class MyCompilerPass implements CompilerPassInterface
{
/**
* @param ContainerBuilder $container
*/
public function process(ContainerBuilder $container)
{
$service = $container->getDefinition('pim_connector.array_converter.flat_to_standard.product.value_converter.simpleselect');
var_dump($service->getArguments());exit;
}
}
我有这个:
/srv/pim/src/MyNamespace/PimCatalogBundle/DependencyInjection/CompilerPass/MyCompilerPass.php:31:
array (size=1)
0 =>
array (size=2)
0 => string 'pim_catalog_simpleselect' (length=24)
1 => string 'pim_reference_data_simpleselect' (length=31)
所以我尝试将参数替换为索引0,但出现此错误:
request.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalThrowableError: "Type error: Argument 1 passed to Pim\Component\Connector\ArrayConverter\FlatToStandard\Product\ValueConverter\SimpleSelectConverter::__construct() must be an instance of Pim\Component\Connector\ArrayConverter\FlatToStandard\Product\FieldSplitter, array given
如果我尝试使用索引1,则有一个:
Uncaught Symfony\Component\DependencyInjection\Exception\OutOfBoundsException: The argument "1" doesn't exist.
您对问题有任何了解吗?
服务pim_connector.array_converter.flat_to_standard.product.value_converter.simpleselect
的类别是这样的:
namespace Pim\Component\Connector\ArrayConverter\FlatToStandard\Product\ValueConverter;
use Pim\Component\Connector\ArrayConverter\FlatToStandard\Product\FieldSplitter;
class SimpleSelectConverter extends AbstractValueConverter
{
/**
* @param FieldSplitter $fieldSplitter
* @param array $supportedFieldType
*/
public function __construct(FieldSplitter $fieldSplitter, array $supportedFieldType)
{
parent::__construct($fieldSplitter);
$this->supportedFieldType = $supportedFieldType;
}
/**
* {@inheritdoc}
*/
public function convert(array $attributeFieldInfo, $value)
{
if ('' === $value) {
$value = null;
}
return [$attributeFieldInfo['attribute']->getCode() => [[
'locale' => $attributeFieldInfo['locale_code'],
'scope' => $attributeFieldInfo['scope_code'],
'data' => $value,
]]];
}
}
我的编译器密码在这里:
class MyCompilerPass implements CompilerPassInterface
{
private const SERVICES = [
'pim_catalog.validator.constraint.attribute_type_for_option' => 0,
'pim_catalog.comparator.attribute.option' => 0,
'pim_api.controller.attribute_option' => 12,
'pim_catalog.query.elasticsearch.filter.option' => 2,
'pim_catalog.query.elasticsearch.sorter.option' => 1,
'pim_connector.array_converter.flat_to_standard.product.value_converter.simpleselect' => 1,
'pim_connector.array_converter.standard_to_flat.product.value_converter.simpleselect' => 1
];
/**
* @param ContainerBuilder $container
*/
public function process(ContainerBuilder $container)
{
foreach (self::SERVICES as $id => $index) {
$service = $container->getDefinition($id);
$service->replaceArgument($index, array_merge(
$service->getArgument($index),
['my_new_type']
));
}
}
}
答案 0 :(得分:0)
尝试一下
pim_connector.array_converter.flat_to_standard.product.value_converter.simpleselect:
class: '%pim_connector.array_converter.flat_to_standard.product.value_converter.simpleselect.class%'
parent: pim_connector.array_converter.flat_to_standard.product.value_converter.abstract
arguments:
- '@pim_connector.array_converter.flat_to_standard.product.field_splitter'
- ['pim_catalog_simpleselect', 'pim_reference_data_simpleselect']
tags:
- { name: 'pim_connector.array_converter.flat_to_standard.product.value_converter' }
由于您的类SimpleSelectConverter需要将服务作为第一个参数,将数组作为第二个参数
答案 1 :(得分:0)
我在github上创建了一个问题:[DI] Compiler pass : can't replace a parameter with inheritance of services
编辑:所以解决方案是在编译器遍历上添加优先级:
like_status