* objects).
*
* Services that take this type as a parameter to their constructor should specify a list of the
- * keys they expect to receive in an array. The convention is to make it a public static variable
- * called $constructorOptions. (When we drop HHVM support -- see T192166 -- it should become a
- * const.) In the constructor, they should call assertRequiredOptions() to make sure that they
- * weren't passed too few or too many options. This way it's clear what each class depends on, and
- * that it's getting passed the correct set of options. (This means there are no optional options.
- * This makes sense for services, since they shouldn't be constructed by outside code.)
+ * keys they expect to receive in an array. The convention is to make it a public const called
+ * CONSTRUCTOR_OPTIONS. In the constructor, they should call assertRequiredOptions() to make sure
+ * that they weren't passed too few or too many options. This way it's clear what each class
+ * depends on, and that it's getting passed the correct set of options. (This means there are no
+ * optional options. This makes sense for services, since they shouldn't be constructed by
+ * outside code.)
*
* @since 1.34
*/
class ServiceOptions {
+ private $keys = [];
private $options = [];
/**
* @throws InvalidArgumentException if one of $keys is not found in any of $sources
*/
public function __construct( array $keys, ...$sources ) {
+ $this->keys = $keys;
foreach ( $keys as $key ) {
foreach ( $sources as $source ) {
if ( $source instanceof Config ) {
* @param string[] $expectedKeys
*/
public function assertRequiredOptions( array $expectedKeys ) {
- $actualKeys = array_keys( $this->options );
- $extraKeys = array_diff( $actualKeys, $expectedKeys );
- $missingKeys = array_diff( $expectedKeys, $actualKeys );
- Assert::precondition( !$extraKeys && !$missingKeys,
- (
- $extraKeys
- ? 'Unsupported options passed: ' . implode( ', ', $extraKeys ) . '!'
- : ''
- ) . ( $extraKeys && $missingKeys ? ' ' : '' ) . (
- $missingKeys
- ? 'Required options missing: ' . implode( ', ', $missingKeys ) . '!'
- : ''
- )
- );
+ if ( $this->keys !== $expectedKeys ) {
+ $extraKeys = array_diff( $this->keys, $expectedKeys );
+ $missingKeys = array_diff( $expectedKeys, $this->keys );
+ Assert::precondition( !$extraKeys && !$missingKeys,
+ (
+ $extraKeys
+ ? 'Unsupported options passed: ' . implode( ', ', $extraKeys ) . '!'
+ : ''
+ ) . ( $extraKeys && $missingKeys ? ' ' : '' ) . (
+ $missingKeys
+ ? 'Required options missing: ' . implode( ', ', $missingKeys ) . '!'
+ : ''
+ )
+ );
+ }
}
/**