| 1 | efrain | 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 | namespace core\lock;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | use coding_exception;
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 | /**
 | 
        
           |  |  | 22 |  * Lock factory for use during installation.
 | 
        
           |  |  | 23 |  *
 | 
        
           |  |  | 24 |  * @package   core
 | 
        
           |  |  | 25 |  * @category  lock
 | 
        
           |  |  | 26 |  * @copyright  Andrew Nicols <andrew@nicols.co.uk>
 | 
        
           |  |  | 27 |  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 28 |  */
 | 
        
           |  |  | 29 | class installation_lock_factory implements lock_factory {
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 |     /**
 | 
        
           |  |  | 32 |      * Create this lock factory.
 | 
        
           |  |  | 33 |      *
 | 
        
           |  |  | 34 |      * @param string $type - The type, e.g. cron, cache, session
 | 
        
           |  |  | 35 |      */
 | 
        
           |  |  | 36 |     public function __construct($type) {
 | 
        
           |  |  | 37 |     }
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 |     /**
 | 
        
           |  |  | 40 |      * Return information about the blocking behaviour of the lock type on this platform.
 | 
        
           |  |  | 41 |      *
 | 
        
           |  |  | 42 |      * @return boolean - False if attempting to get a lock will block indefinitely.
 | 
        
           |  |  | 43 |      */
 | 
        
           |  |  | 44 |     public function supports_timeout() {
 | 
        
           |  |  | 45 |         return true;
 | 
        
           |  |  | 46 |     }
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 |     /**
 | 
        
           |  |  | 49 |      * This lock type will be automatically released when a process ends.
 | 
        
           |  |  | 50 |      *
 | 
        
           |  |  | 51 |      * @return boolean - True
 | 
        
           |  |  | 52 |      */
 | 
        
           |  |  | 53 |     public function supports_auto_release() {
 | 
        
           |  |  | 54 |         return true;
 | 
        
           |  |  | 55 |     }
 | 
        
           |  |  | 56 |   | 
        
           |  |  | 57 |     /**
 | 
        
           |  |  | 58 |      * This lock factory is only available during the initial installation.
 | 
        
           |  |  | 59 |      * To use it at any other time would be potentially dangerous.
 | 
        
           |  |  | 60 |      *
 | 
        
           |  |  | 61 |      * @return boolean
 | 
        
           |  |  | 62 |      */
 | 
        
           |  |  | 63 |     public function is_available() {
 | 
        
           |  |  | 64 |         return during_initial_install();
 | 
        
           |  |  | 65 |     }
 | 
        
           |  |  | 66 |   | 
        
           |  |  | 67 |     /**
 | 
        
           |  |  | 68 |      * Get some info that might be useful for debugging.
 | 
        
           |  |  | 69 |      * @return boolean - string
 | 
        
           |  |  | 70 |      */
 | 
        
           |  |  | 71 |     protected function get_debug_info() {
 | 
        
           |  |  | 72 |         return 'host:' . php_uname('n') . ', pid:' . getmypid() . ', time:' . time();
 | 
        
           |  |  | 73 |     }
 | 
        
           |  |  | 74 |   | 
        
           |  |  | 75 |     /**
 | 
        
           |  |  | 76 |      * Get a lock within the specified timeout or return false.
 | 
        
           |  |  | 77 |      *
 | 
        
           |  |  | 78 |      * @param string $resource - The identifier for the lock. Should use frankenstyle prefix.
 | 
        
           |  |  | 79 |      * @param int $timeout - The number of seconds to wait for a lock before giving up.
 | 
        
           |  |  | 80 |      * @param int $maxlifetime - Unused by this lock type.
 | 
        
           |  |  | 81 |      * @return boolean - true if a lock was obtained.
 | 
        
           |  |  | 82 |      */
 | 
        
           |  |  | 83 |     public function get_lock($resource, $timeout, $maxlifetime = 86400) {
 | 
        
           |  |  | 84 |         return new lock($resource, $this);
 | 
        
           |  |  | 85 |     }
 | 
        
           |  |  | 86 |   | 
        
           |  |  | 87 |     /**
 | 
        
           |  |  | 88 |      * Release a lock that was previously obtained with @lock.
 | 
        
           |  |  | 89 |      *
 | 
        
           |  |  | 90 |      * @param lock $lock - A lock obtained from this factory.
 | 
        
           |  |  | 91 |      * @return boolean - true if the lock is no longer held (including if it was never held).
 | 
        
           |  |  | 92 |      */
 | 
        
           |  |  | 93 |     public function release_lock(lock $lock) {
 | 
        
           |  |  | 94 |         return true;
 | 
        
           |  |  | 95 |     }
 | 
        
           |  |  | 96 | }
 |