Skip to content

Commit b81df68

Browse files
committed
minor #18820 [DependencyInjection] Autoconfigurable attributes on methods, properties and parameters (alexandre-daubois)
This PR was merged into the 5.4 branch. Discussion ---------- [DependencyInjection] Autoconfigurable attributes on methods, properties and parameters Fix #15763 Commits ------- 9571b51 [DependencyInjection] Autoconfigurable attributes on methods, properties and parameters
2 parents 3cf253a + 9571b51 commit b81df68

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

service_container/tags.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,65 @@ method::
214214
}
215215
}
216216

217+
To go even further, the ``SensitiveElement`` attribute can be updated to be
218+
usable on methods::
219+
220+
// src/Attribute/SensitiveElement.php
221+
namespace App\Attribute;
222+
223+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
224+
class SensitiveElement
225+
{
226+
// ...
227+
}
228+
229+
We should now update the call to
230+
:method:`Symfony\\Component\\DependencyInjection\\ContainerBuilder::registerAttributeForAutoconfiguration`
231+
to support ``ReflectionMethod``::
232+
233+
// src/Kernel.php
234+
use App\Attribute\SensitiveElement;
235+
236+
class Kernel extends BaseKernel
237+
{
238+
// ...
239+
240+
protected function build(ContainerBuilder $container): void
241+
{
242+
// ...
243+
244+
$container->registerAttributeForAutoconfiguration(SensitiveElement::class, static function (
245+
ChildDefinition $definition,
246+
SensitiveElement $attribute,
247+
// we update the union type to support multiple type of reflection
248+
// you can also use the "\Reflector" interface
249+
\ReflectionClass|\ReflectionMethod $reflector): void {
250+
if ($reflection instanceof \ReflectionMethod) {
251+
// ...
252+
}
253+
}
254+
);
255+
}
256+
}
257+
258+
.. tip::
259+
260+
You can also define an attribute to be usable on properties and parameters with
261+
``Attribute::TARGET_PROPERTY`` and ``Attribute::TARGET_PARAMETER``, then support
262+
``ReflectionProperty`` and ``ReflectionParameter`` in your
263+
:method:`Symfony\\Component\\DependencyInjection\\ContainerBuilder::registerAttributeForAutoconfiguration`
264+
callable.
265+
217266
.. versionadded:: 5.3
218267

219268
The :method:`Symfony\\Component\\DependencyInjection\\ContainerBuilder::registerAttributeForAutoconfiguration`
220269
method was introduced in Symfony 5.3.
221270

271+
.. versionadded:: 5.4
272+
273+
The support for autoconfigurable methods, properties and parameters was
274+
introduced in Symfony 5.4.
275+
222276
Creating custom Tags
223277
--------------------
224278

0 commit comments

Comments
 (0)