Find an unique root element or create one if it doesn´t exists

@return BaseResourceTree
16102
protected function findBaseRootElement()
    {
        $root = $this->findBaseRootElementOrNull();
        if (is_null($root)) {
            $root = $this->createBaseRootElement();
        }
        return $root;
    }
452009
public function buildTree()
    {
        if (null === $this->root) {
            throw new \RuntimeException('The configuration tree has no root node.');
        }
        if (null !== $this->tree) {
            return $this->tree;
        }

        return $this->tree = $this->root->getNode(true);
    }
420436
public function getConfigTreeBuilder()
  {
    $treeBuilder = new TreeBuilder();
    $rootNode    = $treeBuilder->root('config');

    $rootNode
      ->useAttributeAsKey('name')
        ->prototype('array')
          ->children()
            ->enumNode('type')->values(array_merge(TypeEnum::toArray(), array_map('strtoupper', TypeEnum::toArray())))->defaultValue(null)->end()
            ->scalarNode('renameTo')->defaultValue(null)->end()
            ->arrayNode('children')->prototype('variable')->end()->end()
            ->arrayNode('options')
            ->addDefaultsIfNotSet()
              ->children()
                ->booleanNode('assoc')->defaultValue(true)->end()
                ->arrayNode('expected')->prototype('scalar')->end()->end()
                ->enumNode('stringValidation')->values(array_merge(StringValidationEnum::toArray(), array_map('strtoupper', StringValidationEnum::toArray())))->defaultValue(null)->end()
                ->booleanNode('strongValidation')->defaultValue(false)->end()
                ->floatNode('min')->defaultValue(null)->end()
                ->floatNode('max')->defaultValue(null)->end()
                ->integerNode('round')->defaultValue(null)->end()
                ->booleanNode('floor')->defaultValue(false)->end()
                ->booleanNode('ceil')->defaultValue(false)->end()
                ->scalarNode('returnClass')->defaultValue(null)->end()
                ->variableNode('defaultValue')->defaultValue(null)->end()
                ->scalarNode('regex')->defaultValue(null)->end()
                ->arrayNode('date')
                  ->addDefaultsIfNotSet()
                  ->children()
                    ->variableNode('expectedFormat')->defaultValue('Y-m-d')->end()
                    ->booleanNode('convertToObject')->defaultValue(false)->end()
                    ->scalarNode('convertToFormat')->defaultValue(null)->end()
                  ->end()
                ->end()
                ->booleanNode('required')->defaultValue(false)->end()
                ->arrayNode('requiredIfNotAvailable')
                ->addDefaultsIfNotSet()
                  ->children()
                    ->arrayNode('and')
                      ->prototype('scalar')->end()
                      ->defaultValue(array())
                    ->end()
                    ->arrayNode('or')
                      ->prototype('scalar')->end()
                      ->defaultValue(array())
                    ->end()
                  ->end()
                ->end()
                ->arrayNode('requiredIfAvailable')
                  ->addDefaultsIfNotSet()
                  ->children()
                    ->arrayNode('and')
                      ->prototype('scalar')->end()
                      ->defaultValue(array())
                    ->end()
                    ->arrayNode('or')
                      ->prototype('scalar')->end()
                      ->defaultValue(array())
                    ->end()
                  ->end()
                ->end()
                ->arrayNode('forbiddenIfNotAvailable')
                  ->prototype('scalar')->end()
                  ->defaultValue(array())
                ->end()
                ->arrayNode('forbiddenIfAvailable')
                  ->prototype('scalar')->end()
                  ->defaultValue(array())
                ->end()
                ->arrayNode('length')
                  ->addDefaultsIfNotSet()
                  ->children()
                    ->floatNode('min')->defaultValue(null)->end()
                    ->floatNode('max')->defaultValue(null)->end()
                  ->end()
                ->end()
                ->arrayNode('individual')->prototype('variable')->end()->end()
                ->append($this->addEventConfiguration())
              ->end()
            ->end()
          ->end()
        ->end()
      ->end();

    return $treeBuilder;
  }
426516
public function getRootDse()
    {
        $query = $this->newQuery();

        $root = $query->in('')->read()->whereHas($this->schema->objectClass())->first();

        if ($root) {
            return (new RootDse([], $query))
                ->setRawAttributes($root->getAttributes());
        }
    }
1216945
public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root($this->getConfigTreeBuilderRoot());

        $rootNode->children()
            ->scalarNode("baseUrl")->defaultValue("https://primeadv.go2cloud.org/")->end()
            ->integerNode("id")->isRequired()->end()
            ->end();

        return $treeBuilder;
    }
