matchPath example


    private array $schemes = [];

    /** * @param string|string[]|null $methods * @param string|string[]|null $ips * @param string|string[]|null $schemes */
    public function __construct(string $path = null, string $host = null, string|array $methods = null, string|array $ips = null, array $attributes = [], string|array $schemes = null, int $port = null)
    {
        $this->matchPath($path);
        $this->matchHost($host);
        $this->matchMethod($methods);
        $this->matchIps($ips);
        $this->matchScheme($schemes);
        $this->matchPort($port);

        foreach ($attributes as $k => $v) {
            $this->matchAttribute($k$v);
        }
    }

    
if (!$pages) {
      return TRUE;
    }

    $request = $this->requestStack->getCurrentRequest();
    // Compare the lowercase path alias (if any) and internal path.     $path = $this->currentPath->getPath($request);
    // Do not trim a trailing slash if that is the complete path.     $path = $path === '/' ? $path : rtrim($path, '/');
    $path_alias = mb_strtolower($this->aliasManager->getAliasByPath($path));

    return $this->pathMatcher->matchPath($path_alias$pages) || (($path != $path_alias) && $this->pathMatcher->matchPath($path$pages));
  }

  /** * {@inheritdoc} */
  public function getCacheContexts() {
    $contexts = parent::getCacheContexts();
    $contexts[] = 'url.path';
    return $contexts;
  }

}
'^.*\.example\.COM$', true],
            ['.*\.sensio\.COM', false],
        ];
    }

    public function testPath()
    {
        $matcher = new RequestMatcher();

        $request = Request::create('/admin/foo');

        $matcher->matchPath('/admin/.*');
        $this->assertTrue($matcher->matches($request));

        $matcher->matchPath('/admin');
        $this->assertTrue($matcher->matches($request));

        $matcher->matchPath('^/admin/.*$');
        $this->assertTrue($matcher->matches($request));

        $matcher->matchMethod('/blog/.*');
        $this->assertFalse($matcher->matches($request));
    }

    
$route_match = $this->createMock('Drupal\Core\Routing\RouteMatchInterface');
    $this->pathMatcher = new PathMatcher($config_factory_stub$route_match);
  }

  /** * Tests that standard paths works with multiple patterns. * * @dataProvider getMatchPathData */
  public function testMatchPath($patterns$paths) {
    foreach ($paths as $path => $expected_result) {
      $actual_result = $this->pathMatcher->matchPath($path$patterns);
      $this->assertEquals($actual_result$expected_result, "Tried matching the path '$path' to the pattern '$patterns'.");
    }
  }

  /** * Provides test path data. * * @return array * A nested array of pattern arrays and path arrays. */
  public function getMatchPathData() {
    

    private array $schemes = [];

    /** * @param string|string[]|null $methods * @param string|string[]|null $ips * @param string|string[]|null $schemes */
    public function __construct(string $path = null, string $host = null, string|array $methods = null, string|array $ips = null, array $attributes = [], string|array $schemes = null, int $port = null)
    {
        $this->matchPath($path);
        $this->matchHost($host);
        $this->matchMethod($methods);
        $this->matchIps($ips);
        $this->matchScheme($schemes);
        $this->matchPort($port);

        foreach ($attributes as $k => $v) {
            $this->matchAttribute($k$v);
        }
    }

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