Skip to content

Commit c082976

Browse files
committed
Merge branch '6.2' into 6.3
* 6.2: [DependencyInjection] Add `closure` argument type import function env to avoid fatal error if copying docs Use DOCtor-RST 1.37.0 [Lock] Complete Lock example for less ambiguity
2 parents 596fe2a + c5c1c78 commit c082976

File tree

4 files changed

+132
-2
lines changed

4 files changed

+132
-2
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ jobs:
7373
key: ${{ runner.os }}-doctor-rst-${{ steps.extract_base_branch.outputs.branch }}
7474

7575
- name: "Run DOCtor-RST"
76-
uses: docker://oskarstark/doctor-rst:1.35.1
76+
uses: docker://oskarstark/doctor-rst:1.37.0
7777
with:
7878
args: --short --error-format=github --cache-file=/github/workspace/.cache/doctor-rst.cache
7979

components/lock.rst

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,42 @@ Serializing Locks
7575
The :class:`Symfony\\Component\\Lock\\Key` contains the state of the
7676
:class:`Symfony\\Component\\Lock\\Lock` and can be serialized. This
7777
allows the user to begin a long job in a process by acquiring the lock, and
78-
continue the job in another process using the same lock::
78+
continue the job in another process using the same lock.
7979

80+
First, you may create a serializable class containing the resource and the
81+
key of the lock::
82+
83+
// src/Lock/RefreshTaxonomy.php
84+
namespace App\Lock;
85+
86+
use Symfony\Component\Lock\Key;
87+
88+
class RefreshTaxonomy
89+
{
90+
private object $article;
91+
private Key $key;
92+
93+
public function __construct(object $article, Key $key)
94+
{
95+
$this->article = $article;
96+
$this->key = $key;
97+
}
98+
99+
public function getArticle(): object
100+
{
101+
return $this->article;
102+
}
103+
104+
public function getKey(): Key
105+
{
106+
return $this->key;
107+
}
108+
}
109+
110+
Then, you can use this class to dispatch all that's needed for another process
111+
to handle the rest of the job::
112+
113+
use App\Lock\RefreshTaxonomy;
80114
use Symfony\Component\Lock\Key;
81115
use Symfony\Component\Lock\Lock;
82116

mailer.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ over SMTP by configuring the DSN in your ``.env`` file (the ``user``,
5454
.. code-block:: php
5555
5656
// config/packages/mailer.php
57+
use function Symfony\Component\DependencyInjection\Loader\Configurator\env;
5758
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
5859
5960
return static function (ContainerConfigurator $containerConfigurator): void {
@@ -1179,6 +1180,7 @@ This can be configured by replacing the ``dsn`` configuration entry with a
11791180
.. code-block:: php
11801181
11811182
// config/packages/mailer.php
1183+
use function Symfony\Component\DependencyInjection\Loader\Configurator\env;
11821184
use Symfony\Config\FrameworkConfig;
11831185
11841186
return static function (FrameworkConfig $framework) {

service_container.rst

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,100 @@ For a full list of *all* possible services in the container, run:
700700
701701
$ php bin/console debug:container
702702
703+
Injecting a Closure as an Argument
704+
----------------------------------
705+
706+
It is possible to inject a callable as an argument of a service.
707+
Let's add an argument to our ``MessageGenerator`` constructor::
708+
709+
// src/Service/MessageGenerator.php
710+
namespace App\Service;
711+
712+
use Psr\Log\LoggerInterface;
713+
714+
class MessageGenerator
715+
{
716+
private $logger;
717+
private $messageHash;
718+
719+
public function __construct(LoggerInterface $logger, callable $generateMessageHash)
720+
{
721+
$this->logger = $logger;
722+
$this->messageHash = $generateMessageHash();
723+
}
724+
// ...
725+
}
726+
727+
Now, we would add a new invokable service to generate the message hash::
728+
729+
// src/Hash/MessageHashGenerator.php
730+
namespace App\Hash;
731+
732+
class MessageHashGenerator
733+
{
734+
public function __invoke(): string
735+
{
736+
// Compute and return a message hash
737+
}
738+
}
739+
740+
Our configuration looks like this:
741+
742+
.. configuration-block::
743+
744+
.. code-block:: yaml
745+
746+
# config/services.yaml
747+
services:
748+
# ... same code as before
749+
750+
# explicitly configure the service
751+
App\Service\MessageGenerator:
752+
arguments:
753+
$logger: '@monolog.logger.request'
754+
$generateMessageHash: !closure '@App\Hash\MessageHashGenerator'
755+
756+
.. code-block:: xml
757+
758+
<!-- config/services.xml -->
759+
<?xml version="1.0" encoding="UTF-8" ?>
760+
<container xmlns="http://symfony.com/schema/dic/services"
761+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
762+
xsi:schemaLocation="http://symfony.com/schema/dic/services
763+
https://symfony.com/schema/dic/services/services-1.0.xsd">
764+
765+
<services>
766+
<!-- ... same code as before -->
767+
768+
<!-- Explicitly configure the service -->
769+
<service id="App\Service\MessageGenerator">
770+
<argument key="$logger" type="service" id="monolog.logger.request"/>
771+
<argument key="$generateMessageHash" type="closure" id="App\Hash\MessageHashGenerator"/>
772+
</service>
773+
</services>
774+
</container>
775+
776+
.. code-block:: php
777+
778+
// config/services.php
779+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
780+
781+
use App\Service\MessageGenerator;
782+
783+
return function(ContainerConfigurator $containerConfigurator) {
784+
// ... same code as before
785+
786+
// explicitly configure the service
787+
$services->set(MessageGenerator::class)
788+
->arg('$logger', service('monolog.logger.request'))
789+
->arg('$generateMessageHash', closure('App\Hash\MessageHashGenerator'))
790+
;
791+
};
792+
793+
.. versionadded:: 6.1
794+
795+
The ``closure`` argument type was introduced in Symfony 6.1.
796+
703797
.. _services-binding:
704798

705799
Binding Arguments by Name or Type

0 commit comments

Comments
 (0)