/** * Adds a route in the tree. * * @param DumperRoute $route * The route * * @return DumperPrefixCollection The node the route was added to * * @throws \LogicException */ public function addPrefixRoute(DumperRoute $route) { $prefix = $route->getRoute()->compile()->getStaticPrefix(); for ($collection = $this; null !== $collection; $collection = $collection->getParent()) { // Same prefix, add to current leave if ($collection->prefix === $prefix) { $collection->add($route); return $collection; } // Prefix starts with route's prefix if ('' === $collection->prefix || 0 === strpos($prefix, $collection->prefix)) { $child = new self(); $child->setPrefix(substr($prefix, 0, strlen($collection->prefix) + 1)); $collection->add($child); return $child->addPrefixRoute($route); } } // Reached only if the root has a non empty prefix throw new \LogicException('The collection root must not have a prefix'); }
/** * Adds a route in the tree. * * @param DumperRoute $route The route * * @return DumperPrefixCollection The node the route was added to */ public function addPrefixRoute(DumperRoute $route) { $prefix = $route->getRoute()->compile()->getStaticPrefix(); // Same prefix, add to current leave if ($this->prefix === $prefix) { $this->add($route); return $this; } // Prefix starts with route's prefix if ('' === $this->prefix || 0 === strpos($prefix, $this->prefix)) { $collection = new DumperPrefixCollection(); $collection->setPrefix(substr($prefix, 0, strlen($this->prefix) + 1)); $this->add($collection); return $collection->addPrefixRoute($route); } // No match, fallback to parent (recursively) if (null === ($parent = $this->getParent())) { throw new \LogicException("The collection root must not have a prefix"); } return $parent->addPrefixRoute($route); }