Get the Namespace root for Entity Relations

@param string $projectRootNamespace
@param array  $subDirectories

@return string
10816
public function getOwningRelationsRootFqn(
        string $projectRootNamespace,
        array $subDirectories
    ): string {
        $relationsRootFqn = $projectRootNamespace
                            . AbstractGenerator::ENTITY_RELATIONS_NAMESPACE . '\\';
        if (count($subDirectories) > 0) {
            $relationsRootFqn .= implode('\\', $subDirectories) . '\\';
        }

        return $this->tidy($relationsRootFqn);
    }
595961
private function namespaceDefinitions(array $definitions, Endpoint $endpoint): array
    {
        $namespacedDefinitions = [];
        /** @var DefinitionInterface $definition */
        foreach ($definitions as $definition) {
            if (!$definition->hasMeta('namespace') || !$definition->getMeta('namespace')) {
                $namespacedDefinitions[$definition->getName()] = $definition;
                continue;
            }

            $root = null;
            $parent = null;
            $namespaceConfig = $definition->getMeta('namespace');
            $namespacePath = $namespaceConfig['namespace'] ?? null;
            if ($namespacePath) {
                $querySuffix = $this->globalConfig['nodes']['query_suffix'] ?? 'Query';
                $mutationSuffix = $this->globalConfig['nodes']['mutation_suffix'] ?? 'Mutation';
                $subscriptionSuffix = $this->globalConfig['nodes']['subscription_suffix'] ?? 'Subscription';
                $namespaces = explode('/', $namespacePath);
                foreach ($namespaces as $namespace) {
                    $name = lcfirst($namespace);
                    $suffix = $querySuffix;
                    if ($definition instanceof MutationDefinition) {
                        $suffix = $mutationSuffix;
                    }
                    if ($definition instanceof SubscriptionDefinition) {
                        $suffix = $subscriptionSuffix;
                    }
                    $typeName = ucfirst($name).$suffix;
                    if (!$root) {
                        if (isset($namespacedDefinitions[$name])) {
                            $root = $namespacedDefinitions[$name];
                        } else {
                            $root = $this->createRootNamespace($definition, $name, $typeName, $endpoint);
                        }
                        $parent = $endpoint->getType($root->getType());
                        $namespacedDefinitions[$root->getName()] = $root;
                    } else {
                        $parent = $this->createChildNamespace($parent, $name, $typeName, $endpoint);
                    }
                }
                if ($alias = $namespaceConfig['alias'] ?? null) {
                    $definition->setName($alias);
                }
                $this->addDefinitionToNamespace($parent, $definition, $definition->getName());
                continue;
            }

            if ($bundle = $namespaceConfig['bundle'] ?? null) {
                $bundleQuerySuffix = $this->globalConfig['bundle']['query_suffix'] ?? 'BundleQuery';
                $bundleMutationSuffix = $this->globalConfig['bundle']['mutation_suffix'] ?? 'BundleMutation';
                $bundleSubscriptionSuffix = $this->globalConfig['bundle']['subscription_suffix'] ?? 'BundleSubscription';
                $name = lcfirst($bundle);
                $suffix = $bundleQuerySuffix;
                if ($definition instanceof MutationDefinition) {
                    $suffix = $bundleMutationSuffix;
                }
                if ($definition instanceof SubscriptionDefinition) {
                    $suffix = $bundleSubscriptionSuffix;
                }
                $typeName = ucfirst($name).$suffix;
                $root = $this->createRootNamespace($definition, $name, $typeName, $endpoint);
                $parent = $endpoint->getType($root->getType());
            }

            if ($nodeName = $namespaceConfig['node'] ?? null) {
                if ($endpoint->hasTypeForClass($nodeName)) {
                    $nodeName = $endpoint->getTypeForClass($nodeName);
                }

                $name = Inflector::pluralize(lcfirst($nodeName));

                $querySuffix = $this->globalConfig['nodes']['query_suffix'] ?? 'Query';
                $mutationSuffix = $this->globalConfig['nodes']['mutation_suffix'] ?? 'Mutation';
                $subscriptionSuffix = $this->globalConfig['nodes']['subscription_suffix'] ?? 'Subscription';

                $suffix = $querySuffix;
                if ($definition instanceof MutationDefinition) {
                    $suffix = $mutationSuffix;
                }
                if ($definition instanceof SubscriptionDefinition) {
                    $suffix = $subscriptionSuffix;
                }

                $typeName = ucfirst($nodeName).$suffix;
                if (!$root) {
                    $root = $this->createRootNamespace($definition, $name, $typeName, $endpoint);
                    $parent = $endpoint->getType($root->getType());
                } elseif ($parent) {
                    $parent = $this->createChildNamespace($parent, $name, $typeName, $endpoint);
                }

                if ($alias = $namespaceConfig['alias'] ?? null) {
                    $originName = $definition->getName();
                    $definition->setName($alias);
                } else {
                    //remove node suffix on namespaced definitions
                    $originName = $definition->getName();
                    $definition->setName(preg_replace(sprintf("/(\w+)%s$/", $nodeName), '$1', $definition->getName()));
                    $definition->setName(preg_replace(sprintf("/(\w+)%s$/", Inflector::pluralize($nodeName)), '$1', $definition->getName()));
                }
            }

            if ($root && $parent) {
                $this->addDefinitionToNamespace($parent, $definition, $originName);
                $namespacedDefinitions[$root->getName()] = $root;
            } else {
                $namespacedDefinitions[$definition->getName()] = $definition;
            }
        }

        return $namespacedDefinitions;
    }
