Commit c539c77
committed
feature symfony#61528 [Validator] Allow using attributes to declare compile-time constraint metadata (nicolas-grekas)
This PR was merged into the 7.4 branch.
Discussion
----------
[Validator] Allow using attributes to declare compile-time constraint metadata
| Q | A
| ------------- | ---
| Branch? | 7.4
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Issues | -
| License | MIT
Prerequisite for symfony#61288
At the moment, validation attributes are read at runtime when `framework.validation.enable_attributes` is true.
This means they don't fit for bundles nor can't they be warmed up.
This PR fixes both issues by using a new `validator.attribute_metadata` resource tag, that's turned into a list of classes to parse for attributes at compile-time.
For apps, the tag is added by autoconfiguration: any `Constraint`-derived attributes found on a class in the `src/` folder will trigger the rule to add the tag.
For bundles (and for apps if they want to), the tag is added by explicit service configuration. In an "eat your own dog-food" spirit, this capability is used to declare the constraints of the `Form` class: instead of loading the `validation.xml` file, we now declare this service resource:
```php
->set('validator.form.attribute_metadata', Form::class)
->tag('container.excluded')
->tag('validator.attribute_metadata')
```
This reads the attributes added to the `Form` class:
```php
#[AssertForm()]
#[Traverse(false)]
class Form implements \IteratorAggregate, FormInterface, ClearableErrorsInterface
```
Bundles can do the same and replace their XML files by attributes.
As a next step, we could also deprecate runtime-discovery of attributes. This could be worth it if this discovery has a measurable performance impact. To be measured if one wants to dig this idea.
Side note: I'm hoping this could allow removing the yaml and xml config formats one day. For serialization metadata also (PR coming). BUT, this doesn't (yet) cover the use case of overriding metadata defined by bundles. For that, apps still have to use xml or yaml in config/validation/. I have an idea to cover this, coming to a next PR if it works.
(failures unrelated)
Commits
-------
0197b50 [Validator] Allow using attributes to declare compile-time constraint metadataFile tree
17 files changed
+305
-78
lines changed- src/Symfony
- Bundle/FrameworkBundle
- CacheWarmer
- DependencyInjection
- Compiler
- Resources/config
- Tests/DependencyInjection
- Component
- Form
- Extension/Validator/Constraints
- Validator
- DependencyInjection
- Mapping/Loader
- Tests
- DependencyInjection
- Mapping/Loader
17 files changed
+305
-78
lines changedLines changed: 4 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
24 | | - | |
| 25 | + | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
| |||
77 | 78 | | |
78 | 79 | | |
79 | 80 | | |
80 | | - | |
| 81 | + | |
81 | 82 | | |
82 | 83 | | |
83 | 84 | | |
84 | 85 | | |
85 | 86 | | |
86 | 87 | | |
87 | | - | |
| 88 | + | |
88 | 89 | | |
89 | 90 | | |
90 | 91 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
| 105 | + | |
105 | 106 | | |
106 | 107 | | |
107 | 108 | | |
| |||
Lines changed: 22 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
216 | 216 | | |
217 | 217 | | |
218 | 218 | | |
| 219 | + | |
219 | 220 | | |
| 221 | + | |
220 | 222 | | |
221 | 223 | | |
222 | 224 | | |
| |||
1801 | 1803 | | |
1802 | 1804 | | |
1803 | 1805 | | |
1804 | | - | |
| 1806 | + | |
1805 | 1807 | | |
1806 | 1808 | | |
1807 | 1809 | | |
1808 | | - | |
| 1810 | + | |
1809 | 1811 | | |
1810 | 1812 | | |
1811 | 1813 | | |
1812 | 1814 | | |
1813 | 1815 | | |
1814 | 1816 | | |
1815 | | - | |
| 1817 | + | |
| 1818 | + | |
| 1819 | + | |
| 1820 | + | |
| 1821 | + | |
| 1822 | + | |
| 1823 | + | |
| 1824 | + | |
| 1825 | + | |
| 1826 | + | |
1816 | 1827 | | |
1817 | 1828 | | |
1818 | 1829 | | |
1819 | | - | |
| 1830 | + | |
1820 | 1831 | | |
1821 | 1832 | | |
1822 | 1833 | | |
| |||
1855 | 1866 | | |
1856 | 1867 | | |
1857 | 1868 | | |
1858 | | - | |
1859 | | - | |
1860 | | - | |
| 1869 | + | |
| 1870 | + | |
| 1871 | + | |
| 1872 | + | |
| 1873 | + | |
1861 | 1874 | | |
1862 | 1875 | | |
1863 | 1876 | | |
| |||
2060 | 2073 | | |
2061 | 2074 | | |
2062 | 2075 | | |
2063 | | - | |
| 2076 | + | |
2064 | 2077 | | |
2065 | 2078 | | |
2066 | 2079 | | |
| |||
2100 | 2113 | | |
2101 | 2114 | | |
2102 | 2115 | | |
2103 | | - | |
| 2116 | + | |
2104 | 2117 | | |
2105 | 2118 | | |
2106 | 2119 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| 78 | + | |
78 | 79 | | |
79 | 80 | | |
80 | 81 | | |
| |||
155 | 156 | | |
156 | 157 | | |
157 | 158 | | |
| 159 | + | |
158 | 160 | | |
159 | 161 | | |
160 | 162 | | |
| |||
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
127 | 128 | | |
128 | 129 | | |
129 | 130 | | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
130 | 135 | | |
131 | 136 | | |
Lines changed: 33 additions & 25 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
| 96 | + | |
96 | 97 | | |
97 | 98 | | |
98 | 99 | | |
| |||
1312 | 1313 | | |
1313 | 1314 | | |
1314 | 1315 | | |
1315 | | - | |
1316 | | - | |
1317 | | - | |
1318 | | - | |
| 1316 | + | |
| 1317 | + | |
| 1318 | + | |
| 1319 | + | |
| 1320 | + | |
1319 | 1321 | | |
1320 | 1322 | | |
1321 | 1323 | | |
1322 | | - | |
| 1324 | + | |
1323 | 1325 | | |
1324 | | - | |
| 1326 | + | |
1325 | 1327 | | |
1326 | 1328 | | |
1327 | 1329 | | |
| |||
1333 | 1335 | | |
1334 | 1336 | | |
1335 | 1337 | | |
1336 | | - | |
| 1338 | + | |
1337 | 1339 | | |
1338 | 1340 | | |
1339 | 1341 | | |
| |||
1408 | 1410 | | |
1409 | 1411 | | |
1410 | 1412 | | |
1411 | | - | |
1412 | | - | |
1413 | | - | |
1414 | | - | |
1415 | | - | |
1416 | | - | |
1417 | | - | |
| 1413 | + | |
| 1414 | + | |
| 1415 | + | |
| 1416 | + | |
| 1417 | + | |
| 1418 | + | |
| 1419 | + | |
| 1420 | + | |
| 1421 | + | |
| 1422 | + | |
1418 | 1423 | | |
1419 | | - | |
| 1424 | + | |
| 1425 | + | |
1420 | 1426 | | |
1421 | 1427 | | |
1422 | 1428 | | |
| |||
1434 | 1440 | | |
1435 | 1441 | | |
1436 | 1442 | | |
1437 | | - | |
1438 | | - | |
1439 | | - | |
1440 | | - | |
1441 | | - | |
1442 | | - | |
1443 | | - | |
1444 | | - | |
| 1443 | + | |
| 1444 | + | |
| 1445 | + | |
| 1446 | + | |
| 1447 | + | |
| 1448 | + | |
| 1449 | + | |
| 1450 | + | |
| 1451 | + | |
| 1452 | + | |
1445 | 1453 | | |
1446 | | - | |
| 1454 | + | |
| 1455 | + | |
1447 | 1456 | | |
1448 | 1457 | | |
1449 | 1458 | | |
| |||
1490 | 1499 | | |
1491 | 1500 | | |
1492 | 1501 | | |
1493 | | - | |
1494 | 1502 | | |
1495 | 1503 | | |
1496 | 1504 | | |
| |||
Lines changed: 8 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| 20 | + | |
19 | 21 | | |
20 | 22 | | |
21 | 23 | | |
| |||
26 | 28 | | |
27 | 29 | | |
28 | 30 | | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
29 | 37 | | |
30 | 38 | | |
31 | 39 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| 31 | + | |
30 | 32 | | |
31 | 33 | | |
32 | 34 | | |
| |||
68 | 70 | | |
69 | 71 | | |
70 | 72 | | |
| 73 | + | |
| 74 | + | |
71 | 75 | | |
72 | 76 | | |
73 | 77 | | |
| |||
301 | 305 | | |
302 | 306 | | |
303 | 307 | | |
304 | | - | |
| 308 | + | |
305 | 309 | | |
306 | 310 | | |
307 | 311 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| |||
0 commit comments