MediaWiki  master
ServiceOptions.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Config;
4 
5 use Config;
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 }
__construct(array $keys,... $sources)
$source
A class for passing options to services.
Interface for configuration instances.
Definition: Config.php:28
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys, without regard for order.