5e533667e79eb0758ab8a1a2fa9f6cf92f4a0054
[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 * Destroy the singleton instance, so that a new one will be created next
35 * time singleton() is called.
36 */
37 public static function destroySingleton() {
38 self::$instance = null;
39 }
40
41 /**
42 * Register lock managers from the global variables
43 *
44 * @return void
45 */
46 protected function initFromGlobals() {
47 global $wgLockManagers;
48
49 $this->register( $wgLockManagers );
50 }
51
52 /**
53 * Register an array of file lock manager configurations
54 *
55 * @param $configs Array
56 * @return void
57 * @throws MWException
58 */
59 protected function register( array $configs ) {
60 foreach ( $configs as $config ) {
61 if ( !isset( $config['name'] ) ) {
62 throw new MWException( "Cannot register a lock manager with no name." );
63 }
64 $name = $config['name'];
65 if ( !isset( $config['class'] ) ) {
66 throw new MWException( "Cannot register lock manager `{$name}` with no class." );
67 }
68 $class = $config['class'];
69 unset( $config['class'] ); // lock manager won't need this
70 $this->managers[$name] = array(
71 'class' => $class,
72 'config' => $config,
73 'instance' => null
74 );
75 }
76 }
77
78 /**
79 * Get the lock manager object with a given name
80 *
81 * @param $name string
82 * @return LockManager
83 * @throws MWException
84 */
85 public function get( $name ) {
86 if ( !isset( $this->managers[$name] ) ) {
87 throw new MWException( "No lock manager defined with the name `$name`." );
88 }
89 // Lazy-load the actual lock manager instance
90 if ( !isset( $this->managers[$name]['instance'] ) ) {
91 $class = $this->managers[$name]['class'];
92 $config = $this->managers[$name]['config'];
93 $this->managers[$name]['instance'] = new $class( $config );
94 }
95 return $this->managers[$name]['instance'];
96 }
97 }