bug #31201 [Form] resolve class name parameters (xabbuh)
This PR was merged into the 4.2 branch.
Discussion
----------
[Form] resolve class name parameters
| Q | A
| ------------- | ---
| Branch? | 4.2
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #31052
| License | MIT
| Doc PR |
Commits
-------
5235be4aa3
resolve class name parameters
This commit is contained in:
commit
571647f7bf
@ -92,18 +92,20 @@ class FormPass implements CompilerPassInterface
|
|||||||
$serviceDefinition = $container->getDefinition($serviceId);
|
$serviceDefinition = $container->getDefinition($serviceId);
|
||||||
|
|
||||||
$tag = $serviceDefinition->getTag($this->formTypeExtensionTag);
|
$tag = $serviceDefinition->getTag($this->formTypeExtensionTag);
|
||||||
|
$typeExtensionClass = $container->getParameterBag()->resolveValue($serviceDefinition->getClass());
|
||||||
|
|
||||||
if (isset($tag[0]['extended_type'])) {
|
if (isset($tag[0]['extended_type'])) {
|
||||||
if (!method_exists($serviceDefinition->getClass(), 'getExtendedTypes')) {
|
if (!method_exists($typeExtensionClass, 'getExtendedTypes')) {
|
||||||
@trigger_error(sprintf('Not implementing the static getExtendedTypes() method in %s when implementing the %s is deprecated since Symfony 4.2. The method will be added to the interface in 5.0.', $serviceDefinition->getClass(), FormTypeExtensionInterface::class), E_USER_DEPRECATED);
|
@trigger_error(sprintf('Not implementing the static getExtendedTypes() method in %s when implementing the %s is deprecated since Symfony 4.2. The method will be added to the interface in 5.0.', $typeExtensionClass, FormTypeExtensionInterface::class), E_USER_DEPRECATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
$typeExtensions[$tag[0]['extended_type']][] = new Reference($serviceId);
|
$typeExtensions[$tag[0]['extended_type']][] = new Reference($serviceId);
|
||||||
$typeExtensionsClasses[] = $serviceDefinition->getClass();
|
$typeExtensionsClasses[] = $typeExtensionClass;
|
||||||
} elseif (method_exists($serviceDefinition->getClass(), 'getExtendedTypes')) {
|
} elseif (method_exists($typeExtensionClass, 'getExtendedTypes')) {
|
||||||
$extendsTypes = false;
|
$extendsTypes = false;
|
||||||
|
|
||||||
$typeExtensionsClasses[] = $serviceDefinition->getClass();
|
$typeExtensionsClasses[] = $typeExtensionClass;
|
||||||
foreach ($serviceDefinition->getClass()::getExtendedTypes() as $extendedType) {
|
foreach ($typeExtensionClass::getExtendedTypes() as $extendedType) {
|
||||||
$typeExtensions[$extendedType][] = new Reference($serviceId);
|
$typeExtensions[$extendedType][] = new Reference($serviceId);
|
||||||
$extendsTypes = true;
|
$extendsTypes = true;
|
||||||
}
|
}
|
||||||
@ -112,7 +114,7 @@ class FormPass implements CompilerPassInterface
|
|||||||
throw new InvalidArgumentException(sprintf('The getExtendedTypes() method for service "%s" does not return any extended types.', $serviceId));
|
throw new InvalidArgumentException(sprintf('The getExtendedTypes() method for service "%s" does not return any extended types.', $serviceId));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new InvalidArgumentException(sprintf('"%s" tagged services have to implement the static getExtendedTypes() method. The class for service "%s" does not implement it.', $this->formTypeExtensionTag, $serviceId));
|
throw new InvalidArgumentException(sprintf('"%s" tagged services have to implement the static getExtendedTypes() method. Class "%s" for service "%s" does not implement it.', $this->formTypeExtensionTag, $typeExtensionClass, $serviceId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,10 +97,14 @@ class FormPassTest extends TestCase
|
|||||||
/**
|
/**
|
||||||
* @dataProvider addTaggedTypeExtensionsDataProvider
|
* @dataProvider addTaggedTypeExtensionsDataProvider
|
||||||
*/
|
*/
|
||||||
public function testAddTaggedTypeExtensions(array $extensions, array $expectedRegisteredExtensions)
|
public function testAddTaggedTypeExtensions(array $extensions, array $expectedRegisteredExtensions, array $parameters = [])
|
||||||
{
|
{
|
||||||
$container = $this->createContainerBuilder();
|
$container = $this->createContainerBuilder();
|
||||||
|
|
||||||
|
foreach ($parameters as $name => $value) {
|
||||||
|
$container->setParameter($name, $value);
|
||||||
|
}
|
||||||
|
|
||||||
$container->setDefinition('form.extension', $this->createExtensionDefinition());
|
$container->setDefinition('form.extension', $this->createExtensionDefinition());
|
||||||
|
|
||||||
foreach ($extensions as $serviceId => $config) {
|
foreach ($extensions as $serviceId => $config) {
|
||||||
@ -191,6 +195,27 @@ class FormPassTest extends TestCase
|
|||||||
]),
|
]),
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'my.type_extension1' => [
|
||||||
|
'class' => '%type1_extension_class%',
|
||||||
|
'tag' => ['extended_type' => 'type1'],
|
||||||
|
],
|
||||||
|
'my.type_extension2' => [
|
||||||
|
'class' => '%type1_extension_class%',
|
||||||
|
'tag' => [],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'type1' => new IteratorArgument([
|
||||||
|
new Reference('my.type_extension1'),
|
||||||
|
new Reference('my.type_extension2'),
|
||||||
|
]),
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'type1_extension_class' => Type1TypeExtension::class,
|
||||||
|
],
|
||||||
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -261,7 +286,7 @@ class FormPassTest extends TestCase
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException \InvalidArgumentException
|
||||||
* @expectedExceptionMessage "form.type_extension" tagged services have to implement the static getExtendedTypes() method. The class for service "my.type_extension" does not implement it.
|
* @expectedExceptionMessage "form.type_extension" tagged services have to implement the static getExtendedTypes() method. Class "stdClass" for service "my.type_extension" does not implement it.
|
||||||
*/
|
*/
|
||||||
public function testAddTaggedFormTypeExtensionWithoutExtendedTypeAttributeNorImplementingGetExtendedTypes()
|
public function testAddTaggedFormTypeExtensionWithoutExtendedTypeAttributeNorImplementingGetExtendedTypes()
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user