Skip to content

[WIP][Routing] Add support for 'priority' option for annotated Routes #11747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ abstract class AnnotationClassLoader implements LoaderInterface
*/
protected $defaultRouteIndex = 0;

/**
* @var bool
*/
protected $routeWithPriority = false;

/**
* Constructor.
*
Expand All @@ -92,6 +97,16 @@ public function setRouteAnnotationClass($class)
$this->routeAnnotationClass = $class;
}

/**
* Tells if one of the annotationed route has priority info
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

routes

*
* @return bool
*/
public function hasRouteWithPriority()
{
return $this->routeWithPriority;
}

/**
* Loads from annotations from a class.
*
Expand All @@ -104,6 +119,8 @@ public function setRouteAnnotationClass($class)
*/
public function load($class, $type = null)
{
$hasPriority = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused variable


if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
}
Expand All @@ -123,6 +140,9 @@ public function load($class, $type = null)
foreach ($this->reader->getMethodAnnotations($method) as $annot) {
if ($annot instanceof $this->routeAnnotationClass) {
$this->addRoute($collection, $annot, $globals, $class, $method);
if (array_key_exists('priority',$annot->getOptions())) {
$this->routeWithPriority = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loop can be left then since all following routes wouldn't change the routeWithPriority property any more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean check if routeWithPriority still false?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind, I didn't see the call to addRoute() first.

}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Routing/Loader/AnnotationDirectoryLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Routing\Loader;

use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Config\Resource\DirectoryResource;

Expand Down Expand Up @@ -58,6 +59,15 @@ public function load($path, $type = null)
}
}

if ($this->loader->hasRouteWithPriority()) {
$collection->getIterator()->uasort(function(Route $a, Route $b) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a space after function

if ($a->getOption('priority') == $b->getOption('priority')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use === instead of ==

return 0;
}
return $a->getOption('priority') > $b->getOption('priority') ? 1 : -1;
});
}

return $collection;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public function getLoadTests()
array('name' => 'route1', 'defaults' => array('arg2' => 'foo'), 'condition' => 'context.getMethod() == "GET"'),
array('arg2' => 'defaultValue2', 'arg3' =>'defaultValue3')
),
array(
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
array('name' => 'route_priority', 'options' => array('priority' => '10')),
array('arg2' => 'defaultValue2', 'arg3' => 'defaultValue3')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a comma at the end of the line

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I add also to all of the others? Cause I just copy paste one of the previous and there were no coma

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say no as it leads to mainly unrelated file diffs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then only to this line or leave this like this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only this line, there's no harm in adding it here. It's part of the diff anyway.

),
);
}

Expand Down Expand Up @@ -120,9 +125,10 @@ public function testLoad($className, $routeDatas = array(), $methodArgs = array(

$this->assertSame($routeDatas['path'], $route->getPath(), '->load preserves path annotation');
$this->assertSame($routeDatas['requirements'],$route->getRequirements(), '->load preserves requirements annotation');
$this->assertCount(0, array_intersect($route->getOptions(), $routeDatas['options']), '->load preserves options annotation');
$this->assertSame(count($routeDatas['options']), count(array_intersect($route->getOptions(), $routeDatas['options'])), '->load preserves options annotation');
$this->assertSame(array_replace($methodArgs, $routeDatas['defaults']), $route->getDefaults(), '->load preserves defaults annotation');
$this->assertEquals($routeDatas['condition'], $route->getCondition(), '->load preserves condition annotation');
$this->assertSame(isset($routeDatas['options']['priority']), $this->loader->hasRouteWithPriority());
}

public function testClassRouteLoad()
Expand Down