12use InvalidArgumentException;
13use UnexpectedValueException;
14use Wikimedia\Assert\Assert;
15use Wikimedia\Services\SalvageableService;
48 public function salvage( SalvageableService $other ) {
49 Assert::parameterType( self::class, $other,
'$other' );
52 '@phan-var self $other';
53 foreach ( $other->factoryFunctions as $name => $otherFunc ) {
54 if ( !isset( $this->factoryFunctions[$name] ) ) {
60 if ( isset( $other->configs[$name] )
61 && $this->factoryFunctions[$name] == $otherFunc
63 $this->configs[$name] = $other->configs[$name];
64 unset( $other->configs[$name] );
69 $other->factoryFunctions = [];
77 return array_keys( $this->factoryFunctions );
89 public function register( $name, $callback ) {
90 if ( !is_callable( $callback ) && !( $callback instanceof
Config ) ) {
91 if ( is_array( $callback ) ) {
92 $callback =
'[ ' . implode(
', ', $callback ) .
' ]';
93 } elseif ( is_object( $callback ) ) {
94 $callback =
'instanceof ' . get_class( $callback );
96 throw new InvalidArgumentException(
'Invalid callback \'' . $callback .
'\' provided
' );
99 unset( $this->configs[$name] );
100 $this->factoryFunctions[$name] = $callback;
112 public function makeConfig( $name ) {
113 if ( !isset( $this->configs[$name] ) ) {
115 if ( !isset( $this->factoryFunctions[$key] ) ) {
118 if ( !isset( $this->factoryFunctions[$key] ) ) {
119 throw new ConfigException( "No registered builder available for $name." );
122 $factory = $this->factoryFunctions[$key];
123 if ( is_callable( $factory ) ) {
124 $conf = $factory( $this );
129 if ( $conf instanceof Config ) {
130 $this->configs[$name] = $conf;
132 throw new UnexpectedValueException( "The builder for $name returned a non-Config object." );
136 return $this->configs[$name];