You are here

class DumperPrefixCollection in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/routing/Matcher/Dumper/DumperPrefixCollection.php \Symfony\Component\Routing\Matcher\Dumper\DumperPrefixCollection

Prefix tree of routes preserving routes order.

@author Arnaud Le Blanc <arnaud.lb@gmail.com>

@internal

Hierarchy

  • class \Symfony\Component\Routing\Matcher\Dumper\DumperCollection implements \Symfony\Component\Routing\Matcher\Dumper\IteratorAggregate

Expanded class hierarchy of DumperPrefixCollection

1 file declares its use of DumperPrefixCollection
DumperPrefixCollectionTest.php in vendor/symfony/routing/Tests/Matcher/Dumper/DumperPrefixCollectionTest.php

File

vendor/symfony/routing/Matcher/Dumper/DumperPrefixCollection.php, line 21

Namespace

Symfony\Component\Routing\Matcher\Dumper
View source
class DumperPrefixCollection extends DumperCollection {

  /**
   * @var string
   */
  private $prefix = '';

  /**
   * Returns the prefix.
   *
   * @return string The prefix
   */
  public function getPrefix() {
    return $this->prefix;
  }

  /**
   * Sets the prefix.
   *
   * @param string $prefix The prefix
   */
  public function setPrefix($prefix) {
    $this->prefix = $prefix;
  }

  /**
   * 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');
  }

  /**
   * Merges nodes whose prefix ends with a slash.
   *
   * Children of a node whose prefix ends with a slash are moved to the parent node
   */
  public function mergeSlashNodes() {
    $children = array();
    foreach ($this as $child) {
      if ($child instanceof self) {
        $child
          ->mergeSlashNodes();
        if ('/' === substr($child->prefix, -1)) {
          $children = array_merge($children, $child
            ->all());
        }
        else {
          $children[] = $child;
        }
      }
      else {
        $children[] = $child;
      }
    }
    $this
      ->setAll($children);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DumperCollection::$attributes private property
DumperCollection::$children private property
DumperCollection::$parent private property
DumperCollection::add public function Adds a route or collection.
DumperCollection::all public function Returns the children routes and collections.
DumperCollection::getAttribute public function Returns an attribute by name.
DumperCollection::getIterator public function Returns an iterator over the children.
DumperCollection::getParent protected function Returns the parent collection.
DumperCollection::getRoot public function Returns the root of the collection.
DumperCollection::hasAttribute public function Returns true if the attribute is defined.
DumperCollection::setAll public function Sets children.
DumperCollection::setAttribute public function Sets an attribute by name.
DumperCollection::setAttributes public function Sets multiple attributes.
DumperCollection::setParent protected function Sets the parent collection.
DumperPrefixCollection::$prefix private property
DumperPrefixCollection::addPrefixRoute public function Adds a route in the tree.
DumperPrefixCollection::getPrefix public function Returns the prefix.
DumperPrefixCollection::mergeSlashNodes public function Merges nodes whose prefix ends with a slash.
DumperPrefixCollection::setPrefix public function Sets the prefix.