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 } elseif ( array_key_exists( $key, $source ) ) {
54 $this->options[$key] = $source[$key];
55 continue 2;
56 }
57 }
58 throw new InvalidArgumentException( "Key \"$key\" not found in input sources" );
59 }
60 }
61
68 public function assertRequiredOptions( array $expectedKeys ) {
69 if ( $this->keys !== $expectedKeys ) {
70 $extraKeys = array_diff( $this->keys, $expectedKeys );
71 $missingKeys = array_diff( $expectedKeys, $this->keys );
72 Assert::precondition( !$extraKeys && !$missingKeys,
73 (
74 $extraKeys
75 ? 'Unsupported options passed: ' . implode( ', ', $extraKeys ) . '!'
76 : ''
77 ) . ( $extraKeys && $missingKeys ? ' ' : '' ) . (
78 $missingKeys
79 ? 'Required options missing: ' . implode( ', ', $missingKeys ) . '!'
80 : ''
81 )
82 );
83 }
84 }
85
90 public function get( $key ) {
91 if ( !array_key_exists( $key, $this->options ) ) {
92 throw new InvalidArgumentException( "Unrecognized option \"$key\"" );
93 }
94 return $this->options[$key];
95 }
96}
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