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

Conversation

ghostika
Copy link

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets [sensiolabs/SensioFrameworkExtraBundle#320]
License MIT

If the given route option parameter has a priority key, it will be sorted. The use case is described in the linked ticket.

If the changes are acceptable, then I will create the changes also for the Doc

@weaverryan
Copy link
Member

Btw, we added this only for annotations, because I think that's the only real use-case where this is important. I thought this would be initially done in SensioFrameworkExtraBundle since my use-case is to make life easier there, but the code ended up living in core :).

Thanks!

@Tobion
Copy link
Contributor

Tobion commented Aug 23, 2014

I don't think the implementation actually works. $collection->getIterator()->uasort this won't change the array order inside the $collection, just in the interator. Or am I wrong?

@Tobion
Copy link
Contributor

Tobion commented Aug 23, 2014

This would also require a lot of tests. For example that the routes without priority actually don't change order.

@ghostika
Copy link
Author

I implemented this first in one of my personal project and checked there the behaviour, and it change the order inside the collection. The problem here, that the code is in sf, but the annotations are in the frameworkextra-bundle. I'm opened to any suggestion, how to test. Or you mean just simply create routes and pass them to the load?

@@ -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

@ghostika
Copy link
Author

I will extract the sorting to a separate function so that it can be tested, cause at the moment I don't see any other option for that.

@@ -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.

@ghostika ghostika changed the title [Routing] Add support for 'priority' option for annotated Routes [WIP][Routing] Add support for 'priority' option for annotated Routes Aug 23, 2014
@@ -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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants