| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
use MediaWiki\Logger\LoggerFactory; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class LockManagerGroup { |
| 32 |
|
| 33 |
protected $domain; |
| 34 |
|
| 35 |
|
| 36 |
protected $managers = []; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public function __construct( $domain, array $lockManagerConfigs ) { |
| 45 |
$this->domain = $domain; |
| 46 |
|
| 47 |
foreach ( $lockManagerConfigs as $config ) { |
| 48 |
$config['domain'] = $this->domain; |
| 49 |
if ( !isset( $config['name'] ) ) { |
| 50 |
throw new InvalidArgumentException( "Cannot register a lock manager with no name." ); |
| 51 |
} |
| 52 |
$name = $config['name']; |
| 53 |
if ( !isset( $config['class'] ) ) { |
| 54 |
throw new InvalidArgumentException( "Cannot register lock manager `{$name}` with no class." ); |
| 55 |
} |
| 56 |
$class = $config['class']; |
| 57 |
unset( $config['class'] ); |
| 58 |
$this->managers[$name] = [ |
| 59 |
'class' => $class, |
| 60 |
'config' => $config, |
| 61 |
'instance' => null |
| 62 |
]; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function get( $name ) { |
| 74 |
if ( !isset( $this->managers[$name] ) ) { |
| 75 |
throw new InvalidArgumentException( "No lock manager defined with the name `$name`." ); |
| 76 |
} |
| 77 |
|
| 78 |
if ( !isset( $this->managers[$name]['instance'] ) ) { |
| 79 |
$class = $this->managers[$name]['class']; |
| 80 |
'@phan-var string $class'; |
| 81 |
$config = $this->managers[$name]['config']; |
| 82 |
$config['logger'] = LoggerFactory::getInstance( 'LockManager' ); |
| 83 |
|
| 84 |
$this->managers[$name]['instance'] = new $class( $config ); |
| 85 |
} |
| 86 |
|
| 87 |
return $this->managers[$name]['instance']; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function config( $name ) { |
| 98 |
if ( !isset( $this->managers[$name] ) ) { |
| 99 |
throw new InvalidArgumentException( "No lock manager defined with the name `$name`." ); |
| 100 |
} |
| 101 |
$class = $this->managers[$name]['class']; |
| 102 |
|
| 103 |
return [ 'class' => $class ] + $this->managers[$name]['config']; |
| 104 |
} |
| 105 |
} |