Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. CollectionConfigurator.php

class CollectionConfigurator

@author Nicolas Grekas <p@tchwork.com>

Hierarchy

  • class \Symfony\Component\Routing\Loader\Configurator\CollectionConfigurator uses \Traits\AddTrait, \Traits\HostTrait, \Traits\RouteTrait

Expanded class hierarchy of CollectionConfigurator

1 file declares its use of CollectionConfigurator
AddTrait.php in vendor/symfony/routing/Loader/Configurator/Traits/AddTrait.php

File

vendor/symfony/routing/Loader/Configurator/CollectionConfigurator.php, line 20

Namespace

Symfony\Component\Routing\Loader\Configurator
View source
class CollectionConfigurator {
    use Traits\AddTrait;
    use Traits\HostTrait;
    use Traits\RouteTrait;
    private string|array|null $host = null;
    public function __construct(RouteCollection $parent, string $name, ?self $parentConfigurator = null, ?array $parentPrefixes = null) {
        $this->name = $name;
        $this->collection = new RouteCollection();
        $this->route = new Route('');
    }
    public function __sleep() : array {
        throw new \BadMethodCallException('Cannot serialize ' . __CLASS__);
    }
    public function __wakeup() : void {
        throw new \BadMethodCallException('Cannot unserialize ' . __CLASS__);
    }
    public function __destruct() {
        if (null === $this->prefixes) {
            $this->collection
                ->addPrefix($this->route
                ->getPath());
        }
        if (null !== $this->host) {
            $this->addHost($this->collection, $this->host);
        }
        $this->parent
            ->addCollection($this->collection);
    }
    
    /**
     * Creates a sub-collection.
     */
    public final function collection(string $name = '') : self {
        return new self($this->collection, $this->name . $name, $this, $this->prefixes);
    }
    
    /**
     * Sets the prefix to add to the path of all child routes.
     *
     * @param string|array $prefix the prefix, or the localized prefixes
     *
     * @return $this
     */
    public final function prefix(string|array $prefix) : static {
        if (\is_array($prefix)) {
            if (null === $this->parentPrefixes) {
                // no-op
            }
            elseif ($missing = array_diff_key($this->parentPrefixes, $prefix)) {
                throw new \LogicException(\sprintf('Collection "%s" is missing prefixes for locale(s) "%s".', $this->name, implode('", "', array_keys($missing))));
            }
            else {
                foreach ($prefix as $locale => $localePrefix) {
                    if (!isset($this->parentPrefixes[$locale])) {
                        throw new \LogicException(\sprintf('Collection "%s" with locale "%s" is missing a corresponding prefix in its parent collection.', $this->name, $locale));
                    }
                    $prefix[$locale] = $this->parentPrefixes[$locale] . $localePrefix;
                }
            }
            $this->prefixes = $prefix;
            $this->route
                ->setPath('/');
        }
        else {
            $this->prefixes = null;
            $this->route
                ->setPath($prefix);
        }
        return $this;
    }
    
    /**
     * Sets the host to use for all child routes.
     *
     * @param string|array $host the host, or the localized hosts
     *
     * @return $this
     */
    public final function host(string|array $host) : static {
        $this->host = $host;
        return $this;
    }
    
    /**
     * This method overrides the one from LocalizedRouteTrait.
     */
    private function createRoute(string $path) : Route {
        return (clone $this->route)
            ->setPath($path);
    }

}

Members

Title Sort descending Modifiers Object type Summary
CollectionConfigurator::$host private property
CollectionConfigurator::collection final public function Creates a sub-collection.
CollectionConfigurator::createRoute private function This method overrides the one from LocalizedRouteTrait.
CollectionConfigurator::host final public function Sets the host to use for all child routes.
CollectionConfigurator::prefix final public function Sets the prefix to add to the path of all child routes.
CollectionConfigurator::__construct public function
CollectionConfigurator::__destruct public function
CollectionConfigurator::__sleep public function
CollectionConfigurator::__wakeup public function

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal