PluginProbe ʕ •ᴥ•ʔ
10Web Booster – Website speed optimization, Cache & Page Speed optimizer / trunk
10Web Booster – Website speed optimization, Cache & Page Speed optimizer vtrunk
2.33.0 2.30.5 2.30.7 2.30.9 2.31.10 2.31.8 2.32.11 2.32.21 2.32.3 2.32.4 2.32.7 2.6.31 2.6.40 2.6.42 2.6.7 2.7.37 2.7.44 2.7.47 2.8.18 2.8.19 2.8.32 2.8.34 2.8.35 2.9.23 2.9.24 2.9.25 2.9.27 v2.27.4 trunk 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.17 2.0.18 2.0.21 2.0.22 2.0.25 2.0.26 2.0.27 2.0.3 2.0.7 2.0.9 2.10.46 2.10.65 2.10.66 2.10.68 2.11.41 2.11.42 2.11.43 2.12.15 2.12.21 2.12.22 2.12.23 2.12.26 2.13.37 2.13.40 2.13.41 2.13.42 2.13.44 2.13.45 2.13.47 2.14.49 2.14.50 2.15.18 2.17.21 2.17.23 2.18.17 2.19.44 2.19.45 2.19.46 2.19.49 2.2.12 2.2.15 2.2.16 2.2.18 2.2.8 2.20.31 2.20.32 2.20.33 2.21.11 2.21.12 2.21.16 2.21.25 2.22.32 2.23.13 2.23.15 2.23.16 2.23.18 2.24.12 2.24.14 2.24.18 2.25.14 2.26.6 2.28.10 2.28.13 2.28.14 2.28.7 2.29.1 2.29.2 2.29.3 2.3.0 2.3.1 2.3.2 2.3.3 2.30.18
tenweb-speed-optimizer / vendor / enqueue / fs / LegacyFilesystemLock.php
tenweb-speed-optimizer / vendor / enqueue / fs Last commit date
.github 2 years ago CannotObtainLockException.php 4 years ago FsConnectionFactory.php 4 years ago FsConsumer.php 4 years ago FsContext.php 4 years ago FsDestination.php 4 years ago FsMessage.php 4 years ago FsProducer.php 4 years ago LICENSE 4 years ago LegacyFilesystemLock.php 4 years ago Lock.php 4 years ago README.md 2 years ago composer.json 2 years ago
LegacyFilesystemLock.php
186 lines
1 <?php
2
3 namespace Enqueue\Fs;
4
5 use Symfony\Component\Filesystem\Exception\IOException;
6 use Symfony\Component\Filesystem\Filesystem;
7
8 class LegacyFilesystemLock implements Lock
9 {
10 /**
11 * Array key is a destination name. String.
12 *
13 * @var LockHandler[]
14 */
15 private $lockHandlers;
16
17 public function __construct()
18 {
19 $this->lockHandlers = [];
20 }
21
22 /**
23 * {@inheritdoc}
24 */
25 public function lock(FsDestination $destination)
26 {
27 $lockHandler = $this->getLockHandler($destination);
28
29 if (false == $lockHandler->lock(true)) {
30 throw new CannotObtainLockException(sprintf('Cannot obtain the lock for destination %s', $destination->getName()));
31 }
32 }
33
34 /**
35 * {@inheritdoc}
36 */
37 public function release(FsDestination $destination)
38 {
39 $lockHandler = $this->getLockHandler($destination);
40
41 $lockHandler->release();
42 }
43
44 public function releaseAll()
45 {
46 foreach ($this->lockHandlers as $lockHandler) {
47 $lockHandler->release();
48 }
49
50 $this->lockHandlers = [];
51 }
52
53 /**
54 * @param FsDestination $destination
55 *
56 * @return LockHandler
57 */
58 private function getLockHandler(FsDestination $destination)
59 {
60 if (false == isset($this->lockHandlers[$destination->getName()])) {
61 $this->lockHandlers[$destination->getName()] = new LockHandler(
62 $destination->getName(),
63 $destination->getFileInfo()->getPath()
64 );
65 }
66
67 return $this->lockHandlers[$destination->getName()];
68 }
69 }
70
71 // symfony/lock component works only with 3.x and 4.x Symfony
72 // For symfony 2.x we should use LockHandler from symfony/component which was removed from 4.x
73 // because we cannot use both at the same time. I copied and pasted the lock handler here
74
75 /*
76 * This file is part of the Symfony package.
77 *
78 * (c) Fabien Potencier <fabien@symfony.com>
79 *
80 * For the full copyright and license information, please view the LICENSE
81 * file that was distributed with this source code.
82 */
83
84 /**
85 * LockHandler class provides a simple abstraction to lock anything by means of
86 * a file lock.
87 *
88 * A locked file is created based on the lock name when calling lock(). Other
89 * lock handlers will not be able to lock the same name until it is released
90 * (explicitly by calling release() or implicitly when the instance holding the
91 * lock is destroyed).
92 *
93 * @author Grégoire Pineau <lyrixx@lyrixx.info>
94 * @author Romain Neutron <imprec@gmail.com>
95 * @author Nicolas Grekas <p@tchwork.com>
96 *
97 * @deprecated since version 3.4, to be removed in 4.0. Use Symfony\Component\Lock\Store\SemaphoreStore or Symfony\Component\Lock\Store\FlockStore instead.
98 */
99 class LockHandler
100 {
101 private $file;
102 private $handle;
103
104 /**
105 * @param string $name The lock name
106 * @param string|null $lockPath The directory to store the lock. Default values will use temporary directory
107 *
108 * @throws IOException If the lock directory could not be created or is not writable
109 */
110 public function __construct($name, $lockPath = null)
111 {
112 $lockPath = $lockPath ?: sys_get_temp_dir();
113
114 if (!is_dir($lockPath)) {
115 $fs = new Filesystem();
116 $fs->mkdir($lockPath);
117 }
118
119 if (!is_writable($lockPath)) {
120 throw new IOException(sprintf('The directory "%s" is not writable.', $lockPath), 0, null, $lockPath);
121 }
122
123 $this->file = sprintf('%s/sf.%s.%s.lock', $lockPath, preg_replace('/[^a-z0-9\._-]+/i', '-', $name), hash('sha256', $name));
124 }
125
126 /**
127 * Lock the resource.
128 *
129 * @param bool $blocking Wait until the lock is released
130 *
131 * @throws IOException If the lock file could not be created or opened
132 *
133 * @return bool Returns true if the lock was acquired, false otherwise
134 */
135 public function lock($blocking = false)
136 {
137 if ($this->handle) {
138 return true;
139 }
140
141 $error = null;
142
143 // Silence error reporting
144 set_error_handler(function ($errno, $msg) use (&$error) {
145 $error = $msg;
146 });
147
148 if (!$this->handle = fopen($this->file, 'r')) {
149 if ($this->handle = fopen($this->file, 'x')) {
150 chmod($this->file, 0444);
151 } elseif (!$this->handle = fopen($this->file, 'r')) {
152 usleep(100); // Give some time for chmod() to complete
153 $this->handle = fopen($this->file, 'r');
154 }
155 }
156 restore_error_handler();
157
158 if (!$this->handle) {
159 throw new IOException($error, 0, null, $this->file);
160 }
161
162 // On Windows, even if PHP doc says the contrary, LOCK_NB works, see
163 // https://bugs.php.net/54129
164 if (!flock($this->handle, LOCK_EX | ($blocking ? 0 : LOCK_NB))) {
165 fclose($this->handle);
166 $this->handle = null;
167
168 return false;
169 }
170
171 return true;
172 }
173
174 /**
175 * Release the resource.
176 */
177 public function release()
178 {
179 if ($this->handle) {
180 flock($this->handle, LOCK_UN | LOCK_NB);
181 fclose($this->handle);
182 $this->handle = null;
183 }
184 }
185 }
186