eZ\Publish\Core\MVC\Symfony\SiteAccess\Router::matchByName PHP Method

matchByName() public method

Returns corresponding SiteAccess object, according to configuration, with corresponding matcher. Returns null if no matcher can be found (e.g. non versatile).
public matchByName ( string $siteAccessName ) : SiteAccess | null
$siteAccessName string
return eZ\Publish\Core\MVC\Symfony\SiteAccess | null
    public function matchByName($siteAccessName)
    {
        if (!isset($this->siteAccessList[$siteAccessName])) {
            throw new InvalidArgumentException("Invalid SiteAccess name provided for reverse matching: {$siteAccessName}");
        }
        $request = clone $this->request;
        // Be sure to have a clean pathinfo, without SiteAccess part in it.
        if ($this->siteAccess && $this->siteAccess->matcher instanceof URILexer) {
            $request->setPathinfo($this->siteAccess->matcher->analyseURI($request->pathinfo));
        }
        foreach ($this->siteAccessesConfiguration as $matchingClass => $matchingConfiguration) {
            $matcher = $this->matcherBuilder->buildMatcher($matchingClass, $matchingConfiguration, $request);
            if (!$matcher instanceof VersatileMatcher) {
                continue;
            }
            if ($matcher instanceof CompoundInterface) {
                $matcher->setMatcherBuilder($this->matcherBuilder);
            }
            $reverseMatcher = $matcher->reverseMatch($siteAccessName);
            if (!$reverseMatcher instanceof Matcher) {
                continue;
            }
            $siteAccessClass = $this->siteAccessClass;
            /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess $siteAccess */
            $siteAccess = new $siteAccessClass();
            $siteAccess->name = $siteAccessName;
            $siteAccess->matcher = $reverseMatcher;
            $siteAccess->matchingType = $reverseMatcher->getName();
            return $siteAccess;
        }
        // No VersatileMatcher configured for $siteAccessName.
        $this->logger->notice("Siteaccess '{$siteAccessName}' could not be reverse-matched against configuration. No VersatileMatcher found.");
        return null;
    }

Usage Example

Example #1
0
 public function testMatchByNameNoVersatileMatcher()
 {
     $matcherBuilder = $this->getMock('eZ\\Publish\\Core\\MVC\\Symfony\\SiteAccess\\MatcherBuilderInterface');
     $logger = $this->getMock('Psr\\Log\\LoggerInterface');
     $matcherClass = 'Map\\Host';
     $matchedSiteAccess = 'foo';
     $matcherConfig = array('phoenix-rises.fm' => $matchedSiteAccess, 'ez.no' => 'default_sa');
     $config = array($matcherClass => $matcherConfig);
     $router = new Router($matcherBuilder, $logger, 'default_sa', $config, array($matchedSiteAccess, 'default_sa'));
     $router->setSiteAccess(new SiteAccess('test', 'test'));
     $request = $router->getRequest();
     $matcherBuilder->expects($this->once())->method('buildMatcher')->with($matcherClass, $matcherConfig, $request)->will($this->returnValue($this->getMock('eZ\\Publish\\Core\\MVC\\Symfony\\SiteAccess\\Matcher')));
     $logger->expects($this->once())->method('notice');
     $this->assertNull($router->matchByName($matchedSiteAccess));
 }