3 * Class to handle file lock manager registration
9 class LockManagerGroup
{
12 * @var LockManagerGroup
14 protected static $instance = null;
16 /** @var Array of (name => ('class' =>, 'config' =>, 'instance' =>)) */
17 protected $managers = array();
19 protected function __construct() {}
20 protected function __clone() {}
23 * @return LockManagerGroup
25 public static function singleton() {
26 if ( self
::$instance == null ) {
27 self
::$instance = new self();
28 self
::$instance->initFromGlobals();
30 return self
::$instance;
34 * Register lock managers from the global variables
38 protected function initFromGlobals() {
39 global $wgLockManagers;
41 $this->register( $wgLockManagers );
45 * Register an array of file lock manager configurations
47 * @param $configs Array
51 protected function register( array $configs ) {
52 foreach ( $configs as $config ) {
53 if ( !isset( $config['name'] ) ) {
54 throw new MWException( "Cannot register a lock manager with no name." );
56 $name = $config['name'];
57 if ( !isset( $config['class'] ) ) {
58 throw new MWException( "Cannot register lock manager `{$name}` with no class." );
60 $class = $config['class'];
61 unset( $config['class'] ); // lock manager won't need this
62 $this->managers
[$name] = array(
71 * Get the lock manager object with a given name
77 public function get( $name ) {
78 if ( !isset( $this->managers
[$name] ) ) {
79 throw new MWException( "No lock manager defined with the name `$name`." );
81 // Lazy-load the actual lock manager instance
82 if ( !isset( $this->managers
[$name]['instance'] ) ) {
83 $class = $this->managers
[$name]['class'];
84 $config = $this->managers
[$name]['config'];
85 $this->managers
[$name]['instance'] = new $class( $config );
87 return $this->managers
[$name]['instance'];