Skip to content

Commit 5d8e49c

Browse files
committed
minor #18243 [DependencyInjection] Mark service as public with #[Autoconfigure] attribute (adveris-aadam)
This PR was merged into the 5.4 branch. Discussion ---------- [DependencyInjection] Mark service as public with #[Autoconfigure] attribute I wanted to mark a service as public with an attribute to avoid yaml configuration, but I could not find anything in the documentation. So I started digging and found out that the `#[Autoconfigure]` attribute allows to do that. Also, the mention > PHP attributes require at least PHP 8.0. is not needed in 6.0. Should I create an other PR (if this one is relevant) ? Commits ------- d09cf03 [DI] Mark service as public with #[Autoconfigure] attribute
2 parents 7fb3276 + d09cf03 commit 5d8e49c

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

service_container.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,26 @@ setting:
10361036
;
10371037
};
10381038
1039+
It is also possible to define a service as public thanks to the ``#[Autoconfigure]``
1040+
attribute. This attribute must be used directly on the class of the service
1041+
you want to configure::
1042+
1043+
// src/Service/PublicService.php
1044+
namespace App\Service;
1045+
1046+
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
1047+
1048+
#[Autoconfigure(public: true)]
1049+
class PublicService
1050+
{
1051+
// ...
1052+
}
1053+
1054+
.. versionadded:: 5.3
1055+
1056+
The ``#[Autoconfigure]`` attribute was introduced in Symfony 5.3. PHP
1057+
attributes require at least PHP 8.0.
1058+
10391059
.. deprecated:: 5.1
10401060

10411061
As of Symfony 5.1, it is no longer possible to autowire the service

service_container/alias_private.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,26 @@ You can also control the ``public`` option on a service-by-service basis:
6262
->public();
6363
};
6464
65+
It is also possible to define a service as public thanks to the ``#[Autoconfigure]``
66+
attribute. This attribute must be used directly on the class of the service
67+
you want to configure::
68+
69+
// src/Service/Foo.php
70+
namespace App\Service;
71+
72+
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
73+
74+
#[Autoconfigure(public: true)]
75+
class Foo
76+
{
77+
// ...
78+
}
79+
80+
.. versionadded:: 5.3
81+
82+
The ``#[Autoconfigure]`` attribute was introduced in Symfony 5.3. PHP
83+
attributes require at least PHP 8.0.
84+
6585
.. _services-why-private:
6686

6787
Private services are special because they allow the container to optimize whether

0 commit comments

Comments
 (0)