-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Config] Renamings to allow all callback types #14364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,15 @@ abstract class BaseNode implements NodeInterface | |
{ | ||
protected $name; | ||
protected $parent; | ||
protected $normalizationCallbacks = array(); | ||
/** | ||
* @deprecated since version 2.8, to be removed in 3.0. Use the normalizationCallbacks property instead. | ||
*/ | ||
protected $normalizationClosures = array(); | ||
protected $finalValidationCallbacks = array(); | ||
/** | ||
* @deprecated since version 2.8, to be removed in 3.0. Use the finalValidationCallbacks property instead. | ||
*/ | ||
protected $finalValidationClosures = array(); | ||
protected $allowOverwrite = true; | ||
protected $required = false; | ||
|
@@ -48,6 +56,10 @@ public function __construct($name, NodeInterface $parent = null) | |
|
||
$this->name = $name; | ||
$this->parent = $parent; | ||
|
||
// Backwards compatibility for old property names, to be removed in 3.0 | ||
$this->normalizationClosures = &$this->normalizationCallbacks; | ||
$this->finalValidationClosures = &$this->finalValidationCallbacks; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be an option to remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, using magic methods would allow allow deprecation notices and also somewhat simpler implementation (no need for All of these changes look like bikeshedding to me though. I checked with Packanalyst earlier and found just one class that would need to be changed for 3.0 compatibility -- and that one uses the setter methods. A quick search is not going to find every use case, but I think using the properties directly is likely rare even in custom subclasses. Of course, I can still make the changes if there's agreement that using |
||
} | ||
|
||
public function setAttribute($key, $value) | ||
|
@@ -151,24 +163,52 @@ public function setAllowOverwrite($allow) | |
$this->allowOverwrite = (bool) $allow; | ||
} | ||
|
||
/** | ||
* Sets the callbacks used for normalization. | ||
* | ||
* @param callable[] $callbacks An array of callbacks used for normalization | ||
*/ | ||
public function setNormalizationCallbacks(array $callbacks) | ||
{ | ||
$this->normalizationCallbacks = $callbacks; | ||
} | ||
|
||
/** | ||
* Sets the closures used for normalization. | ||
* | ||
* @param \Closure[] $closures An array of Closures used for normalization | ||
* | ||
* @deprecated since version 2.8, to be removed in 3.0. Use setNormalizationCallbacks() instead | ||
*/ | ||
public function setNormalizationClosures(array $closures) | ||
{ | ||
$this->normalizationClosures = $closures; | ||
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0. Use the setNormalizationCallbacks() method instead.', E_USER_DEPRECATED); | ||
|
||
$this->normalizationCallbacks = $closures; | ||
} | ||
|
||
/** | ||
* Sets the callbacks used for final validation. | ||
* | ||
* @param callable[] $callbacks An array of callbacks used for final validation | ||
*/ | ||
public function setFinalValidationCallbacks(array $callbacks) | ||
{ | ||
$this->finalValidationCallbacks = $callbacks; | ||
} | ||
|
||
/** | ||
* Sets the closures used for final validation. | ||
* | ||
* @param \Closure[] $closures An array of Closures used for final validation | ||
* | ||
* @deprecated since version 2.8, to be removed in 3.0. Use setFinalValidationCallbacks() instead | ||
*/ | ||
public function setFinalValidationClosures(array $closures) | ||
{ | ||
$this->finalValidationClosures = $closures; | ||
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0. Use the setFinalValidationCallbacks() method instead.', E_USER_DEPRECATED); | ||
|
||
$this->finalValidationCallbacks = $closures; | ||
} | ||
|
||
/** | ||
|
@@ -235,7 +275,7 @@ final public function merge($leftSide, $rightSide) | |
} | ||
|
||
/** | ||
* Normalizes a value, applying all normalization closures. | ||
* Normalizes a value, applying all normalization callbacks. | ||
* | ||
* @param mixed $value Value to normalize. | ||
* | ||
|
@@ -245,9 +285,9 @@ final public function normalize($value) | |
{ | ||
$value = $this->preNormalize($value); | ||
|
||
// run custom normalization closures | ||
foreach ($this->normalizationClosures as $closure) { | ||
$value = $closure($value); | ||
// run custom normalization callbacks | ||
foreach ($this->normalizationCallbacks as $callback) { | ||
$value = call_user_func($callback, $value); | ||
} | ||
|
||
// replace value with their equivalent | ||
|
@@ -287,7 +327,7 @@ public function getParent() | |
} | ||
|
||
/** | ||
* Finalizes a value, applying all finalization closures. | ||
* Finalizes a value, applying all finalization callbacks. | ||
* | ||
* @param mixed $value The value to finalize | ||
* | ||
|
@@ -302,11 +342,11 @@ final public function finalize($value) | |
|
||
$value = $this->finalizeValue($value); | ||
|
||
// Perform validation on the final value if a closure has been set. | ||
// The closure is also allowed to return another value. | ||
foreach ($this->finalValidationClosures as $closure) { | ||
// Perform validation on the final value if a callback has been set. | ||
// The callback is also allowed to return another value. | ||
foreach ($this->finalValidationCallbacks as $callback) { | ||
try { | ||
$value = $closure($value); | ||
$value = call_user_func($callback, $value); | ||
} catch (Exception $e) { | ||
throw $e; | ||
} catch (\Exception $e) { | ||
|
@@ -317,6 +357,17 @@ final public function finalize($value) | |
return $value; | ||
} | ||
|
||
public function __clone() | ||
{ | ||
// Backwards compatibility for old property names, to be removed in 3.0 | ||
// Break cross-clones references but preserve them inside each one. | ||
unset($this->normalizationCallbacks, $this->finalValidationCallbacks); | ||
$this->normalizationCallbacks = $this->normalizationClosures; | ||
$this->finalValidationCallbacks = $this->finalValidationClosures; | ||
$this->normalizationClosures = &$this->normalizationCallbacks; | ||
$this->finalValidationClosures = &$this->finalValidationCallbacks; | ||
} | ||
|
||
/** | ||
* Validates the type of a Node. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aliasing the old ones by reference would allow preserving BC.
you should do it in the constructor, and for completeness also add a magic __clone method:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Referenced aliases added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You forgot to keep the declaration above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works without, but of course it makes sense to keep the old property declarations as well. Restored now, with deprecation notes.