Simplify the check to make it more understandable
[mediawiki.git] / includes / filerepo / backend / lockmanager / LockManagerGroup.php
blobe501a0f6161be83791d911ba0ef1ee89d5de5d19
1 <?php
2 /**
3 * Class to handle file lock manager registration
4 *
5 * @ingroup LockManager
6 * @author Aaron Schulz
7 */
8 class LockManagerGroup {
9 protected static $instance = null;
11 /** @var Array of (name => ('class' =>, 'config' =>, 'instance' =>)) */
12 protected $managers = array();
14 protected function __construct() {}
15 protected function __clone() {}
17 /**
18 * @return LockManagerGroup
20 public static function singleton() {
21 if ( self::$instance == null ) {
22 self::$instance = new self();
23 self::$instance->initFromGlobals();
25 return self::$instance;
28 /**
29 * Register lock managers from the global variables
31 * @return void
33 protected function initFromGlobals() {
34 global $wgLockManagers;
36 $this->register( $wgLockManagers );
39 /**
40 * Register an array of file lock manager configurations
42 * @param $configs Array
43 * @return void
44 * @throws MWException
46 protected function register( array $configs ) {
47 foreach ( $configs as $config ) {
48 if ( !isset( $config['name'] ) ) {
49 throw new MWException( "Cannot register a lock manager with no name." );
51 $name = $config['name'];
52 if ( !isset( $config['class'] ) ) {
53 throw new MWException( "Cannot register lock manager `{$name}` with no class." );
55 $class = $config['class'];
56 unset( $config['class'] ); // lock manager won't need this
57 $this->managers[$name] = array(
58 'class' => $class,
59 'config' => $config,
60 'instance' => null
65 /**
66 * Get the lock manager object with a given name
68 * @param $name string
69 * @return LockManager
70 * @throws MWException
72 public function get( $name ) {
73 if ( !isset( $this->managers[$name] ) ) {
74 throw new MWException( "No lock manager defined with the name `$name`." );
76 // Lazy-load the actual lock manager instance
77 if ( !isset( $this->managers[$name]['instance'] ) ) {
78 $class = $this->managers[$name]['class'];
79 $config = $this->managers[$name]['config'];
80 $this->managers[$name]['instance'] = new $class( $config );
82 return $this->managers[$name]['instance'];