[DI] Mark service as public with #[Autoconfigure] attribute

This commit is contained in:
Aurélien ADAM
2023-04-22 16:13:04 +02:00
parent 22ea372bc9
commit d09cf034f6
2 changed files with 40 additions and 0 deletions

View File

@@ -927,6 +927,26 @@ setting:
;
};
It is also possible to define a service as public thanks to the ``#[Autoconfigure]``
attribute. This attribute must be used directly on the class of the service
you want to configure::
// src/Service/PublicService.php
namespace App\Service;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
#[Autoconfigure(public: true)]
class PublicService
{
// ...
}
.. versionadded:: 5.3
The ``#[Autoconfigure]`` attribute was introduced in Symfony 5.3. PHP
attributes require at least PHP 8.0.
.. deprecated:: 5.1
As of Symfony 5.1, it is no longer possible to autowire the service

View File

@@ -62,6 +62,26 @@ You can also control the ``public`` option on a service-by-service basis:
->public();
};
It is also possible to define a service as public thanks to the ``#[Autoconfigure]``
attribute. This attribute must be used directly on the class of the service
you want to configure::
// src/Service/Foo.php
namespace App\Service;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
#[Autoconfigure(public: true)]
class Foo
{
// ...
}
.. versionadded:: 5.3
The ``#[Autoconfigure]`` attribute was introduced in Symfony 5.3. PHP
attributes require at least PHP 8.0.
.. _services-why-private:
Private services are special because they allow the container to optimize whether