Skip to content

Commit a96fa9b

Browse files
committed
minor #17835 [Lock] Complete Lock example for less ambiguity (alexandre-daubois)
This PR was merged into the 5.4 branch. Discussion ---------- [Lock] Complete Lock example for less ambiguity Lately, I used a lot the Lock component. When it came to serializing it, I had a pretty hard time to understand the example because of the mention of a `RefreshTaxonomy` class. I wondered if this was a class already present in the framework or something like that, which blocked me a few minutes. This PR aims to clarify this part a bit by showing that `RefreshTaxonomy` is an arbitrary class. Commits ------- 2d4ba43 [Lock] Complete Lock example for less ambiguity
2 parents 999f2da + 2d4ba43 commit a96fa9b

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

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

0 commit comments

Comments
 (0)