Skip to content

Commit

Permalink
Remove wrong deprecation triggers for forms in the DI extension
Browse files Browse the repository at this point in the history
When a form type provides a BC layer with old form names (all core types
do), the form registry will ask for type extensions registered on the
legacy name for BC, and trigger a warning if it finds any.
The DependencyInjectionExtension should not trigger warnings on its own
when being asked for such extensions (especially when it has none
registered).
Core extensions are also registered using the proper extended type
rather than legacy names.
  • Loading branch information
stof committed Sep 9, 2015
1 parent cecc2ee commit e42adf7
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 80 deletions.
Expand Up @@ -33,20 +33,14 @@ public function process(ContainerBuilder $container)
// Builds an array with service IDs as keys and tag aliases as values
$types = array();

// Remember which names will not be supported in Symfony 3.0 to trigger
// deprecation errors
$legacyNames = array();

foreach ($container->findTaggedServiceIds('form.type') as $serviceId => $tag) {
// The following if-else block is deprecated and will be removed
// in Symfony 3.0
// Deprecation errors are triggered in DependencyInjectionExtension
// Deprecation errors are triggered in the form registry
if (isset($tag[0]['alias'])) {
$types[$tag[0]['alias']] = $serviceId;
$legacyNames[$tag[0]['alias']] = true;
} else {
$types[$serviceId] = $serviceId;
$legacyNames[$serviceId] = true;
}

// Support type access by FQCN
Expand All @@ -55,7 +49,6 @@ public function process(ContainerBuilder $container)
}

$definition->replaceArgument(1, $types);
$definition->replaceArgument(4, $legacyNames);

$typeExtensions = array();

Expand Down
10 changes: 4 additions & 6 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml
Expand Up @@ -46,8 +46,6 @@
<argument type="collection" />
<!-- All services with tag "form.type_guesser" are inserted here by FormPass -->
<argument type="collection" />
<!-- The deprecated type names are inserted here by FormPass -->
<argument type="collection" />
</service>

<!-- ValidatorTypeGuesser -->
Expand Down Expand Up @@ -157,7 +155,7 @@
<!-- FormTypeHttpFoundationExtension -->
<service id="form.type_extension.form.http_foundation" class="Symfony\Component\Form\Extension\HttpFoundation\Type\FormTypeHttpFoundationExtension">
<argument type="service" id="form.type_extension.form.request_handler" />
<tag name="form.type_extension" alias="form" />
<tag name="form.type_extension" alias="Symfony\Component\Form\Extension\Core\Type\FormType" />
</service>

<!-- HttpFoundationRequestHandler -->
Expand All @@ -171,14 +169,14 @@

<!-- FormTypeValidatorExtension -->
<service id="form.type_extension.form.validator" class="Symfony\Component\Form\Extension\Validator\Type\FormTypeValidatorExtension">
<tag name="form.type_extension" alias="form" />
<tag name="form.type_extension" alias="Symfony\Component\Form\Extension\Core\Type\FormType" />
<argument type="service" id="validator" />
</service>
<service id="form.type_extension.repeated.validator" class="Symfony\Component\Form\Extension\Validator\Type\RepeatedTypeValidatorExtension">
<tag name="form.type_extension" alias="repeated" />
<tag name="form.type_extension" alias="Symfony\Component\Form\Extension\Core\Type\RepeatedType" />
</service>
<service id="form.type_extension.submit.validator" class="Symfony\Component\Form\Extension\Validator\Type\SubmitTypeValidatorExtension">
<tag name="form.type_extension" alias="submit" />
<tag name="form.type_extension" alias="Symfony\Component\Form\Extension\Core\Type\SubmitType" />
</service>
</services>
</container>
Expand Up @@ -10,7 +10,7 @@
</service>

<service id="form.type_extension.csrf" class="Symfony\Component\Form\Extension\Csrf\Type\FormTypeCsrfExtension">
<tag name="form.type_extension" alias="form" />
<tag name="form.type_extension" alias="Symfony\Component\Form\Extension\Core\Type\FormType" />
<argument type="service" id="security.csrf.token_manager" />
<argument>%form.type_extension.csrf.enabled%</argument>
<argument>%form.type_extension.csrf.field_name%</argument>
Expand Down
Expand Up @@ -21,7 +21,7 @@

<!-- DataCollectorTypeExtension -->
<service id="form.type_extension.form.data_collector" class="%form.type_extension.form.data_collector.class%">
<tag name="form.type_extension" alias="form" />
<tag name="form.type_extension" alias="Symfony\Component\Form\Extension\Core\Type\FormType" />
<argument type="service" id="data_collector.form" />
</service>

Expand Down
Expand Up @@ -43,7 +43,6 @@ public function testAddTaggedTypes()
array(),
array(),
array(),
array(),
));

