@@ -208,6 +208,54 @@ method::
208
208
}
209
209
}
210
210
211
+ You can also make attributes usable on methods. To do so, update the previous
212
+ example and add ``Attribute::TARGET_METHOD` ::
213
+
214
+ // src/Attribute/SensitiveElement.php
215
+ namespace App\Attribute;
216
+
217
+ #[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
218
+ class SensitiveElement
219
+ {
220
+ // ...
221
+ }
222
+
223
+ Then, update the :method: `Symfony\\ Component\\ DependencyInjection\\ ContainerBuilder::registerAttributeForAutoconfiguration `
224
+ call to support ``ReflectionMethod ``::
225
+
226
+ // src/Kernel.php
227
+ use App\Attribute\SensitiveElement;
228
+
229
+ class Kernel extends BaseKernel
230
+ {
231
+ // ...
232
+
233
+ protected function build(ContainerBuilder $container): void
234
+ {
235
+ // ...
236
+
237
+ $container->registerAttributeForAutoconfiguration(SensitiveElement::class, static function (
238
+ ChildDefinition $definition,
239
+ SensitiveElement $attribute,
240
+ // update the union type to support multiple types of reflection
241
+ // you can also use the "\Reflector" interface
242
+ \ReflectionClass|\ReflectionMethod $reflector): void {
243
+ if ($reflection instanceof \ReflectionMethod) {
244
+ // ...
245
+ }
246
+ }
247
+ );
248
+ }
249
+ }
250
+
251
+ .. tip ::
252
+
253
+ You can also define an attribute to be usable on properties and parameters with
254
+ ``Attribute::TARGET_PROPERTY `` and ``Attribute::TARGET_PARAMETER ``; then support
255
+ ``ReflectionProperty `` and ``ReflectionParameter `` in your
256
+ :method: `Symfony\\ Component\\ DependencyInjection\\ ContainerBuilder::registerAttributeForAutoconfiguration `
257
+ callable.
258
+
211
259
Creating custom Tags
212
260
--------------------
213
261
0 commit comments