Skip to content

Commit

Permalink
bug #31201 [Form] resolve class name parameters (xabbuh)
Browse files Browse the repository at this point in the history
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
-------

5235be4 resolve class name parameters
  • Loading branch information
fabpot committed Apr 24, 2019
2 parents 0773baf + 5235be4 commit 571647f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
16 changes: 9 additions & 7 deletions src/Symfony/Component/Form/DependencyInjection/FormPass.php
Expand Up @@ -92,18 +92,20 @@ private function processFormTypeExtensions(ContainerBuilder $container)
$serviceDefinition = $container->getDefinition($serviceId);

$tag = $serviceDefinition->getTag($this->formTypeExtensionTag);
$typeExtensionClass = $container->getParameterBag()->resolveValue($serviceDefinition->getClass());

if (isset($tag[0]['extended_type'])) {
if (!method_exists($serviceDefinition->getClass(), '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);
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.', $typeExtensionClass, FormTypeExtensionInterface::class), E_USER_DEPRECATED);
}

$typeExtensions[$tag[0]['extended_type']][] = new Reference($serviceId);
$typeExtensionsClasses[] = $serviceDefinition->getClass();
} elseif (method_exists($serviceDefinition->getClass(), 'getExtendedTypes')) {
$typeExtensionsClasses[] = $typeExtensionClass;
} elseif (method_exists($typeExtensionClass, 'getExtendedTypes')) {
$extendsTypes = false;

$typeExtensionsClasses[] = $serviceDefinition->getClass();
foreach ($serviceDefinition->getClass()::getExtendedTypes() as $extendedType) {
$typeExtensionsClasses[] = $typeExtensionClass;
foreach ($typeExtensionClass::getExtendedTypes() as $extendedType) {
$typeExtensions[$extendedType][] = new Reference($serviceId);
$extendsTypes = true;
}
Expand All @@ -112,7 +114,7 @@ private function processFormTypeExtensions(ContainerBuilder $container)
throw new InvalidArgumentException(sprintf('The getExtendedTypes() method for service "%s" does not return any extended types.', $serviceId));
}
} 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));
}
}

Expand Down
Expand Up @@ -97,10 +97,14 @@ public function testAddTaggedTypesToDebugCommand()
/**
* @dataProvider addTaggedTypeExtensionsDataProvider
*/
public function testAddTaggedTypeExtensions(array $extensions, array $expectedRegisteredExtensions)
public function testAddTaggedTypeExtensions(array $extensions, array $expectedRegisteredExtensions, array $parameters = [])
{
$container = $this->createContainerBuilder();

foreach ($parameters as $name => $value) {
$container->setParameter($name, $value);
}

$container->setDefinition('form.extension', $this->createExtensionDefinition());

foreach ($extensions as $serviceId => $config) {
Expand Down Expand Up @@ -191,6 +195,27 @@ public function addTaggedTypeExtensionsDataProvider()
]),
],
],
[
[
'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,
],
],
];
}

Expand Down Expand Up @@ -261,7 +286,7 @@ public function addLegacyTaggedTypeExtensionsDataProvider()

/**
* @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()
{
Expand Down

0 comments on commit 571647f

Please sign in to comment.