MediaWiki  1.34.0
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 
26  private $keys = [];
27  private $options = [];
28 
36  public function __construct( array $keys, ...$sources ) {
37  $this->keys = $keys;
38  foreach ( $keys as $key ) {
39  foreach ( $sources as $source ) {
40  if ( $source instanceof Config ) {
41  if ( $source->has( $key ) ) {
42  $this->options[$key] = $source->get( $key );
43  continue 2;
44  }
45  } else {
46  if ( array_key_exists( $key, $source ) ) {
47  $this->options[$key] = $source[$key];
48  continue 2;
49  }
50  }
51  }
52  throw new InvalidArgumentException( "Key \"$key\" not found in input sources" );
53  }
54  }
55 
62  public function assertRequiredOptions( array $expectedKeys ) {
63  if ( $this->keys !== $expectedKeys ) {
64  $extraKeys = array_diff( $this->keys, $expectedKeys );
65  $missingKeys = array_diff( $expectedKeys, $this->keys );
66  Assert::precondition( !$extraKeys && !$missingKeys,
67  (
68  $extraKeys
69  ? 'Unsupported options passed: ' . implode( ', ', $extraKeys ) . '!'
70  : ''
71  ) . ( $extraKeys && $missingKeys ? ' ' : '' ) . (
72  $missingKeys
73  ? 'Required options missing: ' . implode( ', ', $missingKeys ) . '!'
74  : ''
75  )
76  );
77  }
78  }
79 
84  public function get( $key ) {
85  if ( !array_key_exists( $key, $this->options ) ) {
86  throw new InvalidArgumentException( "Unrecognized option \"$key\"" );
87  }
88  return $this->options[$key];
89  }
90 }
MediaWiki\Config\ServiceOptions\$options
$options
Definition: ServiceOptions.php:27
Config
Interface for configuration instances.
Definition: Config.php:28
MediaWiki\Config\ServiceOptions
A class for passing options to services.
Definition: ServiceOptions.php:25
MediaWiki\Config\ServiceOptions\$keys
$keys
Definition: ServiceOptions.php:26
MediaWiki\Config
Definition: ConfigRepository.php:23
$source
$source
Definition: mwdoc-filter.php:34
MediaWiki\Config\ServiceOptions\__construct
__construct(array $keys,... $sources)
Definition: ServiceOptions.php:36
MediaWiki\Config\ServiceOptions\assertRequiredOptions
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
Definition: ServiceOptions.php:62