4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Filesystem;
14 use Symfony\Component\Filesystem\Exception\IOException;
15 use Symfony\Component\Lock\Store\FlockStore;
16 use Symfony\Component\Lock\Store\SemaphoreStore;
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);
21 * LockHandler class provides a simple abstraction to lock anything by means of
24 * A locked file is created based on the lock name when calling lock(). Other
25 * lock handlers will not be able to lock the same name until it is released
26 * (explicitly by calling release() or implicitly when the instance holding the
29 * @author Grégoire Pineau <lyrixx@lyrixx.info>
30 * @author Romain Neutron <imprec@gmail.com>
31 * @author Nicolas Grekas <p@tchwork.com>
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.
41 * @param string $name The lock name
42 * @param string|null $lockPath The directory to store the lock. Default values will use temporary directory
44 * @throws IOException If the lock directory could not be created or is not writable
46 public function __construct($name, $lockPath = null)
48 $lockPath = $lockPath ?: sys_get_temp_dir();
50 if (!is_dir($lockPath)) {
51 $fs = new Filesystem();
52 $fs->mkdir($lockPath);
55 if (!is_writable($lockPath)) {
56 throw new IOException(sprintf('The directory "%s" is not writable.', $lockPath), 0, null, $lockPath);
59 $this->file = sprintf('%s/sf.%s.%s.lock', $lockPath, preg_replace('/[^a-z0-9\._-]+/i', '-', $name), hash('sha256', $name));
65 * @param bool $blocking Wait until the lock is released
67 * @return bool Returns true if the lock was acquired, false otherwise
69 * @throws IOException If the lock file could not be created or opened
71 public function lock($blocking = false)
79 // Silence error reporting
80 set_error_handler(function ($errno, $msg) use (&$error) {
84 if (!$this->handle = fopen($this->file, 'r+') ?: fopen($this->file, 'r')) {
85 if ($this->handle = fopen($this->file, 'x')) {
86 chmod($this->file, 0666);
87 } elseif (!$this->handle = fopen($this->file, 'r+') ?: fopen($this->file, 'r')) {
88 usleep(100); // Give some time for chmod() to complete
89 $this->handle = fopen($this->file, 'r+') ?: fopen($this->file, 'r');
92 restore_error_handler();
95 throw new IOException($error, 0, null, $this->file);
98 // On Windows, even if PHP doc says the contrary, LOCK_NB works, see
99 // https://bugs.php.net/54129
100 if (!flock($this->handle, LOCK_EX | ($blocking ? 0 : LOCK_NB))) {
101 fclose($this->handle);
102 $this->handle = null;
111 * Release the resource.
113 public function release()
116 flock($this->handle, LOCK_UN | LOCK_NB);
117 fclose($this->handle);
118 $this->handle = null;