963932
public function getRootNamespace(): string
    {
        if (! is_null(static::$namespace)) {
            return static::$namespace;
        }

        $composer = Json::decode(file_get_contents($this->getRootPath() . '/composer.json'));

        foreach ((array) Arr::get($composer, 'autoload.psr-4', []) as $namespace => $paths) {
            foreach ((array) $paths as $path) {
                if (realpath($this->getAppPath()) === realpath($this->getRootPath() . '/' . $path)) {
                    return static::$namespace = $namespace;
                }
            }
        }


        unset($composer);

        return '';
    }
543510
public function getNamespace($namespace, $documentsDirectory = null)
    {
        if (!$documentsDirectory) {
            $documentsDirectory = $this->documentDir;
        }

        if (strpos($namespace, ':') !== false) {
            list($bundle, $document) = explode(':', $namespace);
            $bundle = $this->getBundleClass($bundle);

            // If bundle has a sub-namespace it needs to be replaced
            if (strpos($documentsDirectory, '\\')) {
                $bundleSubNamespace = substr(
                    $bundle,
                    $start = strpos($bundle, '\\') + 1,
                    strrpos($bundle, '\\') - $start + 1
                );

                $documentsDirectory = str_replace(
                    $bundleSubNamespace,
                    '',
                    $documentsDirectory
                );
            }

            $namespace = substr($bundle, 0, strrpos($bundle, '\\')) . '\\' .
                $documentsDirectory . '\\' . $document;
        }

        return $namespace;
    }
1256445
function from($namespacePath)
    {
        if ($namespacePath === '')
            # from recursion calls
            return $this;

        // ..

        $cNamespace = $this->_normalizeNamespace($namespacePath);

        if (false === strstr($cNamespace, self::SEPARATOR)) {
            ## recursion fallback here !!
            if (!isset($this->__nestRight[$cNamespace]))
                return false;

            return $this->__nestRight[$cNamespace];
        }

        // ..

        $namespacePath = rtrim($namespacePath, self::SEPARATOR);
        $brkNamespace  = explode(self::SEPARATOR, $namespacePath);


        ## /root/to/nested/namespace
        $cNamespace   = array_shift($brkNamespace);
        $cContainer   = $this;
        if ($cNamespace === '') {
            ## Start with / (separator)
            #- Goto Root Container
            while ($cContainer->__nestLeft)
                $cContainer = $cContainer->__nestLeft;
        }
        else $cContainer = $this->from($cNamespace); ## fallback

        ## fallback recursion from root parent
        return ($cContainer == false) ? $cContainer : $cContainer->from(implode(self::SEPARATOR, $brkNamespace));
    }
1082942
protected function getDefaultNamespace($rootNamespace)
    {
        $namespace = Config::get('service-classes.cached_services.namespace');
        $serviceNamespace = Config::get('service-classes.namespace');

        if (! $serviceNamespace) {
            throw InvalidNamespaceException::missingServiceNamespace();
        }

        return $this->defaultNamespace = $namespace ? $rootNamespace.'\\'.Config::get('service-classes.namespace').'\\'.$namespace
                      : $rootNamespace.'\\'.Config::get('service-classes.cached_services.namespace');
    }
1145641
public static function getNamespace($tokens)
    {
        $count = count($tokens);
        $i = 0;
        $namespace = '';
        $namespace_ok = false;
        while ($i < $count) {
            $token = $tokens[$i];
            if (is_array($token) && $token[0] === T_NAMESPACE) {
                // Found namespace declaration
                while (++$i < $count) {
                    if ($tokens[$i] === ';') {
                        $namespace_ok = true;
                        $namespace = trim($namespace);
                        break;
                    }
                    $namespace .= is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i];
                }
                break;
            }
            $i++;
        }
        if (!$namespace_ok) {
            return null;
        } else {
            return $namespace;
        }
    }
