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
28 private $keys;
30 private $options = [];
31
41 public function __construct( array $keys, ...$sources ) {
42 $this->keys = $keys;
43 foreach ( $keys as $key ) {
44 foreach ( $sources as $source ) {
45 if ( $source instanceof Config ) {
46 if ( $source->has( $key ) ) {
47 $this->options[$key] = $source->get( $key );
48 continue 2;
49 }
50 } elseif ( $source instanceof ServiceOptions ) {
51 if ( array_key_exists( $key, $source->options ) ) {
52 $this->options[$key] = $source->get( $key );
53 continue 2;
54 }
55 } elseif ( array_key_exists( $key, $source ) ) {
56 $this->options[$key] = $source[$key];
57 continue 2;
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