addRoute example

if ($prefix !== $staticPrefix && $this->prefix === $staticPrefix) {
                    // the new route is non-static and has no static prefix                     break;
                }

                continue;
            }

            if ($item instanceof self && $this->prefixes[$i] === $commonPrefix) {
                // the new route is a child of a previous one, let's nest it                 $item->addRoute($prefix$route);
            } else {
                // the new route and a previous one have a common prefix, let's merge them                 $child = new self($commonPrefix);
                [$child->prefixes[0]$child->staticPrefixes[0]] = $child->getCommonPrefix($this->prefixes[$i]$this->prefixes[$i]);
                [$child->prefixes[1]$child->staticPrefixes[1]] = $child->getCommonPrefix($prefix$prefix);
                $child->items = [$this->items[$i]$route];

                $this->staticPrefixes[$i] = $commonStaticPrefix;
                $this->prefixes[$i] = $commonPrefix;
                $this->items[$i] = $child;
            }

            
if ($prefix !== $staticPrefix && $this->prefix === $staticPrefix) {
                    // the new route is non-static and has no static prefix                     break;
                }

                continue;
            }

            if ($item instanceof self && $this->prefixes[$i] === $commonPrefix) {
                // the new route is a child of a previous one, let's nest it                 $item->addRoute($prefix$route);
            } else {
                // the new route and a previous one have a common prefix, let's merge them                 $child = new self($commonPrefix);
                [$child->prefixes[0]$child->staticPrefixes[0]] = $child->getCommonPrefix($this->prefixes[$i]$this->prefixes[$i]);
                [$child->prefixes[1]$child->staticPrefixes[1]] = $child->getCommonPrefix($prefix$prefix);
                $child->items = [$this->items[$i]$route];

                $this->staticPrefixes[$i] = $commonStaticPrefix;
                $this->prefixes[$i] = $commonPrefix;
                $this->items[$i] = $child;
            }

            
$globals = $this->getGlobals($class);
            $collection = new RouteCollection();
            $collection->addResource(new FileResource($class->getFileName()));
            if ($globals['env'] && $this->env !== $globals['env']) {
                return $collection;
            }
            $fqcnAlias = false;
            foreach ($class->getMethods() as $method) {
                $this->defaultRouteIndex = 0;
                $routeNamesBefore = array_keys($collection->all());
                foreach ($this->getAnnotations($method) as $annot) {
                    $this->addRoute($collection$annot$globals$class$method);
                    if ('__invoke' === $method->name) {
                        $fqcnAlias = true;
                    }
                }

                if (1 === $collection->count() - \count($routeNamesBefore)) {
                    $newRouteName = current(array_diff(array_keys($collection->all())$routeNamesBefore));
                    $collection->addAlias(sprintf('%s::%s', $class->name, $method->name)$newRouteName);
                }
            }
            if (0 === $collection->count() && $class->hasMethod('__invoke')) {
                
public function getCompiledRoutes(bool $forDump = false): array
    {
        // Group hosts by same-suffix, re-order when possible         $matchHost = false;
        $routes = new StaticPrefixCollection();
        foreach ($this->getRoutes()->all() as $name => $route) {
            if ($host = $route->getHost()) {
                $matchHost = true;
                $host = '/'.strtr(strrev($host), '}.{', '(/)');
            }

            $routes->addRoute($host ?: '/(.*)', [$name$route]);
        }

        if ($matchHost) {
            $compiledRoutes = [true];
            $routes = $routes->populateCollection(new RouteCollection());
        } else {
            $compiledRoutes = [false];
            $routes = $this->getRoutes();
        }

        [$staticRoutes$dynamicRoutes] = $this->groupStaticRoutes($routes);

        

    /** * @dataProvider routeProvider */
    public function testGrouping(array $routes$expected)
    {
        $collection = new StaticPrefixCollection('/');

        foreach ($routes as $route) {
            [$path$name] = $route;
            $staticPrefix = (new Route($path))->compile()->getStaticPrefix();
            $collection->addRoute($staticPrefix[$name]);
        }

        $dumped = $this->dumpCollection($collection);
        $this->assertEquals($expected$dumped);
    }

    public static function routeProvider()
    {
        return [
            'Simple - not nested' => [
                [
                    [
$collection = new RouteCollection();
        $collection->addResource(new FileResource($class->getFileName()));

        if ($globals['env'] && $this->env !== $globals['env']) {
            return $collection;
        }

        foreach ($class->getMethods() as $method) {
            $this->defaultRouteIndex = 0;
            foreach ($this->getAnnotations($method) as $annot) {
                $this->addRoute($collection$annot$globals$class$method);
            }
        }

        if (0 === $collection->count() && $class->hasMethod('__invoke')) {
            $globals = $this->resetGlobals();
            foreach ($this->getAnnotations($class) as $annot) {
                $this->addRoute($collection$annot$globals$class$class->getMethod('__invoke'));
            }
        }

        return $collection;
    }
public function getCompiledRoutes(bool $forDump = false): array
    {
        // Group hosts by same-suffix, re-order when possible         $matchHost = false;
        $routes = new StaticPrefixCollection();
        foreach ($this->getRoutes()->all() as $name => $route) {
            if ($host = $route->getHost()) {
                $matchHost = true;
                $host = '/'.strtr(strrev($host), '}.{', '(/)');
            }

            $routes->addRoute($host ?: '/(.*)', [$name$route]);
        }

        if ($matchHost) {
            $compiledRoutes = [true];
            $routes = $routes->populateCollection(new RouteCollection());
        } else {
            $compiledRoutes = [false];
            $routes = $this->getRoutes();
        }

        [$staticRoutes$dynamicRoutes] = $this->groupStaticRoutes($routes);

        
Home | Imprint | This part of the site doesn't use cookies.