MediaWiki  master
ServiceOptions.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Config;
4 
5 use Config;
6 use InvalidArgumentException;
7 use Wikimedia\Assert\Assert;
8 
28  private $keys = [];
29  private $options = [];
30 
40  public function __construct( array $keys, ...$sources ) {
41  $this->keys = $keys;
42  foreach ( $keys as $key ) {
43  foreach ( $sources as $source ) {
44  if ( $source instanceof Config ) {
45  if ( $source->has( $key ) ) {
46  $this->options[$key] = $source->get( $key );
47  continue 2;
48  }
49  } else {
50  if ( array_key_exists( $key, $source ) ) {
51  $this->options[$key] = $source[$key];
52  continue 2;
53  }
54  }
55  }
56  throw new InvalidArgumentException( "Key \"$key\" not found in input sources" );
57  }
58  }
59 
66  public function assertRequiredOptions( array $expectedKeys ) {
67  if ( $this->keys !== $expectedKeys ) {
68  $extraKeys = array_diff( $this->keys, $expectedKeys );
69  $missingKeys = array_diff( $expectedKeys, $this->keys );
70  Assert::precondition( !$extraKeys && !$missingKeys,
71  (
72  $extraKeys
73  ? 'Unsupported options passed: ' . implode( ', ', $extraKeys ) . '!'
74  : ''
75  ) . ( $extraKeys && $missingKeys ? ' ' : '' ) . (
76  $missingKeys
77  ? 'Required options missing: ' . implode( ', ', $missingKeys ) . '!'
78  : ''
79  )
80  );
81  }
82  }
83 
88  public function get( $key ) {
89  if ( !array_key_exists( $key, $this->options ) ) {
90  throw new InvalidArgumentException( "Unrecognized option \"$key\"" );
91  }
92  return $this->options[$key];
93  }
94 }
MediaWiki\Config\ServiceOptions\$options
$options
Definition: ServiceOptions.php:29
Config
Interface for configuration instances.
Definition: Config.php:30
MediaWiki\Config\ServiceOptions
A class for passing options to services.
Definition: ServiceOptions.php:27
MediaWiki\Config\ServiceOptions\$keys
$keys
Definition: ServiceOptions.php:28
MediaWiki\Config
Definition: ConfigRepository.php:23
$source
$source
Definition: mwdoc-filter.php:34
MediaWiki\Config\ServiceOptions\__construct
__construct(array $keys,... $sources)
Definition: ServiceOptions.php:40
MediaWiki\Config\ServiceOptions\assertRequiredOptions
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
Definition: ServiceOptions.php:66