You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -75,6 +76,11 @@ class OptionsResolver implements Options
75
76
*/
76
77
private$calling = array();
77
78
79
+
/**
80
+
* A list of deprecated options.
81
+
*/
82
+
private$deprecated = array();
83
+
78
84
/**
79
85
* Whether the instance is locked for reading.
80
86
*
@@ -348,6 +354,57 @@ public function getDefinedOptions()
348
354
returnarray_keys($this->defined);
349
355
}
350
356
357
+
/**
358
+
* Deprecates an option, allowed types or values.
359
+
*
360
+
* Instead of passing the message, you may also pass a closure with the
361
+
* following signature:
362
+
*
363
+
* function ($value) {
364
+
* // ...
365
+
* }
366
+
*
367
+
* The closure receives the value as argument and should return a string.
368
+
* Returns an empty string to ignore the option deprecation.
369
+
*
370
+
* The closure is invoked when {@link resolve()} is called. The parameter
371
+
* passed to the closure is the value of the option after validating it
372
+
* and before normalizing it.
373
+
*
374
+
* @param string|\Closure $deprecationMessage
375
+
*/
376
+
publicfunctionsetDeprecated(string$option, $deprecationMessage = 'The option "%name%" is deprecated.'): self
377
+
{
378
+
if ($this->locked) {
379
+
thrownewAccessException('Options cannot be deprecated from a lazy option or normalizer.');
380
+
}
381
+
382
+
if (!isset($this->defined[$option])) {
383
+
thrownewUndefinedOptionsException(sprintf('The option "%s" does not exist, defined options are: "%s".', $option, implode('", "', array_keys($this->defined))));
384
+
}
385
+
386
+
if (!\is_string($deprecationMessage) && !$deprecationMessageinstanceof \Closure) {
387
+
thrownewInvalidArgumentException(sprintf('Invalid type for deprecation message argument, expected string or \Closure, but got "%s".', \gettype($deprecationMessage)));
388
+
}
389
+
390
+
// ignore if empty string
391
+
if ('' === $deprecationMessage) {
392
+
return$this;
393
+
}
394
+
395
+
$this->deprecated[$option] = $deprecationMessage;
396
+
397
+
// Make sure the option is processed
398
+
unset($this->resolved[$option]);
399
+
400
+
return$this;
401
+
}
402
+
403
+
publicfunctionisDeprecated(string$option): bool
404
+
{
405
+
returnisset($this->deprecated[$option]);
406
+
}
407
+
351
408
/**
352
409
* Sets the normalizer for an option.
353
410
*
@@ -620,6 +677,7 @@ public function clear()
620
677
$this->normalizers = array();
621
678
$this->allowedTypes = array();
622
679
$this->allowedValues = array();
680
+
$this->deprecated = array();
623
681
624
682
return$this;
625
683
}
@@ -836,6 +894,19 @@ public function offsetGet($option)
836
894
}
837
895
}
838
896
897
+
// Check whether the option is deprecated
898
+
if (isset($this->deprecated[$option])) {
899
+
$deprecationMessage = $this->deprecated[$option];
900
+
901
+
if ($deprecationMessageinstanceof \Closure && !\is_string($deprecationMessage = $deprecationMessage($value))) {
902
+
thrownewInvalidArgumentException(sprintf('Invalid type for deprecation message, expected string but got "%s", returns an empty string to ignore.', \gettype($deprecationMessage)));
0 commit comments