$definition1 = new Definition(__CLASS__.'_Type1');
Expand Down Expand Up @@ -80,7 +79,6 @@ public function testUseCustomAliasIfSet()
array(),
array(),
array(),
array(),
));

$definition1 = new Definition(__CLASS__.'_Type1');
Expand All @@ -104,41 +102,6 @@ public function testUseCustomAliasIfSet()
), $extDefinition->getArgument(1));
}

public function testPassLegacyNames()
{
$container = new ContainerBuilder();
$container->addCompilerPass(new FormPass());

$extDefinition = new Definition('Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension');
$extDefinition->setArguments(array(
new Reference('service_container'),
array(),
array(),
array(),
array(),
));

$definition1 = new Definition(__CLASS__.'_Type1');
$definition1->addTag('form.type');
$definition2 = new Definition(__CLASS__.'_Type2');
$definition2->addTag('form.type', array('alias' => 'mytype2'));

$container->setDefinition('form.extension', $extDefinition);
$container->setDefinition('my.type1', $definition1);
$container->setDefinition('my.type2', $definition2);

$container->compile();

$extDefinition = $container->getDefinition('form.extension');

$this->assertEquals(array(
// Service ID if no alias is set
'my.type1' => true,
// Alias if set
'mytype2' => true,
), $extDefinition->getArgument(4));
}

public function testAddTaggedTypeExtensions()
{
$container = new ContainerBuilder();
Expand All @@ -150,7 +113,6 @@ public function testAddTaggedTypeExtensions()
array(),
array(),
array(),
array(),
));

$definition1 = new Definition('stdClass');
Expand Down Expand Up @@ -191,7 +153,6 @@ public function testAddTaggedGuessers()
array(),
array(),
array(),
array(),
));

$definition1 = new Definition('stdClass');
Expand Down
Expand Up @@ -22,19 +22,17 @@ class DependencyInjectionExtension implements FormExtensionInterface
private $typeServiceIds;
private $typeExtensionServiceIds;
private $guesserServiceIds;
private $legacyNames;
private $guesser;
private $guesserLoaded = false;

public function __construct(ContainerInterface $container,
array $typeServiceIds, array $typeExtensionServiceIds,
array $guesserServiceIds, array $legacyNames = array())
array $guesserServiceIds)
{
$this->container = $container;
$this->typeServiceIds = $typeServiceIds;
$this->typeExtensionServiceIds = $typeExtensionServiceIds;
$this->guesserServiceIds = $guesserServiceIds;
$this->legacyNames = $legacyNames;
}

public function getType($name)
Expand All @@ -43,14 +41,10 @@ public function getType($name)
throw new InvalidArgumentException(sprintf('The field type "%s" is not registered with the service container.', $name));
}

if (isset($this->legacyNames[$name])) {
@trigger_error('Accessing form types by type name/service ID is deprecated since version 2.8 and will not be supported in 3.0. Use the fully-qualified type class name instead.', E_USER_DEPRECATED);
}

$type = $this->container->get($this->typeServiceIds[$name]);

// BC: validate result of getName() for legacy names (non-FQCN)
if (isset($this->legacyNames[$name]) && $type->getName() !== $name) {
if ($name !== get_class($type) && $type->getName() !== $name) {
throw new InvalidArgumentException(
sprintf('The type name specified for the service "%s" does not match the actual name. Expected "%s", given "%s"',
$this->typeServiceIds[$name],
Expand All @@ -65,23 +59,11 @@ public function getType($name)

public function hasType($name)
{
if (isset($this->typeServiceIds[$name])) {
if (isset($this->legacyNames[$name])) {
@trigger_error('Accessing form types by type name/service ID is deprecated since version 2.8 and will not be supported in 3.0. Use the fully-qualified type class name instead.', E_USER_DEPRECATED);
}

return true;
}

return false;
return isset($this->typeServiceIds[$name]);
}

public function getTypeExtensions($name)
{
if (isset($this->legacyNames[$name])) {
@trigger_error('Accessing form types by type name/service ID is deprecated since version 2.8 and will not be supported in 3.0. Use the fully-qualified type class name instead.', E_USER_DEPRECATED);
}

$extensions = array();

if (isset($this->typeExtensionServiceIds[$name])) {
Expand All @@ -95,10 +77,6 @@ public function getTypeExtensions($name)

public function hasTypeExtensions($name)
{
if (isset($this->legacyNames[$name])) {
@trigger_error('Accessing form types by type name/service ID is deprecated since version 2.8 and will not be supported in 3.0. Use the fully-qualified type class name instead.', E_USER_DEPRECATED);
}

return isset($this->typeExtensionServiceIds[$name]);
}

Expand Down

0 comments on commit e42adf7

Please sign in to comment.