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
33 protected function initFromGlobals() {
34 global $wgLockManagers;
36 $this->register( $wgLockManagers );
40 * Register an array of file lock manager configurations
42 * @param $configs Array
46 protected function register( array $configs ) {
47 foreach ( $configs as $config ) {
48 if ( !isset( $config['name'] ) ) {
49 throw new MWException( "Cannot register a lock manager with no name." );
51 $name = $config['name'];
52 if ( !isset( $config['class'] ) ) {
53 throw new MWException( "Cannot register lock manager `{$name}` with no class." );
55 $class = $config['class'];
56 unset( $config['class'] ); // lock manager won't need this
57 $this->managers
[$name] = array(
66 * Get the lock manager object with a given name
72 public function get( $name ) {
73 if ( !isset( $this->managers
[$name] ) ) {
74 throw new MWException( "No lock manager defined with the name `$name`." );
76 // Lazy-load the actual lock manager instance
77 if ( !isset( $this->managers
[$name]['instance'] ) ) {
78 $class = $this->managers
[$name]['class'];
79 $config = $this->managers
[$name]['config'];
80 $this->managers
[$name]['instance'] = new $class( $config );
82 return $this->managers
[$name]['instance'];