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() {}
21 * @return LockManagerGroup
23 public static function singleton() {
24 if ( self
::$instance == null ) {
25 self
::$instance = new self();
26 self
::$instance->initFromGlobals();
28 return self
::$instance;
32 * Destroy the singleton instance, so that a new one will be created next
33 * time singleton() is called.
35 public static function destroySingleton() {
36 self
::$instance = null;
40 * Register lock managers from the global variables
44 protected function initFromGlobals() {
45 global $wgLockManagers;
47 $this->register( $wgLockManagers );
51 * Register an array of file lock manager configurations
53 * @param $configs Array
57 protected function register( array $configs ) {
58 foreach ( $configs as $config ) {
59 if ( !isset( $config['name'] ) ) {
60 throw new MWException( "Cannot register a lock manager with no name." );
62 $name = $config['name'];
63 if ( !isset( $config['class'] ) ) {
64 throw new MWException( "Cannot register lock manager `{$name}` with no class." );
66 $class = $config['class'];
67 unset( $config['class'] ); // lock manager won't need this
68 $this->managers
[$name] = array(
77 * Get the lock manager object with a given name
83 public function get( $name ) {
84 if ( !isset( $this->managers
[$name] ) ) {
85 throw new MWException( "No lock manager defined with the name `$name`." );
87 // Lazy-load the actual lock manager instance
88 if ( !isset( $this->managers
[$name]['instance'] ) ) {
89 $class = $this->managers
[$name]['class'];
90 $config = $this->managers
[$name]['config'];
91 $this->managers
[$name]['instance'] = new $class( $config );
93 return $this->managers
[$name]['instance'];