| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace MediaWiki\Config; |
| 25 |
|
| 26 |
use InvalidArgumentException; |
| 27 |
use UnexpectedValueException; |
| 28 |
use Wikimedia\Assert\Assert; |
| 29 |
use Wikimedia\Services\SalvageableService; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class ConfigFactory implements SalvageableService { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
protected $factoryFunctions = []; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
protected $configs = []; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public function salvage( SalvageableService $other ) { |
| 63 |
Assert::parameterType( self::class, $other, '$other' ); |
| 64 |
|
| 65 |
|
| 66 |
'@phan-var self $other'; |
| 67 |
foreach ( $other->factoryFunctions as $name => $otherFunc ) { |
| 68 |
if ( !isset( $this->factoryFunctions[$name] ) ) { |
| 69 |
continue; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
if ( isset( $other->configs[$name] ) |
| 75 |
&& $this->factoryFunctions[$name] == $otherFunc |
| 76 |
) { |
| 77 |
$this->configs[$name] = $other->configs[$name]; |
| 78 |
unset( $other->configs[$name] ); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
$other->factoryFunctions = []; |
| 84 |
$other->configs = []; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function getConfigNames() { |
| 91 |
return array_keys( $this->factoryFunctions ); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 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 ); |
| 109 |
} |
| 110 |
throw new InvalidArgumentException( 'Invalid callback \'' . $callback . '\' provided' ); |
| 111 |
} |
| 112 |
|
| 113 |
unset( $this->configs[$name] ); |
| 114 |
$this->factoryFunctions[$name] = $callback; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public function makeConfig( $name ) { |
| 127 |
if ( !isset( $this->configs[$name] ) ) { |
| 128 |
$key = $name; |
| 129 |
if ( !isset( $this->factoryFunctions[$key] ) ) { |
| 130 |
$key = '*'; |
| 131 |
} |
| 132 |
if ( !isset( $this->factoryFunctions[$key] ) ) { |
| 133 |
throw new ConfigException( "No registered builder available for $name." ); |
| 134 |
} |
| 135 |
|
| 136 |
if ( $this->factoryFunctions[$key] instanceof Config ) { |
| 137 |
$conf = $this->factoryFunctions[$key]; |
| 138 |
} else { |
| 139 |
$conf = call_user_func( $this->factoryFunctions[$key], $this ); |
| 140 |
} |
| 141 |
|
| 142 |
if ( $conf instanceof Config ) { |
| 143 |
$this->configs[$name] = $conf; |
| 144 |
} else { |
| 145 |
throw new UnexpectedValueException( "The builder for $name returned a non-Config object." ); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
return $this->configs[$name]; |
| 150 |
} |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
class_alias( ConfigFactory::class, 'ConfigFactory' ); |