Ver Fonte

[Config] Create VariableNode, which mimics ScalarNode but omits type-checking

This allows for configuration options that must accept arbitrary variables. Even if the node's value is an array, VariableNode will not perform any special processing/merging as is done for ArrayNode. It functionally behaves like a ScalarNode.
Jeremy Mikola há 14 anos atrás
pai
commit
608e443c97

+ 12 - 0
src/Symfony/Component/Config/Definition/Builder/NodeBuilder.php

@@ -160,6 +160,18 @@ class NodeBuilder
         return $this->node($name, 'boolean');
     }
 
+    /**
+     * Creates a child variable node.
+     *
+     * @param string $name The name of the node
+     *
+     * @return Symfony\Component\Config\Definition\Builder\NodeBuilder The builder of the child node
+     */
+    public function variableNode($name)
+    {
+        return $this->node($name, 'variable');
+    }
+
     /**
      * Sets the default value.
      *

+ 44 - 0
src/Symfony/Component/Config/Definition/Builder/TreeBuilder.php

@@ -17,6 +17,7 @@ use Symfony\Component\Config\Definition\BooleanNode;
 
 use Symfony\Component\Config\Definition\ArrayNode;
 use Symfony\Component\Config\Definition\ScalarNode;
+use Symfony\Component\Config\Definition\VariableNode;
 
 /**
  * This is the entry class for building your own config tree.
@@ -146,6 +147,49 @@ class TreeBuilder
         }
     }
 
+    /**
+     * Creates a variable node.
+     *
+     * @param NodeBuilder $node the builder of the node
+     *
+     * @return Symfony\Component\Config\Definition\VariableNode
+     */
+    protected function createVariableConfigNode(NodeBuilder $node)
+    {
+        $configNode = new VariableNode($node->name, $node->parent);
+
+        if (null !== $node->normalization) {
+            $configNode->setNormalizationClosures(
+                $this->buildExpressions($node->normalization->before)
+            );
+        }
+
+        if (null !== $node->merge) {
+            $configNode->setAllowOverwrite($node->merge->allowOverwrite);
+        }
+
+        if (true === $node->default) {
+            $configNode->setDefaultValue($node->defaultValue);
+        }
+
+        if (false === $node->allowEmptyValue) {
+            $configNode->setAllowEmptyValue($node->allowEmptyValue);
+        }
+
+        $configNode->addEquivalentValue(null, $node->nullEquivalent);
+        $configNode->addEquivalentValue(true, $node->trueEquivalent);
+        $configNode->addEquivalentValue(false, $node->falseEquivalent);
+        $configNode->setRequired($node->required);
+
+        if (null !== $node->validation) {
+            $configNode->setFinalValidationClosures(
+                $this->buildExpressions($node->validation->rules)
+            );
+        }
+
+        return $configNode;
+    }
+
     /**
      * Creates an array node.
      *

+ 113 - 0
src/Symfony/Component/Config/Definition/VariableNode.php

@@ -0,0 +1,113 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Config\Definition;
+
+use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
+
+/**
+ * This node represents a variable value in the config tree.
+ *
+ * This node is intended for arbitrary variables. It behaves similar to scalar
+ * nodes except that any PHP type is accepted as a value.
+ *
+ * @author Jeremy Mikola <jmikola@gmail.com>
+ */
+class VariableNode extends BaseNode implements PrototypeNodeInterface
+{
+    protected $defaultValueSet = false;
+    protected $defaultValue;
+    protected $allowEmptyValue = true;
+
+    /**
+     * {@inheritDoc}
+     */
+    public function setDefaultValue($value)
+    {
+        $this->defaultValueSet = true;
+        $this->defaultValue = $value;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function hasDefaultValue()
+    {
+        return $this->defaultValueSet;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function getDefaultValue()
+    {
+        return $this->defaultValue;
+    }
+
+    /**
+     * Sets if this node is allowed to have an empty value.
+     *
+     * @param boolean $boolean True if this entity will accept empty values.
+     * @return void
+     */
+    public function setAllowEmptyValue($boolean)
+    {
+        $this->allowEmptyValue = (Boolean) $boolean;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function setName($name)
+    {
+        $this->name = $name;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected function validateType($value)
+    {
+        return true;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected function finalizeValue($value)
+    {
+        if (!$this->allowEmptyValue && empty($value)) {
+            throw new InvalidConfigurationException(sprintf(
+                'The path "%s" cannot contain an empty value, but got %s.',
+                $this->getPath(),
+                json_encode($value)
+            ));
+        }
+
+        return $value;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected function normalizeValue($value)
+    {
+        return $value;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected function mergeValues($leftSide, $rightSide)
+    {
+        return $rightSide;
+    }
+}