3 * Class to handle file lock manager registration
9 class LockManagerGroup
{
10 protected static $instance = null;
12 /** @var Array of (name => ('class' =>, 'config' =>, 'instance' =>)) */
13 protected $managers = array();
15 protected function __construct() {}
16 protected function __clone() {}
19 * @return LockManagerGroup
21 public static function singleton() {
22 if ( self
::$instance == null ) {
23 self
::$instance = new self();
24 self
::$instance->initFromGlobals();
26 return self
::$instance;
30 * Register lock managers from the global variables
34 protected function initFromGlobals() {
35 global $wgLockManagers;
37 $this->register( $wgLockManagers );
41 * Register an array of file lock manager configurations
43 * @param $configs Array
47 protected function register( array $configs ) {
48 foreach ( $configs as $config ) {
49 if ( !isset( $config['name'] ) ) {
50 throw new MWException( "Cannot register a lock manager with no name." );
52 $name = $config['name'];
53 if ( !isset( $config['class'] ) ) {
54 throw new MWException( "Cannot register lock manager `{$name}` with no class." );
56 $class = $config['class'];
57 unset( $config['class'] ); // lock manager won't need this
58 $this->managers
[$name] = array(
67 * Get the lock manager object with a given name
73 public function get( $name ) {
74 if ( !isset( $this->managers
[$name] ) ) {
75 throw new MWException( "No lock manager defined with the name `$name`." );
77 // Lazy-load the actual lock manager instance
78 if ( !isset( $this->managers
[$name]['instance'] ) ) {
79 $class = $this->managers
[$name]['class'];
80 $config = $this->managers
[$name]['config'];
81 $this->managers
[$name]['instance'] = new $class( $config );
83 return $this->managers
[$name]['instance'];