/** * Adds a route to the node tree. * * @param \Viserio\Routing\Generator\RouteTreeNode $node * @param \Viserio\Contracts\Routing\Route $route * @param array $segments * @param int $segmentDepth * @param array $parameterIndexNameMap */ protected function addRouteToNode(RouteTreeNode $node, RouteContract $route, array $segments, int $segmentDepth, array $parameterIndexNameMap) { if (empty($segments)) { $node->getContents()->addRoute($route, $parameterIndexNameMap); return; } $childSegmentMatcher = $this->getMatcher(array_shift($segments), $parameterIndexNameMap); if ($node->getContents()->hasChildFor($childSegmentMatcher)) { $child = $node->getContents()->getChild($childSegmentMatcher); } else { $child = new RouteTreeNode([$segmentDepth => $childSegmentMatcher], empty($segments) ? new MatchedRouteDataMap() : new ChildrenNodeCollection()); $node->getContents()->addChild($child); } $this->addRouteToNode($child, $route, $segments, $segmentDepth + 1, $parameterIndexNameMap); }
public function testMatchedRouteDataMapOperations() { $node = new RouteTreeNode([$this->mock(AbstractMatcher::class)], new MatchedRouteDataMap()); $node->getContents()->addRoute(new Route(['GET', 'POST'], '', null), []); $this->assertSame(['GET', 'POST', 'HEAD'], $node->getContents()->getAllowedHttpMethods()); $this->assertEquals([[['GET', 'POST', 'HEAD'], [[], 'GET|POST|HEAD']]], $node->getContents()->getHttpMethodRouteDataMap()); $node->getContents()->addRoute(new Route('PATCH', '', null), [0 => 'param']); $this->assertSame(['GET', 'POST', 'HEAD', 'PATCH'], $node->getContents()->getAllowedHttpMethods()); $this->assertEquals([[['GET', 'POST', 'HEAD'], [[], 'GET|POST|HEAD']], [['PATCH'], [[0 => 'param'], 'PATCH']]], $node->getContents()->getHttpMethodRouteDataMap()); }
/** * @param \Viserio\Routing\Generator\RouteTreeNode $node */ public function addChild(RouteTreeNode $node) { $hash = $node->getFirstMatcher()->getHash(); $this->children[$hash] = $node; }
/** * @param \Viserio\Routing\Generator\RouteTreeNode $node1 * @param \Viserio\Routing\Generator\RouteTreeNode $node2 * * @return \Viserio\Routing\Generator\RouteTreeNode|null */ protected function extractCommonParentNode(RouteTreeNode $node1, RouteTreeNode $node2) { $matcherCompare = function (SegmentMatcherContract $matcher, SegmentMatcherContract $matcher2) { return strcmp($matcher->getHash(), $matcher2->getHash()); }; $commonMatchers = array_uintersect_assoc($node1->getMatchers(), $node2->getMatchers(), $matcherCompare); if (empty($commonMatchers)) { return; } $children = []; $nodes = [$node1, $node2]; foreach ($nodes as $node) { $specificMatchers = array_udiff_assoc($node->getMatchers(), $commonMatchers, $matcherCompare); $duplicateMatchers = array_uintersect_assoc($node->getMatchers(), $commonMatchers, $matcherCompare); foreach ($duplicateMatchers as $segmentDepth => $matcher) { $commonMatchers[$segmentDepth]->mergeParameterKeys($matcher); } if (empty($specificMatchers) && $node->isParentNode()) { foreach ($node->getContents()->getChildren() as $childNode) { $children[] = $childNode; } } else { $children[] = $node->update($specificMatchers, $node->getContents()); } } return new RouteTreeNode($commonMatchers, new ChildrenNodeCollection($children)); }