MediaWiki  REL1_24
LockManagerGroup.php
Go to the documentation of this file.
00001 <?php
00031 class LockManagerGroup {
00033     protected static $instances = array();
00034 
00035     protected $domain; // string; domain (usually wiki ID)
00036 
00038     protected $managers = array();
00039 
00043     protected function __construct( $domain ) {
00044         $this->domain = $domain;
00045     }
00046 
00051     public static function singleton( $domain = false ) {
00052         $domain = ( $domain === false ) ? wfWikiID() : $domain;
00053         if ( !isset( self::$instances[$domain] ) ) {
00054             self::$instances[$domain] = new self( $domain );
00055             self::$instances[$domain]->initFromGlobals();
00056         }
00057 
00058         return self::$instances[$domain];
00059     }
00060 
00064     public static function destroySingletons() {
00065         self::$instances = array();
00066     }
00067 
00071     protected function initFromGlobals() {
00072         global $wgLockManagers;
00073 
00074         $this->register( $wgLockManagers );
00075     }
00076 
00083     protected function register( array $configs ) {
00084         foreach ( $configs as $config ) {
00085             $config['domain'] = $this->domain;
00086             if ( !isset( $config['name'] ) ) {
00087                 throw new MWException( "Cannot register a lock manager with no name." );
00088             }
00089             $name = $config['name'];
00090             if ( !isset( $config['class'] ) ) {
00091                 throw new MWException( "Cannot register lock manager `{$name}` with no class." );
00092             }
00093             $class = $config['class'];
00094             unset( $config['class'] ); // lock manager won't need this
00095             $this->managers[$name] = array(
00096                 'class' => $class,
00097                 'config' => $config,
00098                 'instance' => null
00099             );
00100         }
00101     }
00102 
00110     public function get( $name ) {
00111         if ( !isset( $this->managers[$name] ) ) {
00112             throw new MWException( "No lock manager defined with the name `$name`." );
00113         }
00114         // Lazy-load the actual lock manager instance
00115         if ( !isset( $this->managers[$name]['instance'] ) ) {
00116             $class = $this->managers[$name]['class'];
00117             $config = $this->managers[$name]['config'];
00118             $this->managers[$name]['instance'] = new $class( $config );
00119         }
00120 
00121         return $this->managers[$name]['instance'];
00122     }
00123 
00131     public function config( $name ) {
00132         if ( !isset( $this->managers[$name] ) ) {
00133             throw new MWException( "No lock manager defined with the name `$name`." );
00134         }
00135         $class = $this->managers[$name]['class'];
00136 
00137         return array( 'class' => $class ) + $this->managers[$name]['config'];
00138     }
00139 
00146     public function getDefault() {
00147         return isset( $this->managers['default'] )
00148             ? $this->get( 'default' )
00149             : new NullLockManager( array() );
00150     }
00151 
00160     public function getAny() {
00161         return isset( $this->managers['default'] )
00162             ? $this->get( 'default' )
00163             : $this->get( 'fsLockManager' );
00164     }
00165 }