Skip to content

[FrameworkBundle] [Routing] Added --sort to debug:router #36579

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 6 commits into from

Conversation

vjandrea
Copy link

@vjandrea vjandrea commented Apr 25, 2020

Q A
Branch? master
Bug fix? no
New feature? yes
Deprecations? no
Tickets
License MIT
Doc PR symfony/symfony-docs#13591

This PR adds a --sort functionality to console debug:router. It allows to sort by name, path or priority (default).

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

LGTM, just some nitpicking.

@Tobion
Copy link
Contributor

Tobion commented Apr 29, 2020

I think this PR gives the wrong impression. First of all routes are not only sorted by priority but also by the order your define them in by default. So the description is not correct.
And sorting by name and path can be done using something like sort -k 5 which is what I used so far.
I think it's probably still ok to add those options for convenience but it needs a big fat warning that when sorting by name or path, the shown routes do not reflect the matching order anymore.

@vjandrea
Copy link
Author

vjandrea commented May 3, 2020

@Tobion Yes, probably "priority" is not the best name because by default debug:router outputs the list in the evaluation order. There's also the new priority annotation to consider: keeping priority, it might sound ambiguous if the priority annotation value will be shown in the routes table.
I agree to show a red warning when sorting by name or path. I'll fix the conflicts for now and I'll see in a couple of days if there are other opinions about the points you suggested.

@vjandrea vjandrea requested review from sroze and xabbuh as code owners May 3, 2020 15:05
@vjandrea vjandrea force-pushed the debug-router-sort branch from 460477f to 2b9e044 Compare May 3, 2020 18:01
@fabpot
Copy link
Member

fabpot commented Jun 3, 2020

Something went wrong here.

@vjandrea
Copy link
Author

vjandrea commented Jun 3, 2020

Sorry @fabpot, I was trying to fix the failing tests without noticing that the main build was failing too. I will try to cleanup the git mess otherwise I'll drop this PR and submit a new one.

@vjandrea vjandrea force-pushed the debug-router-sort branch 2 times, most recently from 6c67ce1 to ced94da Compare June 7, 2020 06:37
@vjandrea vjandrea force-pushed the debug-router-sort branch from 7af952f to 49029ae Compare July 11, 2020 09:13
@vjandrea
Copy link
Author

@Tobion, I added a warning when the sorting result might be misleading: 49029ae

@vjandrea
Copy link
Author

@sroze, @xabbuh, do you mind reviewing this PR please? thanks!

if ('name' === $propertyName) {
ksort($sortedRoutes);
} elseif ('path' === $propertyName) {
uasort($sortedRoutes, function ($a, $b) { return $a->getPath() <=> $b->getPath(); });
Copy link
Contributor

Choose a reason for hiding this comment

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

please add the type for $a and $b

Copy link
Contributor

Choose a reason for hiding this comment

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

if routes have the same path but different prio, this can result is routes getting mixed up. sorting will only be stable in php 8: https://wiki.php.net/rfc/stable_sorting
So we would need to implement the stable part ourselves prio to php 8.

Copy link
Author

Choose a reason for hiding this comment

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

Ok, I'll refactor the test to verify the stability

private function sortRoutes(RouteCollection $routes, string $propertyName): RouteCollection
{
$sortedRoutes = $routes->all();
if ('name' === $propertyName) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think a switch statement would be a better fit here

Copy link
Author

Choose a reason for hiding this comment

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

Ok I'm going to change this

$tester->execute(['--sort' => null], ['decorated' => false]);
}

public function testSortingByPriorityWithDuplicatePath()
Copy link
Contributor

Choose a reason for hiding this comment

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

i think this test is useless as it doesn't cover anything new

$this->assertStringNotContainsString('! [CAUTION] The routes list is not sorted in the parsing order.', $tester->getDisplay(true));
}

public function testSortingByNameWithDuplicatePath()
Copy link
Contributor

Choose a reason for hiding this comment

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

i think this test is useless as it doesn't cover anything new

$this->assertStringContainsString('! [CAUTION] The routes list is not sorted in the parsing order.', $tester->getDisplay(true));
}

public function testSortingByPathWithDuplicatePath()
Copy link
Contributor

Choose a reason for hiding this comment

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

this test should actually also make sure the routes with the same path stay in the correct order as before (check routes names) as I mention in the stable sorting comment

@fabpot
Copy link
Member

fabpot commented Aug 17, 2020

@vjandrea Can you take the latest comments into account?

vjandrea and others added 2 commits August 20, 2020 09:25
c/o @Tobion

Co-authored-by: Tobias Schultze <webmaster@tubo-world.de>
c/o @Tobion

Co-authored-by: Tobias Schultze <webmaster@tubo-world.de>
@fabpot
Copy link
Member

fabpot commented Sep 5, 2020

@vjandrea It looks like you did some work, but I still see some feedback that are not taken into account. Do you plan to finish this PR soon?

@fabpot
Copy link
Member

fabpot commented Sep 19, 2020

@vjandrea Still willing to finish this PR?

@vjandrea
Copy link
Author

vjandrea commented Sep 19, 2020 via email

@fabpot
Copy link
Member

fabpot commented Sep 24, 2020

Re-reading the whole thing, I think that having a --sort flag is wrong for the reasons explained by @Tobion. As the output of a command can be piped through any other command, I think we don't need to support this in core. Let's close.

@fabpot fabpot closed this Sep 24, 2020
@nicolas-grekas nicolas-grekas modified the milestones: next, 5.2 Oct 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants