26use InvalidArgumentException;
27use UnexpectedValueException;
28use Wikimedia\Assert\Assert;
29use Wikimedia\Services\SalvageableService;
62 public function salvage( SalvageableService $other ) {
63 Assert::parameterType( self::class, $other,
'$other' );
66 '@phan-var self $other';
67 foreach ( $other->factoryFunctions as $name => $otherFunc ) {
68 if ( !isset( $this->factoryFunctions[$name] ) ) {
74 if ( isset( $other->configs[$name] )
75 && $this->factoryFunctions[$name] == $otherFunc
77 $this->configs[$name] = $other->configs[$name];
78 unset( $other->configs[$name] );
83 $other->factoryFunctions = [];
91 return array_keys( $this->factoryFunctions );
103 public function register( $name, $callback ) {
104 if ( !is_callable( $callback ) && !( $callback instanceof
Config ) ) {
105 if ( is_array( $callback ) ) {
106 $callback =
'[ ' . implode(
', ', $callback ) .
' ]';
107 } elseif ( is_object( $callback ) ) {
108 $callback =
'instanceof ' . get_class( $callback );
110 throw new InvalidArgumentException(
'Invalid callback \'' . $callback .
'\' provided
' );
113 unset( $this->configs[$name] );
114 $this->factoryFunctions[$name] = $callback;
126 public function makeConfig( $name ) {
127 if ( !isset( $this->configs[$name] ) ) {
129 if ( !isset( $this->factoryFunctions[$key] ) ) {
132 if ( !isset( $this->factoryFunctions[$key] ) ) {
133 throw new ConfigException( "No registered builder available for $name." );
136 $factory = $this->factoryFunctions[$key];
137 if ( is_callable( $factory ) ) {
138 $conf = $factory( $this );
143 if ( $conf instanceof Config ) {
144 $this->configs[$name] = $conf;
146 throw new UnexpectedValueException( "The builder for $name returned a non-Config object." );
150 return $this->configs[$name];