Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Filesystem; Chris@0: Chris@0: use Symfony\Component\Filesystem\Exception\IOException; Chris@0: use Symfony\Component\Lock\Store\FlockStore; Chris@0: use Symfony\Component\Lock\Store\SemaphoreStore; Chris@0: Chris@0: @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: Chris@0: /** Chris@0: * LockHandler class provides a simple abstraction to lock anything by means of Chris@0: * a file lock. Chris@0: * Chris@0: * A locked file is created based on the lock name when calling lock(). Other Chris@0: * lock handlers will not be able to lock the same name until it is released Chris@0: * (explicitly by calling release() or implicitly when the instance holding the Chris@0: * lock is destroyed). Chris@0: * Chris@0: * @author Grégoire Pineau Chris@0: * @author Romain Neutron Chris@0: * @author Nicolas Grekas Chris@0: * Chris@0: * @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: */ Chris@0: class LockHandler Chris@0: { Chris@0: private $file; Chris@0: private $handle; Chris@0: Chris@0: /** Chris@0: * @param string $name The lock name Chris@0: * @param string|null $lockPath The directory to store the lock. Default values will use temporary directory Chris@0: * Chris@0: * @throws IOException If the lock directory could not be created or is not writable Chris@0: */ Chris@0: public function __construct($name, $lockPath = null) Chris@0: { Chris@0: $lockPath = $lockPath ?: sys_get_temp_dir(); Chris@0: Chris@0: if (!is_dir($lockPath)) { Chris@0: $fs = new Filesystem(); Chris@0: $fs->mkdir($lockPath); Chris@0: } Chris@0: Chris@0: if (!is_writable($lockPath)) { Chris@0: throw new IOException(sprintf('The directory "%s" is not writable.', $lockPath), 0, null, $lockPath); Chris@0: } Chris@0: Chris@0: $this->file = sprintf('%s/sf.%s.%s.lock', $lockPath, preg_replace('/[^a-z0-9\._-]+/i', '-', $name), hash('sha256', $name)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Lock the resource. Chris@0: * Chris@0: * @param bool $blocking Wait until the lock is released Chris@0: * Chris@0: * @return bool Returns true if the lock was acquired, false otherwise Chris@0: * Chris@0: * @throws IOException If the lock file could not be created or opened Chris@0: */ Chris@0: public function lock($blocking = false) Chris@0: { Chris@0: if ($this->handle) { Chris@0: return true; Chris@0: } Chris@0: Chris@0: $error = null; Chris@0: Chris@0: // Silence error reporting Chris@0: set_error_handler(function ($errno, $msg) use (&$error) { Chris@0: $error = $msg; Chris@0: }); Chris@0: Chris@0: if (!$this->handle = fopen($this->file, 'r+') ?: fopen($this->file, 'r')) { Chris@0: if ($this->handle = fopen($this->file, 'x')) { Chris@0: chmod($this->file, 0444); Chris@0: } elseif (!$this->handle = fopen($this->file, 'r+') ?: fopen($this->file, 'r')) { Chris@0: usleep(100); // Give some time for chmod() to complete Chris@0: $this->handle = fopen($this->file, 'r+') ?: fopen($this->file, 'r'); Chris@0: } Chris@0: } Chris@0: restore_error_handler(); Chris@0: Chris@0: if (!$this->handle) { Chris@0: throw new IOException($error, 0, null, $this->file); Chris@0: } Chris@0: Chris@0: // On Windows, even if PHP doc says the contrary, LOCK_NB works, see Chris@0: // https://bugs.php.net/54129 Chris@0: if (!flock($this->handle, LOCK_EX | ($blocking ? 0 : LOCK_NB))) { Chris@0: fclose($this->handle); Chris@0: $this->handle = null; Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Release the resource. Chris@0: */ Chris@0: public function release() Chris@0: { Chris@0: if ($this->handle) { Chris@0: flock($this->handle, LOCK_UN | LOCK_NB); Chris@0: fclose($this->handle); Chris@0: $this->handle = null; Chris@0: } Chris@0: } Chris@0: }