From bfa965cc8551cd00fb389d0fbc7325d897e2fb3f Mon Sep 17 00:00:00 2001 From: Mikael Pajunen Date: Tue, 30 Jun 2015 15:59:08 +0300 Subject: [PATCH] [Config] Use callable instead of closure in naming Replace references to closures in function, property and parameter names in preparation of future callable support. --- UPGRADE-2.8.md | 12 ++- src/Symfony/Component/Config/CHANGELOG.md | 9 +++ .../Component/Config/Definition/BaseNode.php | 73 ++++++++++++++++--- .../Builder/ArrayNodeDefinition.php | 4 +- .../Builder/VariableNodeDefinition.php | 4 +- 5 files changed, 86 insertions(+), 16 deletions(-) diff --git a/UPGRADE-2.8.md b/UPGRADE-2.8.md index 966f1f4711132..7ddc3f740ddbd 100644 --- a/UPGRADE-2.8.md +++ b/UPGRADE-2.8.md @@ -1,4 +1,4 @@ -UPGRADE FROM 2.7 to 2.8 +UPGRADE FROM 2.7 to 2.8 ======================= Form @@ -136,3 +136,13 @@ DependencyInjection ``` + +Config +------ + + * The methods `setNormalizationClosures()` and `setFinalValidationClosures()` in + `BaseNode` were deprecated, `setNormalizationCallbacks()` and + `setFinalValidationCallbacks()` should be used instead. + + * The protected properties `normalizationClosures` and `finalValidationClosures` in + `BaseNode` were renamed to `normalizationCallbacks` and `finalValidationCallbacks`. diff --git a/src/Symfony/Component/Config/CHANGELOG.md b/src/Symfony/Component/Config/CHANGELOG.md index e1b19e6cb6f91..23f9a0d3398b8 100644 --- a/src/Symfony/Component/Config/CHANGELOG.md +++ b/src/Symfony/Component/Config/CHANGELOG.md @@ -1,6 +1,15 @@ CHANGELOG ========= +2.8.0 +----- + + * [DEPRECATION] The protected properties `normalizationClosures` and `finalValidationClosures` in + `BaseNode` were renamed to `normalizationCallbacks` and `finalValidationCallbacks`. + * [DEPRECATION] The methods `setNormalizationClosures()` and `setFinalValidationClosures()` in + `BaseNode` were deprecated, `setNormalizationCallbacks()` and `setFinalValidationCallbacks()` + should be used instead. + 2.7.0 ----- diff --git a/src/Symfony/Component/Config/Definition/BaseNode.php b/src/Symfony/Component/Config/Definition/BaseNode.php index fc3e01291665a..1734d41b85e73 100644 --- a/src/Symfony/Component/Config/Definition/BaseNode.php +++ b/src/Symfony/Component/Config/Definition/BaseNode.php @@ -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; } 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. * diff --git a/src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php b/src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php index fb34cfa8f71f7..def440b5fbb07 100644 --- a/src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php +++ b/src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php @@ -401,7 +401,7 @@ protected function createNode() $node->setNormalizeKeys($this->normalizeKeys); if (null !== $this->normalization) { - $node->setNormalizationClosures($this->normalization->before); + $node->setNormalizationCallbacks($this->normalization->before); $node->setXmlRemappings($this->normalization->remappings); } @@ -411,7 +411,7 @@ protected function createNode() } if (null !== $this->validation) { - $node->setFinalValidationClosures($this->validation->rules); + $node->setFinalValidationCallbacks($this->validation->rules); } return $node; diff --git a/src/Symfony/Component/Config/Definition/Builder/VariableNodeDefinition.php b/src/Symfony/Component/Config/Definition/Builder/VariableNodeDefinition.php index a46b7ea61ded0..5ff2fadc6026a 100644 --- a/src/Symfony/Component/Config/Definition/Builder/VariableNodeDefinition.php +++ b/src/Symfony/Component/Config/Definition/Builder/VariableNodeDefinition.php @@ -38,7 +38,7 @@ protected function createNode() $node = $this->instantiateNode(); if (null !== $this->normalization) { - $node->setNormalizationClosures($this->normalization->before); + $node->setNormalizationCallbacks($this->normalization->before); } if (null !== $this->merge) { @@ -56,7 +56,7 @@ protected function createNode() $node->setRequired($this->required); if (null !== $this->validation) { - $node->setFinalValidationClosures($this->validation->rules); + $node->setFinalValidationCallbacks($this->validation->rules); } return $node;