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
21 * @ingroup LockManager
25 * Class to handle file lock manager registration
27 * @ingroup LockManager
28 * @author Aaron Schulz
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();
41 * @param string $domain Domain (usually wiki ID)
43 protected function __construct( $domain ) {
44 $this->domain
= $domain;
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();
58 return self
::$instances[$domain];
62 * Destroy the singleton instances
66 public static function destroySingletons() {
67 self
::$instances = array();
71 * Register lock managers from the global variables
75 protected function initFromGlobals() {
76 global $wgLockManagers;
78 $this->register( $wgLockManagers );
82 * Register an array of file lock manager configurations
84 * @param $configs Array
88 protected function register( array $configs ) {
89 foreach ( $configs as $config ) {
90 $config['domain'] = $this->domain
;
91 if ( !isset( $config['name'] ) ) {
92 throw new MWException( "Cannot register a lock manager with no name." );
94 $name = $config['name'];
95 if ( !isset( $config['class'] ) ) {
96 throw new MWException( "Cannot register lock manager `{$name}` with no class." );
98 $class = $config['class'];
99 unset( $config['class'] ); // lock manager won't need this
100 $this->managers
[$name] = array(
109 * Get the lock manager object with a given name
111 * @param $name string
112 * @return LockManager
113 * @throws MWException
115 public function get( $name ) {
116 if ( !isset( $this->managers
[$name] ) ) {
117 throw new MWException( "No lock manager defined with the name `$name`." );
119 // Lazy-load the actual lock manager instance
120 if ( !isset( $this->managers
[$name]['instance'] ) ) {
121 $class = $this->managers
[$name]['class'];
122 $config = $this->managers
[$name]['config'];
123 $this->managers
[$name]['instance'] = new $class( $config );
126 return $this->managers
[$name]['instance'];
130 * Get the config array for a lock manager object with a given name
132 * @param $name string
134 * @throws MWException
136 public function config( $name ) {
137 if ( !isset( $this->managers
[$name] ) ) {
138 throw new MWException( "No lock manager defined with the name `$name`." );
140 $class = $this->managers
[$name]['class'];
142 return array( 'class' => $class ) +
$this->managers
[$name]['config'];
146 * Get the default lock manager configured for the site.
147 * Returns NullLockManager if no lock manager could be found.
149 * @return LockManager
151 public function getDefault() {
152 return isset( $this->managers
['default'] )
153 ?
$this->get( 'default' )
154 : new NullLockManager( array() );
158 * Get the default lock manager configured for the site
159 * or at least some other effective configured lock manager.
160 * Throws an exception if no lock manager could be found.
162 * @return LockManager
163 * @throws MWException
165 public function getAny() {
166 return isset( $this->managers
['default'] )
167 ?
$this->get( 'default' )
168 : $this->get( 'fsLockManager' );