Add priority support to partial matchers in a nested matcher.

8.0.x
Larry Garfield 2012-09-28 12:21:03 -05:00 committed by effulgentsia
parent 854a48bf6b
commit bf586d4f33
3 changed files with 56 additions and 7 deletions

View File

@ -29,6 +29,13 @@ class NestedMatcher implements NestedMatcherInterface {
*/
protected $partialMatchers = array();
/**
* Array of PartialMatcherInterface objects, sorted.
*
* @var type
*/
protected $sortedMatchers = array();
/**
* The initial matcher to match against.
*
@ -50,14 +57,20 @@ class NestedMatcher implements NestedMatcherInterface {
*
* @param \Drupal\Core\Routing\PartialMatcherInterface $matcher
* A partial matcher.
* @param int $priority
* (optional) The priority of the matcher. Higher number matchers will be checked
* first. Default to 0.
*
* @return NestedMatcherInterface
* The current matcher.
*/
public function addPartialMatcher(PartialMatcherInterface $matcher) {
$this->partialMatchers[] = $matcher;
public function addPartialMatcher(PartialMatcherInterface $matcher, $priority = 0) {
if (empty($this->matchers[$priority])) {
$this->matchers[$priority] = array();
}
return $this;
$this->matchers[$priority][] = $matcher;
$this->sortedMatchers = array();
}
/**
@ -114,7 +127,7 @@ class NestedMatcher implements NestedMatcherInterface {
public function matchRequest(Request $request) {
$collection = $this->initialMatcher->matchRequestPartial($request);
foreach ($this->partialMatchers as $matcher) {
foreach ($this->getPartialMatchers() as $matcher) {
if ($collection) {
$matcher->setCollection($collection);
}
@ -126,6 +139,39 @@ class NestedMatcher implements NestedMatcherInterface {
return $attributes;
}
/**
* Sorts the matchers and flattens them.
*
* @return array
* An array of RequestMatcherInterface objects.
*/
public function getPartialMatchers() {
if (empty($this->sortedMatchers)) {
$this->sortedMatchers = $this->sortMatchers();
}
return $this->sortedMatchers;
}
/**
* Sort matchers by priority.
*
* The highest priority number is the highest priority (reverse sorting).
*
* @return \Symfony\Component\Routing\RequestMatcherInterface[]
* An array of Matcher objects in the order they should be used.
*/
protected function sortMatchers() {
$sortedMatchers = array();
krsort($this->matchers);
foreach ($this->matchers as $matchers) {
$sortedMatchers = array_merge($sortedMatchers, $matchers);
}
return $sortedMatchers;
}
/**
* Sets the request context.
*

View File

@ -35,11 +35,14 @@ interface NestedMatcherInterface extends RequestMatcherInterface {
*
* @param \Drupal\Core\Routing\PartialMatcherInterface $matcher
* A partial matcher.
* @param int $priority
* (optional) The priority of the matcher. Higher number matchers will be checked
* first. Default to 0.
*
* @return \Drupal\Core\Routing\NestedMatcherInterface
* @return NestedMatcherInterface
* The current matcher.
*/
public function addPartialMatcher(PartialMatcherInterface $matcher);
public function addPartialMatcher(PartialMatcherInterface $matcher, $priority = 0);
/**
* Sets the final matcher for the matching plan.

View File

@ -53,7 +53,7 @@ class NestedMatcherTest extends UnitTestBase {
$matcher = new NestedMatcher();
$matcher->setInitialMatcher(new MockPathMatcher($this->fixtures->sampleRouteCollection()));
$matcher->addPartialMatcher(new HttpMethodMatcher());
$matcher->addPartialMatcher(new HttpMethodMatcher(), 1);
$matcher->setFinalMatcher(new FirstEntryFinalMatcher());
$request = Request::create('/path/one', 'GET');