|
1 | 1 | LockHandler
|
2 | 2 | ===========
|
3 | 3 |
|
4 |
| -What is a Lock? |
5 |
| ---------------- |
6 |
| - |
7 |
| -File locking is a mechanism that restricts access to a computer file by allowing |
8 |
| -only one user or process access at any specific time. This mechanism was |
9 |
| -introduced a few decades ago for mainframes, but continues being useful for |
10 |
| -modern applications. |
11 |
| - |
12 |
| -Symfony provides a LockHelper to help you use locks in your project. |
13 |
| - |
14 |
| -Usage |
15 |
| ------ |
16 |
| - |
17 | 4 | .. caution::
|
18 | 5 |
|
19 |
| - The lock handler only works if you're using just one server. If you have |
20 |
| - several hosts, you must not use this helper. |
21 |
| - |
22 |
| -A lock can be used, for example, to allow only one instance of a command to run. |
23 |
| - |
24 |
| -.. code-block:: php |
25 |
| -
|
26 |
| - use Symfony\Component\Filesystem\LockHandler; |
27 |
| -
|
28 |
| - $lockHandler = new LockHandler('hello.lock'); |
29 |
| - if (!$lockHandler->lock()) { |
30 |
| - // the resource "hello" is already locked by another process |
31 |
| -
|
32 |
| - return 0; |
33 |
| - } |
34 |
| -
|
35 |
| -The first argument of the constructor is a string that it will use as part of |
36 |
| -the name of the file used to create the lock on the local filesystem. A best |
37 |
| -practice for Symfony commands is to use the command name, such as ``acme:my-command``. |
38 |
| -``LockHandler`` sanitizes the contents of the string before creating |
39 |
| -the file, so you can pass any value for this argument. |
40 |
| - |
41 |
| -.. tip:: |
42 |
| - |
43 |
| - The ``.lock`` extension is optional, but it's a common practice to include |
44 |
| - it. This will make it easier to find lock files on the filesystem. Moreover, |
45 |
| - to avoid name collisions, ``LockHandler`` also appends a hash to the name of |
46 |
| - the lock file. |
47 |
| - |
48 |
| -By default, the lock will be created in the system's temporary directory, but |
49 |
| -you can optionally select the directory where locks are created by passing it as |
50 |
| -the second argument of the constructor. |
51 |
| - |
52 |
| -.. tip:: |
53 |
| - |
54 |
| - Another way to configure the directory where the locks are created is to |
55 |
| - define a special environment variable, because PHP will use that value to |
56 |
| - override the default temporary directory. On Unix-based systems, define the |
57 |
| - ``TMPDIR`` variable. On Windows systems, define any of these variables: |
58 |
| - ``TMP``, ``TEMP`` or ``USERPROFILE`` (they are checked in this order). This |
59 |
| - technique is useful for example when deploying a third-party Symfony |
60 |
| - application whose code can't be modified. |
61 |
| - |
62 |
| -The :method:`Symfony\\Component\\Filesystem\\LockHandler::lock` method tries to |
63 |
| -acquire the lock. If the lock is acquired, the method returns ``true``, |
64 |
| -``false`` otherwise. If the ``lock()`` method is called several times on the same |
65 |
| -instance it will always return ``true`` if the lock was acquired on the first |
66 |
| -call. |
67 |
| - |
68 |
| -You can pass an optional blocking argument as the first argument to the |
69 |
| -``lock()`` method, which defaults to ``false``. If this is set to ``true``, your |
70 |
| -PHP code will wait indefinitely until the lock is released by another process. |
71 |
| - |
72 |
| -.. caution:: |
73 |
| - |
74 |
| - Be aware of the fact that the resource lock is automatically released as soon |
75 |
| - as PHP applies the garbage-collection process to the ``LockHandler`` object. |
76 |
| - This means that if you refactor the first example shown in this article as |
77 |
| - follows:: |
78 |
| - |
79 |
| - use Symfony\Component\Filesystem\LockHandler; |
80 |
| - |
81 |
| - if (!(new LockHandler('hello.lock'))->lock()) { |
82 |
| - // the resource "hello" is already locked by another process |
83 |
| - |
84 |
| - return 0; |
85 |
| - } |
86 |
| - |
87 |
| - Now the code won't work as expected because PHP's garbage collection mechanism |
88 |
| - removes the reference to the ``LockHandler`` object and thus, the lock is released |
89 |
| - just after it's been created. |
90 |
| - |
91 |
| - Another alternative way to release the lock explicitly when needed is to use the |
92 |
| - :method:`Symfony\\Component\\Filesystem\\LockHandler::release` method. |
| 6 | + The ``LockHandler`` utility is deprecated since Symfony 3.4. Use the new |
| 7 | + Symfony Lock component instead. |
0 commit comments