Skip to content

Commit 95b8a38

Browse files
committed
minor #14394 [DependencyInjection] Document the Required attribute (derrabus)
This PR was merged into the 5.x branch. Discussion ---------- [DependencyInjection] Document the Required attribute This PR adds documentation for symfony/symfony#37545 and fixes #14189. Commits ------- 701f704 [DependencyInjection] Document the Required attribute.
2 parents 0a5df46 + 701f704 commit 95b8a38

File tree

2 files changed

+85
-28
lines changed

2 files changed

+85
-28
lines changed

service_container/autowiring.rst

Lines changed: 84 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -544,50 +544,107 @@ Autowiring other Methods (e.g. Setters and Public Typed Properties)
544544

545545
When autowiring is enabled for a service, you can *also* configure the container
546546
to call methods on your class when it's instantiated. For example, suppose you want
547-
to inject the ``logger`` service, and decide to use setter-injection::
547+
to inject the ``logger`` service, and decide to use setter-injection:
548548

549-
// src/Util/Rot13Transformer.php
550-
namespace App\Util;
549+
.. configuration-block::
551550

552-
class Rot13Transformer
553-
{
554-
private $logger;
551+
.. code-block:: php-annotations
555552
556-
/**
557-
* @required
558-
*/
559-
public function setLogger(LoggerInterface $logger)
553+
// src/Util/Rot13Transformer.php
554+
namespace App\Util;
555+
556+
class Rot13Transformer
560557
{
561-
$this->logger = $logger;
558+
private $logger;
559+
560+
/**
561+
* @required
562+
*/
563+
public function setLogger(LoggerInterface $logger)
564+
{
565+
$this->logger = $logger;
566+
}
567+
568+
public function transform($value)
569+
{
570+
$this->logger->info('Transforming '.$value);
571+
// ...
572+
}
562573
}
563574
564-
public function transform($value)
575+
.. code-block:: php-attributes
576+
577+
// src/Util/Rot13Transformer.php
578+
namespace App\Util;
579+
580+
use Symfony\Contracts\Service\Attribute\Required;
581+
582+
class Rot13Transformer
565583
{
566-
$this->logger->info('Transforming '.$value);
567-
// ...
584+
private $logger;
585+
586+
#[Required]
587+
public function setLogger(LoggerInterface $logger)
588+
{
589+
$this->logger = $logger;
590+
}
591+
592+
public function transform($value)
593+
{
594+
$this->logger->info('Transforming '.$value);
595+
// ...
596+
}
568597
}
569-
}
570598
571-
Autowiring will automatically call *any* method with the ``@required`` annotation
599+
Autowiring will automatically call *any* method with the ``#[Required]`` attribute
572600
above it, autowiring each argument. If you need to manually wire some of the arguments
573601
to a method, you can always explicitly :doc:`configure the method call </service_container/calls>`.
574602

575-
Despite property injection has some :ref:`drawbacks <property-injection>`, autowiring with ``@required`` annotation
576-
can also be applied to public typed properties::
603+
If you need to stay compatible with PHP 7 and thus cannot use attributes, you can use
604+
the ``@required`` annotation instead.
577605

578-
namespace App\Util;
606+
.. versionadded:: 5.2
579607

580-
class Rot13Transformer
581-
{
582-
/** @required */
583-
public LoggerInterface $logger;
608+
The ``#[Required]`` attribute was introduced in Symfony 5.2.
584609

585-
public function transform($value)
610+
Despite property injection has some :ref:`drawbacks <property-injection>`, autowiring with ``#[Required]``
611+
or ``@required`` can also be applied to public typed properties::
612+
613+
.. configuration-block::
614+
615+
.. code-block:: php-annotations
616+
617+
namespace App\Util;
618+
619+
class Rot13Transformer
586620
{
587-
$this->logger->info('Transforming '.$value);
588-
// ...
621+
/** @required */
622+
public LoggerInterface $logger;
623+
624+
public function transform($value)
625+
{
626+
$this->logger->info('Transforming '.$value);
627+
// ...
628+
}
629+
}
630+
631+
.. code-block:: php-attributes
632+
633+
namespace App\Util;
634+
635+
use Symfony\Contracts\Service\Attribute\Required;
636+
637+
class Rot13Transformer
638+
{
639+
#[Required]
640+
public LoggerInterface $logger;
641+
642+
public function transform($value)
643+
{
644+
$this->logger->info('Transforming '.$value);
645+
// ...
646+
}
589647
}
590-
}
591648
592649
.. versionadded:: 5.1
593650

service_container/calls.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Service Method Calls and Setter Injection
66

77
.. tip::
88

9-
If you're using autowiring, you can use ``@required`` to
9+
If you're using autowiring, you can use ``#[Required]`` or ``@required`` to
1010
:ref:`automatically configure method calls <autowiring-calls>`.
1111

1212
Usually, you'll want to inject your dependencies via the constructor. But sometimes,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy