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