[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/classes/lock/ -> lock.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Class representing a lock
  19   *
  20   * The methods available for a specific lock type are only known by it's factory.
  21   *
  22   * @package    core
  23   * @copyright  Damyon Wiese 2013
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  namespace core\lock;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  /**
  32   * Class representing a lock
  33   *
  34   * The methods available for a specific lock type are only known by it's factory.
  35   *
  36   * @package   core
  37   * @category  lock
  38   * @copyright Damyon Wiese 2013
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class lock {
  42  
  43      /** @var string|int $key A unique key representing a held lock */
  44      protected $key = '';
  45  
  46      /** @var lock_factory $factory The factory that generated this lock */
  47      protected $factory;
  48  
  49      /** @var bool $released Has this lock been released? If a lock falls out of scope without being released - show a warning. */
  50      protected $released;
  51  
  52      /**
  53       * Construct a lock containing the unique key required to release it.
  54       * @param mixed $key - The lock key. The type of this is up to the lock_factory being used.
  55       *      For file locks this is a file handle. For MySQL this is a string.
  56       * @param lock_factory $factory - The factory that generated this lock.
  57       */
  58      public function __construct($key, $factory) {
  59          $this->factory = $factory;
  60          $this->key = $key;
  61          $this->released = false;
  62      }
  63  
  64      /**
  65       * Return the unique key representing this lock.
  66       * @return string|int lock key.
  67       */
  68      public function get_key() {
  69          return $this->key;
  70      }
  71  
  72      /**
  73       * Extend the lifetime of this lock. Not supported by all factories.
  74       * @param int $maxlifetime - the new lifetime for the lock (in seconds).
  75       * @return bool
  76       */
  77      public function extend($maxlifetime = 86400) {
  78          if ($this->factory) {
  79              return $this->factory->extend_lock($this, $maxlifetime);
  80          }
  81          return false;
  82      }
  83  
  84      /**
  85       * Release this lock
  86       * @return bool
  87       */
  88      public function release() {
  89          $this->released = true;
  90          if (empty($this->factory)) {
  91              return false;
  92          }
  93          $result = $this->factory->release_lock($this);
  94          // Release any held references to the factory.
  95          unset($this->factory);
  96          $this->factory = null;
  97          $this->key = '';
  98          return $result;
  99      }
 100  
 101      /**
 102       * Print debugging if this lock falls out of scope before being released.
 103       */
 104      public function __destruct() {
 105          if (!$this->released && defined('PHPUNIT_TEST')) {
 106              $this->release();
 107              throw new \coding_exception('\core\lock\lock(' . $this->key . ') has fallen out of scope ' .
 108                                          'without being released.' . "\n" .
 109                                          'Locks must ALWAYS be released by calling $mylock->release().');
 110          }
 111      }
 112  
 113  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1