Kill "* @return void"
[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 protected function initFromGlobals() {
32 global $wgLockManagers;
33
34 $this->register( $wgLockManagers );
35 }
36
37 /**
38 * Register an array of file lock manager configurations
39 *
40 * @param $configs Array
41 * @throws MWException
42 */
43 protected function register( array $configs ) {
44 foreach ( $configs as $config ) {
45 if ( !isset( $config['name'] ) ) {
46 throw new MWException( "Cannot register a lock manager with no name." );
47 }
48 $name = $config['name'];
49 if ( !isset( $config['class'] ) ) {
50 throw new MWException( "Cannot register lock manager `{$name}` with no class." );
51 }
52 $class = $config['class'];
53 unset( $config['class'] ); // lock manager won't need this
54 $this->managers[$name] = array(
55 'class' => $class,
56 'config' => $config,
57 'instance' => null
58 );
59 }
60 }
61
62 /**
63 * Get the lock manager object with a given name
64 *
65 * @param $name string
66 * @return LockManager
67 * @throws MWException
68 */
69 public function get( $name ) {
70 if ( !isset( $this->managers[$name] ) ) {
71 throw new MWException( "No lock manager defined with the name `$name`." );
72 }
73 // Lazy-load the actual lock manager instance
74 if ( !isset( $this->managers[$name]['instance'] ) ) {
75 $class = $this->managers[$name]['class'];
76 $config = $this->managers[$name]['config'];
77 $this->managers[$name]['instance'] = new $class( $config );
78 }
79 return $this->managers[$name]['instance'];
80 }
81 }