Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Filesystem;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Filesystem\Exception\IOException;
|
Chris@0
|
15 use Symfony\Component\Lock\Store\FlockStore;
|
Chris@0
|
16 use Symfony\Component\Lock\Store\SemaphoreStore;
|
Chris@0
|
17
|
Chris@0
|
18 @trigger_error(sprintf('The %s class is deprecated since Symfony 3.4 and will be removed in 4.0. Use %s or %s instead.', LockHandler::class, SemaphoreStore::class, FlockStore::class), E_USER_DEPRECATED);
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * LockHandler class provides a simple abstraction to lock anything by means of
|
Chris@0
|
22 * a file lock.
|
Chris@0
|
23 *
|
Chris@0
|
24 * A locked file is created based on the lock name when calling lock(). Other
|
Chris@0
|
25 * lock handlers will not be able to lock the same name until it is released
|
Chris@0
|
26 * (explicitly by calling release() or implicitly when the instance holding the
|
Chris@0
|
27 * lock is destroyed).
|
Chris@0
|
28 *
|
Chris@0
|
29 * @author Grégoire Pineau <lyrixx@lyrixx.info>
|
Chris@0
|
30 * @author Romain Neutron <imprec@gmail.com>
|
Chris@0
|
31 * @author Nicolas Grekas <p@tchwork.com>
|
Chris@0
|
32 *
|
Chris@0
|
33 * @deprecated since version 3.4, to be removed in 4.0. Use Symfony\Component\Lock\Store\SemaphoreStore or Symfony\Component\Lock\Store\FlockStore instead.
|
Chris@0
|
34 */
|
Chris@0
|
35 class LockHandler
|
Chris@0
|
36 {
|
Chris@0
|
37 private $file;
|
Chris@0
|
38 private $handle;
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * @param string $name The lock name
|
Chris@0
|
42 * @param string|null $lockPath The directory to store the lock. Default values will use temporary directory
|
Chris@0
|
43 *
|
Chris@0
|
44 * @throws IOException If the lock directory could not be created or is not writable
|
Chris@0
|
45 */
|
Chris@0
|
46 public function __construct($name, $lockPath = null)
|
Chris@0
|
47 {
|
Chris@0
|
48 $lockPath = $lockPath ?: sys_get_temp_dir();
|
Chris@0
|
49
|
Chris@0
|
50 if (!is_dir($lockPath)) {
|
Chris@0
|
51 $fs = new Filesystem();
|
Chris@0
|
52 $fs->mkdir($lockPath);
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 if (!is_writable($lockPath)) {
|
Chris@0
|
56 throw new IOException(sprintf('The directory "%s" is not writable.', $lockPath), 0, null, $lockPath);
|
Chris@0
|
57 }
|
Chris@0
|
58
|
Chris@0
|
59 $this->file = sprintf('%s/sf.%s.%s.lock', $lockPath, preg_replace('/[^a-z0-9\._-]+/i', '-', $name), hash('sha256', $name));
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * Lock the resource.
|
Chris@0
|
64 *
|
Chris@0
|
65 * @param bool $blocking Wait until the lock is released
|
Chris@0
|
66 *
|
Chris@0
|
67 * @return bool Returns true if the lock was acquired, false otherwise
|
Chris@0
|
68 *
|
Chris@0
|
69 * @throws IOException If the lock file could not be created or opened
|
Chris@0
|
70 */
|
Chris@0
|
71 public function lock($blocking = false)
|
Chris@0
|
72 {
|
Chris@0
|
73 if ($this->handle) {
|
Chris@0
|
74 return true;
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 $error = null;
|
Chris@0
|
78
|
Chris@0
|
79 // Silence error reporting
|
Chris@0
|
80 set_error_handler(function ($errno, $msg) use (&$error) {
|
Chris@0
|
81 $error = $msg;
|
Chris@0
|
82 });
|
Chris@0
|
83
|
Chris@0
|
84 if (!$this->handle = fopen($this->file, 'r+') ?: fopen($this->file, 'r')) {
|
Chris@0
|
85 if ($this->handle = fopen($this->file, 'x')) {
|
Chris@4
|
86 chmod($this->file, 0666);
|
Chris@0
|
87 } elseif (!$this->handle = fopen($this->file, 'r+') ?: fopen($this->file, 'r')) {
|
Chris@0
|
88 usleep(100); // Give some time for chmod() to complete
|
Chris@0
|
89 $this->handle = fopen($this->file, 'r+') ?: fopen($this->file, 'r');
|
Chris@0
|
90 }
|
Chris@0
|
91 }
|
Chris@0
|
92 restore_error_handler();
|
Chris@0
|
93
|
Chris@0
|
94 if (!$this->handle) {
|
Chris@0
|
95 throw new IOException($error, 0, null, $this->file);
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 // On Windows, even if PHP doc says the contrary, LOCK_NB works, see
|
Chris@0
|
99 // https://bugs.php.net/54129
|
Chris@0
|
100 if (!flock($this->handle, LOCK_EX | ($blocking ? 0 : LOCK_NB))) {
|
Chris@0
|
101 fclose($this->handle);
|
Chris@0
|
102 $this->handle = null;
|
Chris@0
|
103
|
Chris@0
|
104 return false;
|
Chris@0
|
105 }
|
Chris@0
|
106
|
Chris@0
|
107 return true;
|
Chris@0
|
108 }
|
Chris@0
|
109
|
Chris@0
|
110 /**
|
Chris@0
|
111 * Release the resource.
|
Chris@0
|
112 */
|
Chris@0
|
113 public function release()
|
Chris@0
|
114 {
|
Chris@0
|
115 if ($this->handle) {
|
Chris@0
|
116 flock($this->handle, LOCK_UN | LOCK_NB);
|
Chris@0
|
117 fclose($this->handle);
|
Chris@0
|
118 $this->handle = null;
|
Chris@0
|
119 }
|
Chris@0
|
120 }
|
Chris@0
|
121 }
|