MediaWiki  REL1_22
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         return self::$instances[$domain];
00058     }
00059 
00065     public static function destroySingletons() {
00066         self::$instances = array();
00067     }
00068 
00074     protected function initFromGlobals() {
00075         global $wgLockManagers;
00076 
00077         $this->register( $wgLockManagers );
00078     }
00079 
00087     protected function register( array $configs ) {
00088         foreach ( $configs as $config ) {
00089             $config['domain'] = $this->domain;
00090             if ( !isset( $config['name'] ) ) {
00091                 throw new MWException( "Cannot register a lock manager with no name." );
00092             }
00093             $name = $config['name'];
00094             if ( !isset( $config['class'] ) ) {
00095                 throw new MWException( "Cannot register lock manager `{$name}` with no class." );
00096             }
00097             $class = $config['class'];
00098             unset( $config['class'] ); // lock manager won't need this
00099             $this->managers[$name] = array(
00100                 'class' => $class,
00101                 'config' => $config,
00102                 'instance' => null
00103             );
00104         }
00105     }
00106 
00114     public function get( $name ) {
00115         if ( !isset( $this->managers[$name] ) ) {
00116             throw new MWException( "No lock manager defined with the name `$name`." );
00117         }
00118         // Lazy-load the actual lock manager instance
00119         if ( !isset( $this->managers[$name]['instance'] ) ) {
00120             $class = $this->managers[$name]['class'];
00121             $config = $this->managers[$name]['config'];
00122             $this->managers[$name]['instance'] = new $class( $config );
00123         }
00124         return $this->managers[$name]['instance'];
00125     }
00126 
00134     public function config( $name ) {
00135         if ( !isset( $this->managers[$name] ) ) {
00136             throw new MWException( "No lock manager defined with the name `$name`." );
00137         }
00138         $class = $this->managers[$name]['class'];
00139         return array( 'class' => $class ) + $this->managers[$name]['config'];
00140     }
00141 
00148     public function getDefault() {
00149         return isset( $this->managers['default'] )
00150             ? $this->get( 'default' )
00151             : new NullLockManager( array() );
00152     }
00153 
00162     public function getAny() {
00163         return isset( $this->managers['default'] )
00164             ? $this->get( 'default' )
00165             : $this->get( 'fsLockManager' );
00166     }
00167 }