MediaWiki
REL1_24
|
00001 <?php 00002 00029 class ConfigFactory { 00030 00035 protected $factoryFunctions = array(); 00036 00042 protected $configs = array(); 00043 00047 private static $self; 00048 00049 public static function getDefaultInstance() { 00050 if ( !self::$self ) { 00051 self::$self = new self; 00052 global $wgConfigRegistry; 00053 foreach ( $wgConfigRegistry as $name => $callback ) { 00054 self::$self->register( $name, $callback ); 00055 } 00056 } 00057 return self::$self; 00058 } 00059 00066 public static function destroyDefaultInstance() { 00067 if ( !defined( 'MW_PHPUNIT_TEST' ) ) { 00068 throw new MWException( __METHOD__ . ' was called outside of unit tests' ); 00069 } 00070 00071 self::$self = null; 00072 } 00073 00081 public function register( $name, $callback ) { 00082 if ( !is_callable( $callback ) ) { 00083 throw new InvalidArgumentException( 'Invalid callback provided' ); 00084 } 00085 $this->factoryFunctions[$name] = $callback; 00086 } 00087 00097 public function makeConfig( $name ) { 00098 if ( !isset( $this->configs[$name] ) ) { 00099 if ( !isset( $this->factoryFunctions[$name] ) ) { 00100 throw new ConfigException( "No registered builder available for $name." ); 00101 } 00102 $conf = call_user_func( $this->factoryFunctions[$name], $this ); 00103 if ( $conf instanceof Config ) { 00104 $this->configs[$name] = $conf; 00105 } else { 00106 throw new UnexpectedValueException( "The builder for $name returned a non-Config object." ); 00107 } 00108 } 00109 00110 return $this->configs[$name]; 00111 } 00112 }