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