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