Commit bf586d4f authored by Crell's avatar Crell Committed by effulgentsia

Add priority support to partial matchers in a nested matcher.

parent 854a48bf
......@@ -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 @@ public function setInitialMatcher(InitialMatcherInterface $initial) {
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 @@ public function matchRequest(Request $request) {
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.
*
......
......@@ -35,11 +35,14 @@ public function setInitialMatcher(InitialMatcherInterface $initial);
*
* @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.
......
......@@ -53,7 +53,7 @@ public function testNestedMatcher() {
$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');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment