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