Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / filebackend / lockmanager / LockManagerGroup.php
blobe88667392dc99fe8b0f157de872528ac09fdd8d2
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
23 use MediaWiki\Logger\LoggerFactory;
25 /**
26 * Class to handle file lock manager registration
28 * @ingroup LockManager
29 * @since 1.19
31 class LockManagerGroup {
32 /** @var string domain (usually wiki ID) */
33 protected $domain;
35 /** @var array Array of (name => ('class' => ..., 'config' => ..., 'instance' => ...)) */
36 protected $managers = [];
38 /**
39 * Do not call this directly. Use LockManagerGroupFactory.
41 * @param string $domain Domain (usually wiki ID)
42 * @param array[] $lockManagerConfigs In format of $wgLockManagers
44 public function __construct( $domain, array $lockManagerConfigs ) {
45 $this->domain = $domain;
47 foreach ( $lockManagerConfigs as $config ) {
48 $config['domain'] = $this->domain;
49 if ( !isset( $config['name'] ) ) {
50 throw new InvalidArgumentException( "Cannot register a lock manager with no name." );
52 $name = $config['name'];
53 if ( !isset( $config['class'] ) ) {
54 throw new InvalidArgumentException( "Cannot register lock manager `{$name}` with no class." );
56 $class = $config['class'];
57 unset( $config['class'] ); // lock manager won't need this
58 $this->managers[$name] = [
59 'class' => $class,
60 'config' => $config,
61 'instance' => null
66 /**
67 * Get the lock manager object with a given name
69 * @param string $name
70 * @return LockManager
71 * @throws Exception
73 public function get( $name ) {
74 if ( !isset( $this->managers[$name] ) ) {
75 throw new InvalidArgumentException( "No lock manager defined with the name `$name`." );
77 // Lazy-load the actual lock manager instance
78 if ( !isset( $this->managers[$name]['instance'] ) ) {
79 $class = $this->managers[$name]['class'];
80 '@phan-var string $class';
81 $config = $this->managers[$name]['config'];
82 $config['logger'] = LoggerFactory::getInstance( 'LockManager' );
84 $this->managers[$name]['instance'] = new $class( $config );
87 return $this->managers[$name]['instance'];
90 /**
91 * Get the config array for a lock manager object with a given name
93 * @param string $name
94 * @return array
95 * @throws Exception
97 public function config( $name ) {
98 if ( !isset( $this->managers[$name] ) ) {
99 throw new InvalidArgumentException( "No lock manager defined with the name `$name`." );
101 $class = $this->managers[$name]['class'];
103 return [ 'class' => $class ] + $this->managers[$name]['config'];