3 * Class to handle file lock manager registration
8 class LockManagerGroup
{
9 protected static $instance = null;
11 /** @var Array of (name => ('class' =>, 'config' =>, 'instance' =>)) */
12 protected $managers = array();
14 protected function __construct() {}
15 protected function __clone() {}
18 * @return LockManagerGroup
20 public static function singleton() {
21 if ( self
::$instance == null ) {
22 self
::$instance = new self();
23 self
::$instance->initFromGlobals();
25 return self
::$instance;
29 * Register lock managers from the global variables
31 protected function initFromGlobals() {
32 global $wgLockManagers;
34 $this->register( $wgLockManagers );
38 * Register an array of file lock manager configurations
40 * @param $configs Array
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." );
48 $name = $config['name'];
49 if ( !isset( $config['class'] ) ) {
50 throw new MWException( "Cannot register lock manager `{$name}` with no class." );
52 $class = $config['class'];
53 unset( $config['class'] ); // lock manager won't need this
54 $this->managers
[$name] = array(
63 * Get the lock manager object with a given name
69 public function get( $name ) {
70 if ( !isset( $this->managers
[$name] ) ) {
71 throw new MWException( "No lock manager defined with the name `$name`." );
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 );
79 return $this->managers
[$name]['instance'];