Skip to content

Commit 8dd9cb1

Browse files
committed
Merge branch '5.4' into 6.3
* 5.4: Tweak [DependencyInjection] Autoconfigurable attributes on methods, properties and parameters
2 parents 3b9cf72 + bedfda0 commit 8dd9cb1

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

service_container/tags.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,54 @@ method::
208208
}
209209
}
210210

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+
211259
Creating custom Tags
212260
--------------------
213261

0 commit comments

Comments
 (0)