1370362
public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('app');

        $rootNode
            ->addDefaultsIfNotSet()
            ->children()
                ->integerNode('session_lifetime')->defaultValue(7200)->end()
                ->arrayNode('api')
                    ->isRequired()
                    ->cannotBeEmpty()
                    ->children()
                        ->scalarNode('base_url')
                            ->isRequired()
                            ->cannotBeEmpty()
                        ->end()
                        ->scalarNode('api_key')
                            ->isRequired()
                            ->cannotBeEmpty()
                        ->end()
                        ->scalarNode('param_key')->defaultValue('api_key')->end()
                    ->end()
                ->end()
                ->arrayNode('http')
                    ->isRequired()
                    ->cannotBeEmpty()
                    ->children()
                        ->scalarNode('client')->defaultValue(null)->end()
                        ->arrayNode('get')
                            ->addDefaultsIfNotSet()
                            ->children()
                                ->scalarNode('accept')->defaultValue('application/ld+json')->end()
                                ->scalarNode('content_type')->defaultValue('application/ld+json')->end()
                            ->end()
                        ->end()
                        ->arrayNode('post')
                            ->addDefaultsIfNotSet()
                            ->children()
                                ->scalarNode('accept')->defaultValue('application/json')->end()
                                ->scalarNode('content_type')->defaultValue('application/json')->end()
                            ->end()
                        ->end()
                        ->arrayNode('put')
                            ->addDefaultsIfNotSet()
                            ->children()
                                ->scalarNode('accept')->defaultValue('application/json')->end()
                                ->scalarNode('content_type')->defaultValue('application/json')->end()
                            ->end()
                        ->end()
                        ->arrayNode('patch')
                            ->addDefaultsIfNotSet()
                            ->children()
                                ->scalarNode('accept')->defaultValue('application/json')->end()
                                ->scalarNode('content_type')->defaultValue('application/json')->end()
                            ->end()
                        ->end()
                        ->arrayNode('delete')
                            ->addDefaultsIfNotSet()
                            ->children()
                                ->scalarNode('accept')->defaultValue('application/json')->end()
                                ->scalarNode('content_type')->defaultValue('application/json')->end()
                            ->end()
                        ->end()
                    ->end()
                ->end()
                ->arrayNode('middlewares')
                    ->defaultValue([])
                    ->prototype('array')
                        ->children()
                            ->scalarNode('class')
                                ->isRequired()
                                ->cannotBeEmpty()
                            ->end()
                            ->variableNode('parameters')
                                ->defaultValue([])
                            ->end()
                            ->integerNode('priority')->defaultValue(0)->end()
                        ->end()
                    ->end()
                ->end()
                ->arrayNode('event_listeners')
                    ->defaultValue([])
                    ->prototype('array')
                        ->children()
                            ->scalarNode('event')
                                ->isRequired()
                                ->cannotBeEmpty()
                            ->end()
                            ->scalarNode('class')
                                ->isRequired()
                                ->cannotBeEmpty()
                            ->end()
                            ->scalarNode('method')
                                ->isRequired()
                                ->cannotBeEmpty()
                            ->end()
                            ->integerNode('priority')->defaultValue(0)->end()
                        ->end()
                    ->end()
                ->end()
                ->arrayNode('routes')
                    ->isRequired()
                    ->cannotBeEmpty()
                    ->prototype('array')
                        ->children()
                            ->scalarNode('path')
                                ->isRequired()
                                ->cannotBeEmpty()
                            ->end()
                            ->scalarNode('controller')
                                ->isRequired()
                                ->cannotBeEmpty()
                            ->end()
                            ->variableNode('requirements')
                                ->defaultValue([])
                            ->end()
                            ->variableNode('defaults')
                                ->defaultValue([])
                            ->end()
                            ->arrayNode('methods')
                                ->prototype('scalar')
                                    ->beforeNormalization()
                                        ->ifString()
                                        ->then(function ($v) { return strtoupper($v); })
                                    ->end()
                                    ->validate()
                                        ->ifNotInArray(['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'HEAD'])
                                        ->thenInvalid('Invalid HTTP Verbs')
                                    ->end()
                                    ->defaultValue(['GET'])
                                ->end()
                            ->end()
                        ->end()
                    ->end()
                ->end()
                ->arrayNode('template')
                    ->isRequired()
                    ->cannotBeEmpty()
                    ->children()
                        ->scalarNode('engine')->defaultValue(null)->end()
                        ->scalarNode('path')->defaultValue(null)->end()
                        ->scalarNode('cache_dir')->defaultValue(null)->end()
                    ->end()
                ->end()
            ->end()
        ;

        return $treeBuilder;
    }
1244688
public function getTree()
    {
        $nodes = [];
        foreach ($this->nodes[$this->_config['rootid']]->getDescendants() as $subnode) {
            $nodes[] = $subnode;
        }
        return $nodes;
    }
677353
function getRoot() {
    /**
     * Return existing root if found.
     */
    const existingRoot = document.getElementById(ROOT_ID);
    if (existingRoot) {
      return existingRoot;
    }

    /**
     * If no existing root, create and return a new root.
     */
    const newRoot = document.createElement('div');
    newRoot.id = ROOT_ID;
    document.body.appendChild(newRoot);
    return newRoot;
  }
29428
public function getConfigTreeBuilder ()
    {
        $treeBuilder = new TreeBuilder();
        $root = $treeBuilder->root("becklyn_static_roles");

        $root
            ->children()
                ->arrayNode("roles")
                    ->useAttributeAsKey("role")
                    ->prototype('array')
                        ->children()
                            ->scalarNode("title")->isRequired()->end()
                            ->scalarNode("description")->end()
                            ->booleanNode("hidden")->defaultFalse()->end()
                            ->arrayNode("included_roles")
                                ->prototype('scalar')->end()
                            ->end()
                            ->arrayNode("tags")
                                ->prototype('scalar')->end()
                            ->end()
                            ->arrayNode("actions")
                                ->prototype('scalar')->end()
                            ->end()
                        ->end()
                    ->end()
                ->end()
            ->end();

        return $treeBuilder;
    }
1154276
public function getParent(): self
    {
        $parentPath = \dirname($this->getPath());

        if ($parentPath === '.') {
            throw new OutOfRangeException('Cannot get parent of root reference');
        }

        return new self($this->uri->withPath($parentPath), $this->apiClient, $this->validator);
    }
255663
public function getRootContext()
    {
        if ($parentContext = $this->getParentContext()) {
            if ($parentContext instanceof ParentDelegationBuilder) {
                return $parentContext->getRootContext();
            }
            return $parentContext;
        }

        return $this;
    }
588097
public function root()
    {
        $category = $this->createModel();

        $category = $category->whereExists(function($query)
        {
            $query->select(\DB::raw(1))
                  ->from('category_hierarchy')
                  ->whereRaw(DB::getTablePrefix().'categories.id = '.DB::getTablePrefix().'category_hierarchy.category_id')
                  ->where('category_hierarchy.category_parent_id', 0);
        });

        return $category;
    }
725787
public function buildRootResource()
    {
        $language = new ExpressionLanguage();

        $resources = array();
        foreach ($this->resourceConfig as $name => $resource) {
            $resources[] = new Values\Resource(
                $name,
                $resource['mediaType'],
                $language->evaluate($resource['href'], [
                    'router' => $this->router,
                    'templateRouter' => $this->templateRouter,
                ])
            );
        }

        return new Root($resources);
    }
960826
public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder('fos_rest');

        if (method_exists($treeBuilder, 'getRootNode')) {
            $rootNode = $treeBuilder->getRootNode();
        } else {
            $rootNode = $treeBuilder->root('fos_rest');
        }

        $rootNode
            ->children()
                ->scalarNode('disable_csrf_role')->defaultNull()->end()
                ->arrayNode('access_denied_listener')
                    ->canBeEnabled()
                    ->beforeNormalization()
                        ->ifArray()->then(function ($v) {
                            if (!empty($v) && empty($v['formats'])) {
                                unset($v['enabled']);
                                $v = ['enabled' => true, 'formats' => $v];
                            }

                            return $v;
                        })
                    ->end()
                    ->fixXmlConfig('format', 'formats')
                    ->children()
                        ->scalarNode('service')->defaultNull()->end()
                        ->arrayNode('formats')
                            ->useAttributeAsKey('name')
                            ->prototype('boolean')->end()
                        ->end()
                    ->end()
                ->end()
                ->scalarNode('unauthorized_challenge')->defaultNull()->end()
                ->arrayNode('param_fetcher_listener')
                    ->beforeNormalization()
                        ->ifString()
                        ->then(function ($v) {
                            return ['enabled' => in_array($v, ['force', 'true']), 'force' => 'force' === $v];
                        })
                    ->end()
                    ->canBeEnabled()
                    ->children()
                        ->booleanNode('force')->defaultFalse()->end()
                        ->scalarNode('service')->defaultNull()->end()
                    ->end()
                ->end()
                ->scalarNode('cache_dir')->cannotBeEmpty()->defaultValue('%kernel.cache_dir%/fos_rest')->end()
                ->arrayNode('allowed_methods_listener')
                    ->canBeEnabled()
                    ->children()
                        ->scalarNode('service')->defaultNull()->end()
                    ->end()
                ->end()
                ->arrayNode('routing_loader')
                    ->addDefaultsIfNotSet()
                    ->children()
                        ->scalarNode('default_format')->defaultNull()->end()
                        ->scalarNode('prefix_methods')->defaultTrue()->end()
                        ->scalarNode('include_format')->defaultTrue()->end()
                    ->end()
                ->end()
                ->arrayNode('body_converter')
                    ->canBeEnabled()
                    ->children()
                        ->scalarNode('validate')
                            ->defaultFalse()
                            ->beforeNormalization()
                                ->ifTrue()
                                ->then(function ($value) {
                                    if (!class_exists(OptionsResolver::class)) {
                                        throw new InvalidConfigurationException("'body_converter.validate: true' requires OptionsResolver component installation ( composer require symfony/options-resolver )");
                                    }

                                    return $value;
                                })
                            ->end()
                        ->end()
                        ->scalarNode('validation_errors_argument')->defaultValue('validationErrors')->end()
                    ->end()
                ->end()
                ->arrayNode('service')
                    ->addDefaultsIfNotSet()
                    ->children()
                        ->scalarNode('router')->defaultValue('router')->end()
                        ->scalarNode('templating')->defaultValue('templating')->end()
                        ->scalarNode('serializer')->defaultNull()->end()
                        ->scalarNode('view_handler')->defaultValue('fos_rest.view_handler.default')->end()
                        ->scalarNode('inflector')->defaultValue('fos_rest.inflector.doctrine')->end()
                        ->scalarNode('validator')->defaultValue('validator')->end()
                    ->end()
                ->end()
                ->arrayNode('serializer')
                    ->addDefaultsIfNotSet()
                    ->children()
                        ->scalarNode('version')->defaultNull()->end()
                        ->arrayNode('groups')
                            ->prototype('scalar')->end()
                        ->end()
                        ->booleanNode('serialize_null')->defaultFalse()->end()
                    ->end()
                ->end()
                ->arrayNode('zone')
                    ->cannotBeOverwritten()
                    ->prototype('array')
                    ->fixXmlConfig('ip')
                    ->children()
                        ->scalarNode('path')
                            ->defaultNull()
                            ->info('use the urldecoded format')
                            ->example('^/path to resource/')
                        ->end()
                        ->scalarNode('host')->defaultNull()->end()
                        ->arrayNode('methods')
                            ->beforeNormalization()->ifString()->then(function ($v) {
                                return preg_split('/\s*,\s*/', $v);
                            })->end()
                            ->prototype('scalar')->end()
                        ->end()
                        ->arrayNode('ips')
                            ->beforeNormalization()->ifString()->then(function ($v) {
                                return array($v);
                            })->end()
                            ->prototype('scalar')->end()
                        ->end()
                    ->end()
                ->end()
            ->end()
        ->end();

        $this->addViewSection($rootNode);
        $this->addExceptionSection($rootNode);
        $this->addBodyListenerSection($rootNode);
        $this->addFormatListenerSection($rootNode);
        $this->addVersioningSection($rootNode);

        return $treeBuilder;
    }
787078
private function initRoot()
    {
        $rootEntities = $this->queryBuilder->getRootEntities();
        $rootAliases = $this->queryBuilder->getRootAliases();
        if (!count($rootEntities) || !count($rootAliases)) {
            throw new QueryBuilderException('Path "FROM" in query is empty');
        }
        $this->rootEntity = $rootEntities[0];
        $this->rootAlias = $rootAliases[0];
        $this->primary = $this->queryBuilder
            ->getEntityManager()
            ->getClassMetadata($this->rootEntity)
            ->getSingleIdentifierFieldName();

        return $this;
    }
769056
public function getRoot()
    {
        if (!$this->root) {
            $this->root = $this->active->getContext()->getCurrentSiteNode();
        }

        return $this->root;
    }
474439
public function root() : ContainerElement
    {
        // no need to check anything else, we are at the top
        if ($this instanceof Speak) {
            return $this;
        }

        // loop until we find an element that has no parent
        $currentParent = $this->parent;
        while ($currentParent->parent !== null) {
            $currentParent = $currentParent->parent;
        }

        return $currentParent;
    }