vendor/symfony/config/Definition/Builder/NodeBuilder.php line 195

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Config\Definition\Builder;
  11. /**
  12.  * This class provides a fluent interface for building a node.
  13.  *
  14.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  15.  */
  16. class NodeBuilder implements NodeParentInterface
  17. {
  18.     protected $parent;
  19.     protected $nodeMapping;
  20.     public function __construct()
  21.     {
  22.         $this->nodeMapping = [
  23.             'variable' => VariableNodeDefinition::class,
  24.             'scalar' => ScalarNodeDefinition::class,
  25.             'boolean' => BooleanNodeDefinition::class,
  26.             'integer' => IntegerNodeDefinition::class,
  27.             'float' => FloatNodeDefinition::class,
  28.             'array' => ArrayNodeDefinition::class,
  29.             'enum' => EnumNodeDefinition::class,
  30.         ];
  31.     }
  32.     /**
  33.      * Set the parent node.
  34.      *
  35.      * @return $this
  36.      */
  37.     public function setParent(ParentNodeDefinitionInterface $parent null): static
  38.     {
  39.         $this->parent $parent;
  40.         return $this;
  41.     }
  42.     /**
  43.      * Creates a child array node.
  44.      */
  45.     public function arrayNode(string $name): ArrayNodeDefinition
  46.     {
  47.         return $this->node($name'array');
  48.     }
  49.     /**
  50.      * Creates a child scalar node.
  51.      */
  52.     public function scalarNode(string $name): ScalarNodeDefinition
  53.     {
  54.         return $this->node($name'scalar');
  55.     }
  56.     /**
  57.      * Creates a child Boolean node.
  58.      */
  59.     public function booleanNode(string $name): BooleanNodeDefinition
  60.     {
  61.         return $this->node($name'boolean');
  62.     }
  63.     /**
  64.      * Creates a child integer node.
  65.      */
  66.     public function integerNode(string $name): IntegerNodeDefinition
  67.     {
  68.         return $this->node($name'integer');
  69.     }
  70.     /**
  71.      * Creates a child float node.
  72.      */
  73.     public function floatNode(string $name): FloatNodeDefinition
  74.     {
  75.         return $this->node($name'float');
  76.     }
  77.     /**
  78.      * Creates a child EnumNode.
  79.      */
  80.     public function enumNode(string $name): EnumNodeDefinition
  81.     {
  82.         return $this->node($name'enum');
  83.     }
  84.     /**
  85.      * Creates a child variable node.
  86.      */
  87.     public function variableNode(string $name): VariableNodeDefinition
  88.     {
  89.         return $this->node($name'variable');
  90.     }
  91.     /**
  92.      * Returns the parent node.
  93.      *
  94.      * @return NodeDefinition&ParentNodeDefinitionInterface
  95.      */
  96.     public function end()
  97.     {
  98.         return $this->parent;
  99.     }
  100.     /**
  101.      * Creates a child node.
  102.      *
  103.      * @throws \RuntimeException When the node type is not registered
  104.      * @throws \RuntimeException When the node class is not found
  105.      */
  106.     public function node(?string $namestring $type): NodeDefinition
  107.     {
  108.         $class $this->getNodeClass($type);
  109.         $node = new $class($name);
  110.         $this->append($node);
  111.         return $node;
  112.     }
  113.     /**
  114.      * Appends a node definition.
  115.      *
  116.      * Usage:
  117.      *
  118.      *     $node = new ArrayNodeDefinition('name')
  119.      *         ->children()
  120.      *             ->scalarNode('foo')->end()
  121.      *             ->scalarNode('baz')->end()
  122.      *             ->append($this->getBarNodeDefinition())
  123.      *         ->end()
  124.      *     ;
  125.      *
  126.      * @return $this
  127.      */
  128.     public function append(NodeDefinition $node): static
  129.     {
  130.         if ($node instanceof BuilderAwareInterface) {
  131.             $builder = clone $this;
  132.             $builder->setParent(null);
  133.             $node->setBuilder($builder);
  134.         }
  135.         if (null !== $this->parent) {
  136.             $this->parent->append($node);
  137.             // Make this builder the node parent to allow for a fluid interface
  138.             $node->setParent($this);
  139.         }
  140.         return $this;
  141.     }
  142.     /**
  143.      * Adds or overrides a node Type.
  144.      *
  145.      * @param string $type  The name of the type
  146.      * @param string $class The fully qualified name the node definition class
  147.      *
  148.      * @return $this
  149.      */
  150.     public function setNodeClass(string $typestring $class): static
  151.     {
  152.         $this->nodeMapping[strtolower($type)] = $class;
  153.         return $this;
  154.     }
  155.     /**
  156.      * Returns the class name of the node definition.
  157.      *
  158.      * @throws \RuntimeException When the node type is not registered
  159.      * @throws \RuntimeException When the node class is not found
  160.      */
  161.     protected function getNodeClass(string $type): string
  162.     {
  163.         $type strtolower($type);
  164.         if (!isset($this->nodeMapping[$type])) {
  165.             throw new \RuntimeException(sprintf('The node type "%s" is not registered.'$type));
  166.         }
  167.         $class $this->nodeMapping[$type];
  168.         if (!class_exists($class)) {
  169.             throw new \RuntimeException(sprintf('The node class "%s" does not exist.'$class));
  170.         }
  171.         return $class;
  172.     }
  173. }