Skip to content

[DependencyInjection] Mark service as public with #[Autoconfigure] attribute #18243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions service_container.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions service_container/alias_private.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down