Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / filerepo / backend / lockmanager / LockManagerGroup.php
blobb8308556c954bc22fcce60c990460d1ddc56c51a
1 <?php
2 /**
3 * Lock manager registration handling.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup LockManager
24 /**
25 * Class to handle file lock manager registration
27 * @ingroup LockManager
28 * @author Aaron Schulz
29 * @since 1.19
31 class LockManagerGroup {
33 /**
34 * @var LockManagerGroup
36 protected static $instance = null;
38 /** @var Array of (name => ('class' =>, 'config' =>, 'instance' =>)) */
39 protected $managers = array();
41 protected function __construct() {}
43 /**
44 * @return LockManagerGroup
46 public static function singleton() {
47 if ( self::$instance == null ) {
48 self::$instance = new self();
49 self::$instance->initFromGlobals();
51 return self::$instance;
54 /**
55 * Destroy the singleton instance, so that a new one will be created next
56 * time singleton() is called.
58 public static function destroySingleton() {
59 self::$instance = null;
62 /**
63 * Register lock managers from the global variables
65 * @return void
67 protected function initFromGlobals() {
68 global $wgLockManagers;
70 $this->register( $wgLockManagers );
73 /**
74 * Register an array of file lock manager configurations
76 * @param $configs Array
77 * @return void
78 * @throws MWException
80 protected function register( array $configs ) {
81 foreach ( $configs as $config ) {
82 if ( !isset( $config['name'] ) ) {
83 throw new MWException( "Cannot register a lock manager with no name." );
85 $name = $config['name'];
86 if ( !isset( $config['class'] ) ) {
87 throw new MWException( "Cannot register lock manager `{$name}` with no class." );
89 $class = $config['class'];
90 unset( $config['class'] ); // lock manager won't need this
91 $this->managers[$name] = array(
92 'class' => $class,
93 'config' => $config,
94 'instance' => null
99 /**
100 * Get the lock manager object with a given name
102 * @param $name string
103 * @return LockManager
104 * @throws MWException
106 public function get( $name ) {
107 if ( !isset( $this->managers[$name] ) ) {
108 throw new MWException( "No lock manager defined with the name `$name`." );
110 // Lazy-load the actual lock manager instance
111 if ( !isset( $this->managers[$name]['instance'] ) ) {
112 $class = $this->managers[$name]['class'];
113 $config = $this->managers[$name]['config'];
114 $this->managers[$name]['instance'] = new $class( $config );
116 return $this->managers[$name]['instance'];