In FileBackend:
[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 * @since 1.19
8 */
9 class LockManagerGroup {
10 protected static $instance = null;
11
12 /** @var Array of (name => ('class' =>, 'config' =>, 'instance' =>)) */
13 protected $managers = array();
14
15 protected function __construct() {}
16 protected function __clone() {}
17
18 /**
19 * @return LockManagerGroup
20 */
21 public static function singleton() {
22 if ( self::$instance == null ) {
23 self::$instance = new self();
24 self::$instance->initFromGlobals();
25 }
26 return self::$instance;
27 }
28
29 /**
30 * Register lock managers from the global variables
31 *
32 * @return void
33 */
34 protected function initFromGlobals() {
35 global $wgLockManagers;
36
37 $this->register( $wgLockManagers );
38 }
39
40 /**
41 * Register an array of file lock manager configurations
42 *
43 * @param $configs Array
44 * @return void
45 * @throws MWException
46 */
47 protected function register( array $configs ) {
48 foreach ( $configs as $config ) {
49 if ( !isset( $config['name'] ) ) {
50 throw new MWException( "Cannot register a lock manager with no name." );
51 }
52 $name = $config['name'];
53 if ( !isset( $config['class'] ) ) {
54 throw new MWException( "Cannot register lock manager `{$name}` with no class." );
55 }
56 $class = $config['class'];
57 unset( $config['class'] ); // lock manager won't need this
58 $this->managers[$name] = array(
59 'class' => $class,
60 'config' => $config,
61 'instance' => null
62 );
63 }
64 }
65
66 /**
67 * Get the lock manager object with a given name
68 *
69 * @param $name string
70 * @return LockManager
71 * @throws MWException
72 */
73 public function get( $name ) {
74 if ( !isset( $this->managers[$name] ) ) {
75 throw new MWException( "No lock manager defined with the name `$name`." );
76 }
77 // Lazy-load the actual lock manager instance
78 if ( !isset( $this->managers[$name]['instance'] ) ) {
79 $class = $this->managers[$name]['class'];
80 $config = $this->managers[$name]['config'];
81 $this->managers[$name]['instance'] = new $class( $config );
82 }
83 return $this->managers[$name]['instance'];
84 }
85 }