LockHandler.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. $error = null;
  62. // Silence error reporting
  63. set_error_handler(function ($errno, $msg) use (&$error) {
  64. $error = $msg;
  65. });
  66. if (!$this->handle = fopen($this->file, 'r+') ?: fopen($this->file, 'r')) {
  67. if ($this->handle = fopen($this->file, 'x')) {
  68. chmod($this->file, 0666);
  69. } elseif (!$this->handle = fopen($this->file, 'r+') ?: fopen($this->file, 'r')) {
  70. usleep(100); // Give some time for chmod() to complete
  71. $this->handle = fopen($this->file, 'r+') ?: fopen($this->file, 'r');
  72. }
  73. }
  74. restore_error_handler();
  75. if (!$this->handle) {
  76. throw new IOException($error, 0, null, $this->file);
  77. }
  78. // On Windows, even if PHP doc says the contrary, LOCK_NB works, see
  79. // https://bugs.php.net/54129
  80. if (!flock($this->handle, LOCK_EX | ($blocking ? 0 : LOCK_NB))) {
  81. fclose($this->handle);
  82. $this->handle = null;
  83. return false;
  84. }
  85. return true;
  86. }
  87. /**
  88. * Release the resource.
  89. */
  90. public function release()
  91. {
  92. if ($this->handle) {
  93. flock($this->handle, LOCK_UN | LOCK_NB);
  94. fclose($this->handle);
  95. $this->handle = null;
  96. }
  97. }
  98. }