Skip to content

Commit 52b7239

Browse files
committed
feature symfony#24530 [Form] simplify the form type extension registration (xabbuh)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Form] simplify the form type extension registration | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#22833, symfony#27906 | License | MIT | Doc PR | Commits ------- 6a1d4c5 simplify the form type extension registration
2 parents c10d2c0 + 6a1d4c5 commit 52b7239

32 files changed

+474
-82
lines changed

UPGRADE-4.2.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,38 @@ Finder
5555
Form
5656
----
5757

58+
* The `getExtendedType()` method of the `FormTypeExtensionInterface` is deprecated and will be removed in 5.0. Type
59+
extensions must implement the static `getExtendedTypes()` method instead and return an iterable of extended types.
60+
61+
Before:
62+
63+
```php
64+
class FooTypeExtension extends AbstractTypeExtension
65+
{
66+
public function getExtendedType()
67+
{
68+
return FormType::class;
69+
}
70+
71+
// ...
72+
}
73+
```
74+
75+
After:
76+
77+
```php
78+
class FooTypeExtension extends AbstractTypeExtension
79+
{
80+
public static function getExtendedTypes(): iterable
81+
{
82+
return array(FormType::class);
83+
}
84+
85+
// ...
86+
}
87+
```
5888
* The `scale` option of the `IntegerType` is deprecated.
5989
* The `$scale` argument of the `IntegerToLocalizedStringTransformer` is deprecated.
60-
6190
* Deprecated calling `FormRenderer::searchAndRenderBlock` for fields which were already rendered.
6291
Instead of expecting such calls to return empty strings, check if the field has already been rendered.
6392

UPGRADE-5.0.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,36 @@ Finder
7272
Form
7373
----
7474

75+
* The `getExtendedType()` method was removed from the `FormTypeExtensionInterface`. It is replaced by the the static
76+
`getExtendedTypes()` method which must return an iterable of extended types.
77+
78+
Before:
79+
80+
```php
81+
class FooTypeExtension extends AbstractTypeExtension
82+
{
83+
public function getExtendedType()
84+
{
85+
return FormType::class;
86+
}
87+
88+
// ...
89+
}
90+
```
91+
92+
After:
93+
94+
```php
95+
class FooTypeExtension extends AbstractTypeExtension
96+
{
97+
public static function getExtendedTypes(): iterable
98+
{
99+
return array(FormType::class);
100+
}
101+
102+
// ...
103+
}
104+
```
75105
* The `scale` option was removed from the `IntegerType`.
76106
* The `$scale` argument of the `IntegerToLocalizedStringTransformer` was removed.
77107

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
5353
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
5454
use Symfony\Component\Finder\Finder;
55+
use Symfony\Component\Form\FormTypeExtensionInterface;
5556
use Symfony\Component\Form\FormTypeGuesserInterface;
5657
use Symfony\Component\Form\FormTypeInterface;
5758
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;
@@ -325,6 +326,8 @@ public function load(array $configs, ContainerBuilder $container)
325326
->addTag('form.type');
326327
$container->registerForAutoconfiguration(FormTypeGuesserInterface::class)
327328
->addTag('form.type_guesser');
329+
$container->registerForAutoconfiguration(FormTypeExtensionInterface::class)
330+
->addTag('form.type_extension');
328331
$container->registerForAutoconfiguration(CacheClearerInterface::class)
329332
->addTag('kernel.cache_clearer');
330333
$container->registerForAutoconfiguration(CacheWarmerInterface::class)

src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
<!-- FormTypeHttpFoundationExtension -->
7575
<service id="form.type_extension.form.http_foundation" class="Symfony\Component\Form\Extension\HttpFoundation\Type\FormTypeHttpFoundationExtension">
7676
<argument type="service" id="form.type_extension.form.request_handler" />
77-
<tag name="form.type_extension" extended-type="Symfony\Component\Form\Extension\Core\Type\FormType" />
77+
<tag name="form.type_extension" />
7878
</service>
7979

8080
<!-- HttpFoundationRequestHandler -->
@@ -92,13 +92,13 @@
9292
<argument type="service" id="validator" />
9393
</service>
9494
<service id="form.type_extension.repeated.validator" class="Symfony\Component\Form\Extension\Validator\Type\RepeatedTypeValidatorExtension">
95-
<tag name="form.type_extension" extended-type="Symfony\Component\Form\Extension\Core\Type\RepeatedType" />
95+
<tag name="form.type_extension" />
9696
</service>
9797
<service id="form.type_extension.submit.validator" class="Symfony\Component\Form\Extension\Validator\Type\SubmitTypeValidatorExtension">
9898
<tag name="form.type_extension" extended-type="Symfony\Component\Form\Extension\Core\Type\SubmitType" />
9999
</service>
100100
<service id="form.type_extension.upload.validator" class="Symfony\Component\Form\Extension\Validator\Type\UploadValidatorExtension">
101-
<tag name="form.type_extension" extended-type="Symfony\Component\Form\Extension\Core\Type\FormType" />
101+
<tag name="form.type_extension" />
102102
<argument type="service" id="translator"/>
103103
<argument type="string">%validator.translation_domain%</argument>
104104
</service>

src/Symfony/Bundle/FrameworkBundle/Resources/config/form_csrf.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<defaults public="false" />
99

