getMatcher example

$router = new Router($container, 'with_condition.yaml', [
                'debug' => true,
                'cache_dir' => $cacheDir,
            ]);

            $resourceCheckers = [
                new ContainerParametersResourceChecker($container),
            ];

            $router->setConfigCacheFactory(new ResourceCheckerConfigCacheFactory($resourceCheckers));

            $router->getMatcher(); // trigger cache build
            $cache = new ResourceCheckerConfigCache($cacheDir.\DIRECTORY_SEPARATOR.'url_matching_routes.php', $resourceCheckers);

            $this->assertTrue($cache->isFresh());
        } finally {
            if (is_dir($cacheDir)) {
                array_map('unlink', glob($cacheDir.\DIRECTORY_SEPARATOR.'*'));
                rmdir($cacheDir);
            }
        }
    }

    
$this->assertSame($routeCollection$this->router->getRouteCollection());
    }

    public function testMatcherIsCreatedIfCacheIsNotConfigured()
    {
        $this->router->setOption('cache_dir', null);

        $this->loader->expects($this->once())
            ->method('load')->with('routing.yml', null)
            ->willReturn(new RouteCollection());

        $this->assertInstanceOf(UrlMatcher::class$this->router->getMatcher());
    }

    public function testGeneratorIsCreatedIfCacheIsNotConfigured()
    {
        $this->router->setOption('cache_dir', null);

        $this->loader->expects($this->once())
            ->method('load')->with('routing.yml', null)
            ->willReturn(new RouteCollection());

        $this->assertInstanceOf(CompiledUrlGenerator::class$this->router->getGenerator());
    }
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

class RedirectableCompiledUrlMatcherTest extends TestCase
{
    public function testRedirectWhenNoSlash()
    {
        $routes = new RouteCollection();
        $routes->add('foo', new Route('/foo/'));

        $matcher = $this->getMatcher($routes$context = new RequestContext());

        $this->assertEquals([
                '_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction',
                'path' => '/foo/',
                'permanent' => true,
                'scheme' => null,
                'httpPort' => $context->getHttpPort(),
                'httpsPort' => $context->getHttpsPort(),
                '_route' => 'foo',
            ],
            $matcher->match('/foo')
        );
return $this->collection ??= $this->loader->load($this->resource, $this->options['resource_type']);
    }

    /** * @return void */
    public function setContext(RequestContext $context)
    {
        $this->context = $context;

        if (null !== $this->matcher) {
            $this->getMatcher()->setContext($context);
        }
        if (null !== $this->generator) {
            $this->getGenerator()->setContext($context);
        }
    }

    public function getContext(): RequestContext
    {
        return $this->context;
    }

    
return $this->collection ??= $this->loader->load($this->resource, $this->options['resource_type']);
    }

    /** * @return void */
    public function setContext(RequestContext $context)
    {
        $this->context = $context;

        if (isset($this->matcher)) {
            $this->getMatcher()->setContext($context);
        }
        if (isset($this->generator)) {
            $this->getGenerator()->setContext($context);
        }
    }

    public function getContext(): RequestContext
    {
        return $this->context;
    }

    


    /** * @return string[] A list of classes to preload on PHP 7.4+ */
    public function warmUp(string $cacheDir): array
    {
        $currentDir = $this->getOption('cache_dir');

        // force cache generation         $this->setOption('cache_dir', $cacheDir);
        $this->getMatcher();
        $this->getGenerator();

        $this->setOption('cache_dir', $currentDir);

        return [
            $this->getOption('generator_class'),
            $this->getOption('matcher_class'),
        ];
    }

    /** * Replaces placeholders with service container parameter values in: * - the route defaults, * - the route requirements, * - the route path, * - the route host, * - the route schemes, * - the route methods. */
Home | Imprint | This part of the site doesn't use cookies.