Debian Codegraph @

includes/config/ServiceOptions.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Config;
4
5 use InvalidArgumentException;
6 use Wikimedia\Assert\Assert;
7 use function array_key_exists;
8
9 /**
10 * A class for passing options to services. It can be constructed from a Config, and in practice
11 * most options will be taken from site configuration, but they don't have to be. The options passed
12 * are copied and will not reflect subsequent updates to site configuration (assuming they're not
13 * objects).
14 *
15 * Services that take this type as a parameter to their constructor should specify a list of the
16 * keys they expect to receive in an array. The convention is to make it a public const called
17 * CONSTRUCTOR_OPTIONS. In the constructor, they should call assertRequiredOptions() to make sure
18 * that they weren't passed too few or too many options. This way it's clear what each class
19 * depends on, and that it's getting passed the correct set of options. (This means there are no
20 * optional options. This makes sense for services, since they shouldn't be constructed by
21 * outside code.)
22 *
23 * @newable since 1.36
24 *
25 * @since 1.34
26 */
27 class ServiceOptions {
28 /** @var string[] */
29 private $keys;
30 /** @var array */
31 private $options = [];
32
33 /**
34 * @stable to call since 1.36
35 *
36 * @param string[] $keys Which keys to extract from $sources
37 * @param Config|ServiceOptions|array ...$sources Each source is either a Config object or an array. If the
38 * same key is present in two sources, the first one takes precedence. Keys that are not in
39 * $keys are ignored.
40 * @throws InvalidArgumentException if one of $keys is not found in any of $sources
41 */
42 public function __construct( array $keys, ...$sources ) {
43 $this->keys = $keys;
44 foreach ( $keys as $key ) {
45 foreach ( $sources as $source ) {
46 if ( $source instanceof Config ) {
47 if ( $source->has( $key ) ) {
48 $this->options[$key] = $source->get( $key );
49 continue 2;
50 }
51 } elseif ( $source instanceof ServiceOptions ) {
52 if ( array_key_exists( $key, $source->options ) ) {
53 $this->options[$key] = $source->get( $key );
54 continue 2;
55 }
56 } elseif ( array_key_exists( $key, $source ) ) {
57 $this->options[$key] = $source[$key];
58 continue 2;
59 }
60 }
61 throw new InvalidArgumentException( "Key \"$key\" not found in input sources" );
62 }
63 }
64
65 /**
66 * Assert that the list of options provided in this instance exactly match $expectedKeys,
67 * without regard for order.
68 *
69 * @param string[] $expectedKeys
70 */
71 public function assertRequiredOptions( array $expectedKeys ) {
72 if ( $this->keys !== $expectedKeys ) {
73 $extraKeys = array_diff( $this->keys, $expectedKeys );
74 $missingKeys = array_diff( $expectedKeys, $this->keys );
75 Assert::precondition( !$extraKeys && !$missingKeys,
76 (
77 $extraKeys
78 ? 'Unsupported options passed: ' . implode( ', ', $extraKeys ) . '!'
79 : ''
80 ) . ( $extraKeys && $missingKeys ? ' ' : '' ) . (
81 $missingKeys
82 ? 'Required options missing: ' . implode( ', ', $missingKeys ) . '!'
83 : ''
84 )
85 );
86 }
87 }
88
89 /**
90 * @param string $key
91 * @return mixed
92 */
93 public function get( $key ) {
94 if ( !array_key_exists( $key, $this->options ) ) {
95 throw new InvalidArgumentException( "Unrecognized option \"$key\"" );
96 }
97 return $this->options[$key];
98 }
99 }