1010
<service id="form.type_extension.csrf" class="Symfony\Component\Form\Extension\Csrf\Type\FormTypeCsrfExtension">
11-
<tag name="form.type_extension" extended-type="Symfony\Component\Form\Extension\Core\Type\FormType" />
11+
<tag name="form.type_extension" />
1212
<argument type="service" id="security.csrf.token_manager" />
1313
<argument>%form.type_extension.csrf.enabled%</argument>
1414
<argument>%form.type_extension.csrf.field_name%</argument>

src/Symfony/Bundle/FrameworkBundle/Resources/config/form_debug.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
<!-- DataCollectorTypeExtension -->
1818
<service id="form.type_extension.form.data_collector" class="Symfony\Component\Form\Extension\DataCollector\Type\DataCollectorTypeExtension">
19-
<tag name="form.type_extension" extended-type="Symfony\Component\Form\Extension\Core\Type\FormType" />
19+
<tag name="form.type_extension" />
2020
<argument type="service" id="data_collector.form" />
2121
</service>
2222

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"symfony/dom-crawler": "~3.4|~4.0",
4040
"symfony/polyfill-intl-icu": "~1.0",
4141
"symfony/security": "~3.4|~4.0",
42-
"symfony/form": "^4.1",
42+
"symfony/form": "^4.2",
4343
"symfony/expression-language": "~3.4|~4.0",
4444
"symfony/messenger": "^4.2",
4545
"symfony/process": "~3.4|~4.0",
@@ -66,7 +66,7 @@
6666
"phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0",
6767
"symfony/asset": "<3.4",
6868
"symfony/console": "<3.4",
69-
"symfony/form": "<4.1",
69+
"symfony/form": "<4.2",
7070
"symfony/messenger": "<4.2",
7171
"symfony/property-info": "<3.4",
7272
"symfony/serializer": "<4.1",

src/Symfony/Component/Form/AbstractExtension.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,21 @@ private function initTypeExtensions()
175175
throw new UnexpectedTypeException($extension, 'Symfony\Component\Form\FormTypeExtensionInterface');
176176
}
177177

178-
$type = $extension->getExtendedType();
178+
if (method_exists($extension, 'getExtendedTypes')) {
179+
$extendedTypes = array();
179180

180-
$this->typeExtensions[$type][] = $extension;
181+
foreach ($extension::getExtendedTypes() as $extendedType) {
182+
$extendedTypes[] = $extendedType;
183+
}
184+
} else {
185+
@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.', \get_class($extension), FormTypeExtensionInterface::class), E_USER_DEPRECATED);
186+
187+
$extendedTypes = array($extension->getExtendedType());
188+
}
189+
190+
foreach ($extendedTypes as $extendedType) {
191+
$this->typeExtensions[$extendedType][] = $extension;
192+
}
181193
}
182194
}
183195

src/Symfony/Component/Form/AbstractTypeExtension.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Form;
1313

14+
use Symfony\Component\Form\Exception\LogicException;
1415
use Symfony\Component\OptionsResolver\OptionsResolver;
1516

1617
/**
@@ -45,4 +46,22 @@ public function finishView(FormView $view, FormInterface $form, array $options)
4546
public function configureOptions(OptionsResolver $resolver)
4647
{
4748
}
49+
50+
/**
51+
* {@inheritdoc}
52+
*
53+
* @deprecated since Symfony 4.2, use getExtendedTypes() instead.
54+
*/
55+
public function getExtendedType()
56+
{
57+
if (!method_exists($this, 'getExtendedTypes')) {
58+
throw new LogicException(sprintf('You need to implement the static getExtendedTypes() method when implementing the %s in %s.', FormTypeExtensionInterface::class, static::class));
59+
}
60+
61+
@trigger_error(sprintf('The %s::getExtendedType() method is deprecated since Symfony 4.2 and will be removed in 5.0. Use getExtendedTypes() instead.', \get_class($this)), E_USER_DEPRECATED);
62+
63+
foreach (static::getExtendedTypes() as $extendedType) {
64+
return $extendedType;
65+
}
66+
}
4867
}

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,36 @@ CHANGELOG
44
4.2.0
55
-----
66

7+
* The `getExtendedType()` method of the `FormTypeExtensionInterface` is deprecated and will be removed in 5.0. Type
8+
extensions must implement the static `getExtendedTypes()` method instead and return an iterable of extended types.
9+
10+
Before:
11+
12+
```php
13+
class FooTypeExtension extends AbstractTypeExtension
14+
{
15+
public function getExtendedType()
16+
{
17+
return FormType::class;
18+
}
19+
20+
// ...
21+
}
22+
```
23+
24+
After:
25+
26+
```php
27+
class FooTypeExtension extends AbstractTypeExtension
28+
{
29+
public static function getExtendedTypes(): iterable
30+
{
31+
return array(FormType::class);
32+
}
33+
34+
// ...
35+
}
36+
```
737
* deprecated the `$scale` argument of the `IntegerToLocalizedStringTransformer`
838
* added `Symfony\Component\Form\ClearableErrorsInterface`
939
* deprecated calling `FormRenderer::searchAndRenderBlock` for fields which were already rendered

0 commit comments

Comments
 (0)