906929
public static function pickRootDir($path)
    {
        // when relational path.
        if ($path[0] !== '/') return $path;
        
        $f = function($names, $count) {
            for ($i = 0; $i < $count; $i++) {
                array_pop($names);
            }
            return $names;
        };

        $root = null;
        foreach (self::$namespaces as $p => $ns) {
            $d = join(DS, $f(explode(DS, $p), count(explode('\\', $ns))));
            if (strpos($path, $d) === 0) {
                if ($root === null || strlen($root) < strlen($d)) $root = $d;
            }
        }

        return $root ? $root : $path;
    }
707398
public function getMainPrefixAndPath(Dir $rootDir) {
    $prefixesPaths = $this->getPrefixes();
    $prefixes = array_keys($prefixesPaths);
    
    if (count($prefixes) > 0) {
      $firstPrefixPaths = array_shift($prefixesPaths);

      $path = array_shift($firstPrefixPaths);
      $dir = $path instanceof Dir ? $path : $rootDir->sub($path);
    
      return array(array_shift($prefixes), $dir);
    }
    
    throw new NoAutoLoadPrefixException('Cannot retrieve the main Prefix and Library Path from AutoLoadInfo. AutoLoadInfo is defined: '.print_r($prefixesPaths, true)."\nPlease insert autoload informations into your composer.json");
  }
1222001
private function scanRoot(NamespaceInterface $root): Generator
    {
        foreach ($root->iterateNamespaces() as $namespace) {

            // If you find a project readme, this is a project. Scan it for packages and sub-packages.
            $readMes = new \Symfony\Component\Finder\Finder();
            $readMes->files()
                    ->in($namespace->getPaths())
                    ->depth(0)
                    ->name('project.md');

            if ($readMes->count()) {
                yield from $this->scanNamespaceForPackages($namespace);
                return;
            }

            // Otherwise, this must be a library. Scan it as a package itself.
            yield from $this->loadPackage($namespace);
        }
    }
177745
protected function getRootNameSpace()
    {
        if (!$namespace = $this->option('namespace')) {
            list($vendor, $name) = explode('/', $this->package);

            $default = str_replace(['-', '-'], '', Str::title($vendor).'\\'.Str::title($name));

            $namespace = $this->ask('Root namespace', $default);
        }

        return $namespace;
    }
174467
public function getNamespacedName() : ResolvedName {
        $namespaceDefinition = $this->getNamespaceDefinition();
        $content = $this->getFileContents();
        if ($namespaceDefinition === null) {
            // global namespace -> strip namespace\ prefix
            return ResolvedName::buildName($this->getNameParts(), $content);
        }

        if ($namespaceDefinition->name !== null) {
            $resolvedName = ResolvedName::buildName($namespaceDefinition->name->nameParts, $content);
        } else {
            $resolvedName = ResolvedName::buildName([], $content);
        }
        if (
            !($this instanceof QualifiedName && (
            ($this->parent instanceof NamespaceDefinition) ||
            ($this->parent instanceof NamespaceUseDeclaration) ||
            ($this->parent instanceof NamespaceUseClause) ||
            ($this->parent instanceof NamespaceUseGroupClause)))
        ) {
            $resolvedName->addNameParts($this->getNameParts(), $content);
        }
        return $resolvedName;
    }
651219
public function getPluralNamespacedName(string $entityFqn, array $subDirectories): string
    {
        $plural = \ucfirst(MappingHelper::getPluralForFqn($entityFqn));

        return $this->getNamespacedName($plural, $subDirectories);
    }
892218
protected function getRootControllerNamespace()
    {
        $urlGenerator = $this->app['url'];
        $reflection = new \ReflectionClass($urlGenerator);

        $property = $reflection->getProperty('rootNamespace');
        $property->setAccessible(true);

        return $property->getValue($urlGenerator);
    }
1128755
public function getProjectNamespaceRootFromEntityFqn(string $entityFqn): string
    {
        return $this->tidy(
            \substr(
                $entityFqn,
                0,
                \strrpos(
                    $entityFqn,
                    '\\' . AbstractGenerator::ENTITIES_FOLDER_NAME . '\\'
                )
            )
        );
    }
569375
public function getEntityNamespace()
    {
        $namespace = '';
        if (($bundleNamespace = $this->parseComment('bundleNamespace')) || ($bundleNamespace = $this->getConfig()->get(Formatter::CFG_BUNDLE_NAMESPACE))) {
            $namespace = $bundleNamespace.'\\';
        }
        if ($entityNamespace = $this->getConfig()->get(Formatter::CFG_ENTITY_NAMESPACE)) {
            $namespace .= $entityNamespace;
        } else {
            $namespace .= 'Entity';
        }

        return $namespace;
    }
884387
private function fetchNamespaces (NamespaceRegistry $entryNamespaces) : array
    {
        $namespaces = [];

        foreach ($entryNamespaces as $namespace => $path)
        {
            $namespaces[$namespace] = $this->makePathRelative($path);
        }

        return $namespaces;
    }