/** * @param \Viserio\Routing\Generator\ChildrenNodeCollection $nodeCollection * * @return \Viserio\Routing\Generator\ChildrenNodeCollection */ protected function moveCommonMatchersToParentNode(ChildrenNodeCollection $nodeCollection) : ChildrenNodeCollection { $nodes = $nodeCollection->getChildren(); if (count($nodes) <= 1) { return $nodeCollection; } $children = []; $previous = array_shift($nodes); foreach ($nodes as $node) { $parent = $this->extractCommonParentNode($previous, $node); if ($parent) { $previous = $parent; } else { $children[] = $previous; $previous = $node; } } $children[] = $previous; return new ChildrenNodeCollection($children); }
/** * Comple the segemtns nodes to if statements. * * @param object $code * @param ChildrenNodeCollection $nodes * @param array $segmentVariables * @param array $parameters */ protected function compileSegmentNodes($code, ChildrenNodeCollection $nodes, array $segmentVariables, array $parameters = []) { $originalParameters = $parameters; foreach ($nodes->getChildren() as $node) { $parameters = $originalParameters; $segmentMatchers = $node->getMatchers(); $conditions = []; $currentParameter = empty($parameters) ? 0 : max(array_keys($parameters)) + 1; $count = $currentParameter; foreach ($segmentMatchers as $segmentDepth => $matcher) { $conditions[] = $matcher->getConditionExpression($segmentVariables[$segmentDepth], $count++); } $code->appendLine('if (' . implode(' && ', $conditions) . ') {'); ++$code->indent; $count = $currentParameter; foreach ($segmentMatchers as $segmentDepth => $matcher) { $matchedParameters = $matcher->getMatchedParameterExpressions($segmentVariables[$segmentDepth], $count++); foreach ($matchedParameters as $parameterKey => $matchedParameter) { $parameters[$parameterKey] = $matchedParameter; } } $contents = $node->getContents(); if ($contents instanceof MatchedRouteDataMap) { $this->compiledRouteHttpMethodMatch($code, $contents, $parameters); } else { $this->compileSegmentNodes($code, $contents, $segmentVariables, $parameters); } --$code->indent; $code->appendLine('}'); } }