From 94b8e064590c461c23dd40511f874b46d0b9cf9f Mon Sep 17 00:00:00 2001 From: Nattfarinn Date: Wed, 14 Dec 2016 08:34:54 +0100 Subject: [PATCH] EZS-1210: Compound site-access reverse matcher --- Resources/config/services.yml | 4 ++-- SiteAccess/ReverseMatcher.php | 50 ++++++++++++++++++++++++++----------------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 5f521c1..d2e7a86 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -54,7 +54,7 @@ services: ezstudioui.reverse_matcher: class: '%ezstudioui.reverse_matcher.class%' arguments: + - "@request_stack" - "@ezpublish.siteaccess.matcher_builder" - - "%ezpublish.siteaccess.match_config%" - "%ezpublish.siteaccess.list%" - - "@request_stack" + - "%ezpublish.siteaccess.match_config%" diff --git a/SiteAccess/ReverseMatcher.php b/SiteAccess/ReverseMatcher.php index 0e82360..9a07899 100644 --- a/SiteAccess/ReverseMatcher.php +++ b/SiteAccess/ReverseMatcher.php @@ -2,66 +2,76 @@ namespace EzSystems\StudioUIBundle\SiteAccess; +use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException; use eZ\Publish\Core\MVC\Symfony\Routing\SimplifiedRequest; -use eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\Map\Host; -use eZ\Publish\Core\MVC\Symfony\SiteAccess\MatcherBuilder; -use eZ\Publish\Core\MVC\Symfony\SiteAccess\VersatileMatcher; use eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\CompoundInterface; -use eZ\Publish\Core\MVC\Exception\InvalidSiteAccessException; +use eZ\Publish\Core\MVC\Symfony\SiteAccess\MatcherBuilderInterface; +use eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher; +use eZ\Publish\Core\MVC\Symfony\SiteAccess\VersatileMatcher; use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; use Symfony\Component\HttpFoundation\RequestStack; class ReverseMatcher { - protected $matcherBuilder; + protected $siteAccessList; protected $siteAccessesConfiguration; - protected $siteAccessList; - protected $requestStack; + protected $siteAccessRouter; + public function __construct( - MatcherBuilder $matcherBuilder, - array $siteAccessesConfiguration, + RequestStack $requestStack, + MatcherBuilderInterface $matcherBuilder, array $siteAccessList, - RequestStack $requestStack) - { + array $siteAccessesConfiguration + ) { + $this->requestStack = $requestStack; $this->matcherBuilder = $matcherBuilder; - $this->siteAccessesConfiguration = $siteAccessesConfiguration; $this->siteAccessList = $siteAccessList; - $this->requestStack = $requestStack; + $this->siteAccessesConfiguration = $siteAccessesConfiguration; } /** - * Returns the VersatoleMatcher object containing request corresponding to the reverse match. + * Returns the VersatileMatcher object containing request corresponding to the reverse match. * This request object can then be used to build a link to the "reverse matched" SiteAccess. * * @param string $siteAccessName * - * @see reverseMatch() - * * @return VersatileMatcher + * + * @throws InvalidArgumentException + * + * @see reverseMatch() */ public function getMatcher($siteAccessName) { if (!in_array($siteAccessName, $this->siteAccessList)) { - throw new InvalidSiteAccessException($siteAccessName, $this->siteAccessList, get_class($this)); + throw new InvalidArgumentException('SiteAccess', $siteAccessName); } foreach ($this->siteAccessesConfiguration as $matchingClass => $matchingConfiguration) { $matcher = $this->matcherBuilder->buildMatcher($matchingClass, $matchingConfiguration, new SimplifiedRequest()); + if (!$matcher instanceof VersatileMatcher) { + continue; + } if ($matcher instanceof CompoundInterface) { $matcher->setMatcherBuilder($this->matcherBuilder); } - if ($matcher instanceof VersatileMatcher && $reverseMatch = $matcher->reverseMatch($siteAccessName)) { - return $reverseMatch; + $reverseMatcher = $matcher->reverseMatch($siteAccessName); + if (!$reverseMatcher instanceof Matcher) { + continue; } + + return $reverseMatcher; } - throw new InvalidConfigurationException('Cannot reverse match SiteAccess; at least one VersatileMatcher configuration is required.'); + throw new InvalidConfigurationException( + sprintf('SiteAccess "%s" could not be reverse-matched against configuration. No VersatileMatcher found.', $siteAccessName) + ); } public function getUrlParts($siteAccessName)