Merge "Typo fix"
[mediawiki.git] / includes / filebackend / lockmanager / LockManagerGroup.php
blob9aff241528b9ded3e5319fb600dcb2f4c245573b
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 {
32 /** @var Array (domain => LockManager) */
33 protected static $instances = array();
35 protected $domain; // string; domain (usually wiki ID)
37 /** @var Array of (name => ('class' => ..., 'config' => ..., 'instance' => ...)) */
38 protected $managers = array();
40 /**
41 * @param string $domain Domain (usually wiki ID)
43 protected function __construct( $domain ) {
44 $this->domain = $domain;
47 /**
48 * @param string $domain Domain (usually wiki ID)
49 * @return LockManagerGroup
51 public static function singleton( $domain = false ) {
52 $domain = ( $domain === false ) ? wfWikiID() : $domain;
53 if ( !isset( self::$instances[$domain] ) ) {
54 self::$instances[$domain] = new self( $domain );
55 self::$instances[$domain]->initFromGlobals();
57 return self::$instances[$domain];
60 /**
61 * Destroy the singleton instances
63 * @return void
65 public static function destroySingletons() {
66 self::$instances = array();
69 /**
70 * Register lock managers from the global variables
72 * @return void
74 protected function initFromGlobals() {
75 global $wgLockManagers;
77 $this->register( $wgLockManagers );
80 /**
81 * Register an array of file lock manager configurations
83 * @param $configs Array
84 * @return void
85 * @throws MWException
87 protected function register( array $configs ) {
88 foreach ( $configs as $config ) {
89 $config['domain'] = $this->domain;
90 if ( !isset( $config['name'] ) ) {
91 throw new MWException( "Cannot register a lock manager with no name." );
93 $name = $config['name'];
94 if ( !isset( $config['class'] ) ) {
95 throw new MWException( "Cannot register lock manager `{$name}` with no class." );
97 $class = $config['class'];
98 unset( $config['class'] ); // lock manager won't need this
99 $this->managers[$name] = array(
100 'class' => $class,
101 'config' => $config,
102 'instance' => null
108 * Get the lock manager object with a given name
110 * @param $name string
111 * @return LockManager
112 * @throws MWException
114 public function get( $name ) {
115 if ( !isset( $this->managers[$name] ) ) {
116 throw new MWException( "No lock manager defined with the name `$name`." );
118 // Lazy-load the actual lock manager instance
119 if ( !isset( $this->managers[$name]['instance'] ) ) {
120 $class = $this->managers[$name]['class'];
121 $config = $this->managers[$name]['config'];
122 $this->managers[$name]['instance'] = new $class( $config );
124 return $this->managers[$name]['instance'];
128 * Get the config array for a lock manager object with a given name
130 * @param $name string
131 * @return Array
132 * @throws MWException
134 public function config( $name ) {
135 if ( !isset( $this->managers[$name] ) ) {
136 throw new MWException( "No lock manager defined with the name `$name`." );
138 $class = $this->managers[$name]['class'];
139 return array( 'class' => $class ) + $this->managers[$name]['config'];
143 * Get the default lock manager configured for the site.
144 * Returns NullLockManager if no lock manager could be found.
146 * @return LockManager
148 public function getDefault() {
149 return isset( $this->managers['default'] )
150 ? $this->get( 'default' )
151 : new NullLockManager( array() );
155 * Get the default lock manager configured for the site
156 * or at least some other effective configured lock manager.
157 * Throws an exception if no lock manager could be found.
159 * @return LockManager
160 * @throws MWException
162 public function getAny() {
163 return isset( $this->managers['default'] )
164 ? $this->get( 'default' )
165 : $this->get( 'fsLockManager' );