MediaWiki  master
LockManagerGroup.php
Go to the documentation of this file.
1 <?php
26 
35  protected $domain;
36 
38  protected $lbFactory;
39 
41  protected $managers = [];
42 
50  public function __construct( $domain, array $lockManagerConfigs, LBFactory $lbFactory ) {
51  $this->domain = $domain;
52  $this->lbFactory = $lbFactory;
53 
54  foreach ( $lockManagerConfigs as $config ) {
55  $config['domain'] = $this->domain;
56  if ( !isset( $config['name'] ) ) {
57  throw new Exception( "Cannot register a lock manager with no name." );
58  }
59  $name = $config['name'];
60  if ( !isset( $config['class'] ) ) {
61  throw new Exception( "Cannot register lock manager `{$name}` with no class." );
62  }
63  $class = $config['class'];
64  unset( $config['class'] ); // lock manager won't need this
65  $this->managers[$name] = [
66  'class' => $class,
67  'config' => $config,
68  'instance' => null
69  ];
70  }
71  }
72 
79  public static function singleton( $domain = false ) {
80  return MediaWikiServices::getInstance()->getLockManagerGroupFactory()
81  ->getLockManagerGroup( $domain );
82  }
83 
89  public static function destroySingletons() {
90  MediaWikiServices::getInstance()->resetServiceForTesting( 'LockManagerGroupFactory' );
91  }
92 
100  public function get( $name ) {
101  if ( !isset( $this->managers[$name] ) ) {
102  throw new Exception( "No lock manager defined with the name `$name`." );
103  }
104  // Lazy-load the actual lock manager instance
105  if ( !isset( $this->managers[$name]['instance'] ) ) {
106  $class = $this->managers[$name]['class'];
107  '@phan-var string $class';
108  $config = $this->managers[$name]['config'];
109  $config['logger'] = LoggerFactory::getInstance( 'LockManager' );
110 
111  $this->managers[$name]['instance'] = new $class( $config );
112  }
113 
114  return $this->managers[$name]['instance'];
115  }
116 
124  public function config( $name ) {
125  if ( !isset( $this->managers[$name] ) ) {
126  throw new Exception( "No lock manager defined with the name `$name`." );
127  }
128  $class = $this->managers[$name]['class'];
129 
130  return [ 'class' => $class ] + $this->managers[$name]['config'];
131  }
132 
141  public function getDefault() {
142  wfDeprecated( __METHOD__, '1.35' );
143 
144  return isset( $this->managers['default'] )
145  ? $this->get( 'default' )
146  : new NullLockManager( [] );
147  }
148 
159  public function getAny() {
160  wfDeprecated( __METHOD__, '1.35' );
161 
162  return isset( $this->managers['default'] )
163  ? $this->get( 'default' )
164  : $this->get( 'fsLockManager' );
165  }
166 }
LockManagerGroup\destroySingletons
static destroySingletons()
Destroy the singleton instances.
Definition: LockManagerGroup.php:89
LockManagerGroup\__construct
__construct( $domain, array $lockManagerConfigs, LBFactory $lbFactory)
Do not call this directly.
Definition: LockManagerGroup.php:50
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:149
LockManagerGroup\singleton
static singleton( $domain=false)
Definition: LockManagerGroup.php:79
LockManagerGroup\getAny
getAny()
Get the default lock manager configured for the site or at least some other effective configured lock...
Definition: LockManagerGroup.php:159
LockManagerGroup\$domain
string $domain
domain (usually wiki ID)
Definition: LockManagerGroup.php:35
NullLockManager
Simple version of LockManager that only does lock reference counting.
Definition: NullLockManager.php:28
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1030
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
LockManagerGroup
Class to handle file lock manager registration.
Definition: LockManagerGroup.php:33
LockManagerGroup\$lbFactory
LBFactory $lbFactory
Definition: LockManagerGroup.php:38
LockManagerGroup\config
config( $name)
Get the config array for a lock manager object with a given name.
Definition: LockManagerGroup.php:124
Wikimedia\Rdbms\LBFactory
An interface for generating database load balancers.
Definition: LBFactory.php:41
LockManagerGroup\getDefault
getDefault()
Get the default lock manager configured for the site.
Definition: LockManagerGroup.php:141
LockManagerGroup\$managers
array $managers
Array of (name => ('class' => ..., 'config' => ..., 'instance' => ...))
Definition: LockManagerGroup.php:41