Skip to content

Commit

Permalink
minor #18243 [DependencyInjection] Mark service as public with #[Auto…
Browse files Browse the repository at this point in the history
…configure] 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
  • Loading branch information
javiereguiluz committed Jun 21, 2024
2 parents 7fb3276 + d09cf03 commit 5d8e49c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
20 changes: 20 additions & 0 deletions service_container.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,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

0 comments on commit 5d8e49c

Please sign in to comment.