[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/symfony/filesystem/ -> LockHandler.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\Filesystem;
  13  
  14  use Symfony\Component\Filesystem\Exception\IOException;
  15  use Symfony\Component\Lock\Store\FlockStore;
  16  use Symfony\Component\Lock\Store\SemaphoreStore;
  17  
  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);
  19  
  20  /**
  21   * LockHandler class provides a simple abstraction to lock anything by means of
  22   * a file lock.
  23   *
  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
  27   * lock is destroyed).
  28   *
  29   * @author Grégoire Pineau <lyrixx@lyrixx.info>
  30   * @author Romain Neutron <imprec@gmail.com>
  31   * @author Nicolas Grekas <p@tchwork.com>
  32   *
  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.
  34   */
  35  class LockHandler
  36  {
  37      private $file;
  38      private $handle;
  39  
  40      /**
  41       * @param string      $name     The lock name
  42       * @param string|null $lockPath The directory to store the lock. Default values will use temporary directory
  43       *
  44       * @throws IOException If the lock directory could not be created or is not writable
  45       */
  46      public function __construct($name, $lockPath = null)
  47      {
  48          $lockPath = $lockPath ?: sys_get_temp_dir();
  49  
  50          if (!is_dir($lockPath)) {
  51              $fs = new Filesystem();
  52              $fs->mkdir($lockPath);
  53          }
  54  
  55          if (!is_writable($lockPath)) {
  56              throw new IOException(sprintf('The directory "%s" is not writable.', $lockPath), 0, null, $lockPath);
  57          }
  58  
  59          $this->file = sprintf('%s/sf.%s.%s.lock', $lockPath, preg_replace('/[^a-z0-9\._-]+/i', '-', $name), hash('sha256', $name));
  60      }
  61  
  62      /**
  63       * Lock the resource.
  64       *
  65       * @param bool $blocking Wait until the lock is released
  66       *
  67       * @return bool Returns true if the lock was acquired, false otherwise
  68       *
  69       * @throws IOException If the lock file could not be created or opened
  70       */
  71      public function lock($blocking = false)
  72      {
  73          if ($this->handle) {
  74              return true;
  75          }
  76  
  77          $error = null;
  78  
  79          // Silence error reporting
  80          set_error_handler(function ($errno, $msg) use (&$error) {
  81              $error = $msg;
  82          });
  83  
  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');
  90              }
  91          }
  92          restore_error_handler();
  93  
  94          if (!$this->handle) {
  95              throw new IOException($error, 0, null, $this->file);
  96          }
  97  
  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;
 103  
 104              return false;
 105          }
 106  
 107          return true;
 108      }
 109  
 110      /**
 111       * Release the resource.
 112       */
 113      public function release()
 114      {
 115          if ($this->handle) {
 116              flock($this->handle, \LOCK_UN | \LOCK_NB);
 117              fclose($this->handle);
 118              $this->handle = null;
 119          }
 120      }
 121  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1