* FU r106752: de-uglified Setup.php by moving most of the b/c code into FileBackendGr...
[lhc/web/wiklou.git] / includes / filerepo / backend / lockmanager / LockManagerGroup.php
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;
10
11 /** @var Array of (name => ('class' =>, 'config' =>, 'instance' =>)) */
12 protected $managers = array();
13
14 protected function __construct() {}
15 protected function __clone() {}
16
17 /**
18 * @return LockManagerGroup
19 */
20 public static function singleton() {
21 if ( self::$instance == null ) {
22 self::$instance = new self();
23 self::$instance->initFromGlobals();
24 }
25 return self::$instance;
26 }
27
28 /**
29 * Register lock managers from the global variables
30 *
31 * @return void
32 */
33 protected function initFromGlobals() {
34 global $wgLockManagers;
35
36 $this->register( $wgLockManagers );
37 }
38
39 /**
40 * Register an array of file lock manager configurations
41 *
42 * @param $configs Array
43 * @return void
44 * @throws MWException
45 */
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." );
50 }
51 $name = $config['name'];
52 if ( !isset( $config['class'] ) ) {
53 throw new MWException( "Cannot register lock manager `{$name}` with no class." );
54 }
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
61 );
62 }
63 }
64
65 /**
66 * Get the lock manager object with a given name
67 *
68 * @param $name string
69 * @return LockManager
70 * @throws MWException
71 */
72 public function get( $name ) {
73 if ( !isset( $this->managers[$name] ) ) {
74 throw new MWException( "No lock manager defined with the name `$name`." );
75 }
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 );
81 }
82 return $this->managers[$name]['instance'];
83 }
84 }