LockHandler.php 3.2 KB

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