8000 Add priority to debug router by MrYamous · Pull Request #36110 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Add priority to debug router #36110

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ protected function getRouteData(Route $route): array
'hostRegex' => '' !== $route->getHost() ? $route->compile()->getHostRegex() : '',
'scheme' => $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY',
'method' => $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY',
'priority' => $route->getPriority() ? $route->getPriority() : '',
Copy link
Contributor

Choose a reason for hiding this comment

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

why sometimes you compare 0 !== $route->getPriority() and sometimes only $route->getPriority()?
maybe using the same pattern is better

'class' => \get_class($route),
'defaults' => $route->getDefaults(),
'requirements' => $route->getRequirements() ?: 'NO CUSTOM',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ protected function describeRoute(Route $route, array $options = [])
."\n".'- Host Regex: '.('' !== $route->getHost() ? $route->compile()->getHostRegex() : '')
."\n".'- Scheme: '.($route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY')
."\n".'- Method: '.($route->getMethods() ? implode('|', $route->getMethods()) : 'ANY')
."\n".'- Priority: '.($route->getPriority() ? $route->getPriority() : '')
."\n".'- Class: '.\get_class($route)
."\n".'- Defaults: '.$this->formatRouterConfig($route->getDefaults())
."\n".'- Requirements: '.($route->getRequirements() ? $this->formatRouterConfig($route->getRequirements()) : 'NO CUSTOM')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected function describeRouteCollection(RouteCollection $routes, array $optio
{
$showControllers = isset($options['show_controllers']) && $options['show_controllers'];

$tableHeaders = ['Name', 'Method', 'Scheme', 'Host', 'Path'];
$tableHeaders = ['Name', 'Method', 'Scheme', 'Host', 'Priority', 'Path'];
if ($showControllers) {
$tableHeaders[] = 'Controller';
}
Expand All @@ -64,6 +64,7 @@ protected function describeRouteCollection(RouteCollection $routes, array $optio
$route->getMethods() ? implode('|', $route->getMethods()) : 'ANY',
$route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY',
'' !== $route->getHost() ? $route->getHost() : 'ANY',
0 !== $route->getPriority() ? $route->getPriority() : '',
$this->formatControllerLink($controller, $route->getPath()),
];

Expand Down Expand Up @@ -97,6 +98,7 @@ protected function describeRoute(Route $route, array $options = [])
['Host Regex', ('' !== $route->getHost() ? $route->compile()->getHostRegex() : '')],
['Scheme', ($route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY')],
['Method', ($route->getMethods() ? implode('|', $route->getMethods()) : 'ANY')],
['Priority', (0 !== $route->getPriority() ? $route->getPriority() : '')],
['Requirements', ($route->getRequirements() ? $this->formatRouterConfig($route->getRequirements()) : 'NO CUSTOM')],
['Class', \get_class($route)],
['Defaults', $this->formatRouterConfig($route->getDefaults())],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ private function getRouteDocument(Route $route, string $name = null): \DOMDocume
$methodXML->appendChild(new \DOMText($method));
}

$routeXML->appendChild($priorityXML = $dom->createElement('priority'));
$priorityXML->appendChild(new \DOMText($route->getPriority()));

if ($route->getDefaults()) {
$routeXML->appendChild($defaultsXML = $dom->createElement('defaults'));
foreach ($route->getDefaults() as $attribute => $value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public static function getRoutes()
['opt1' => 'val1', 'opt2' => 'val2'],
'localhost',
['http', 'https'],
['get', 'head']
['get', 'head'],
'',
2
),
'route_2' => new RouteStub(
'/name/add',
Expand All @@ -54,7 +56,8 @@ public static function getRoutes()
'localhost',
['http', 'https'],
['put', 'post'],
"context.getMethod() in ['GET', 'HEAD', 'POST']"
"context.getMethod() in ['GET', 'HEAD', 'POST']",
1
),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"hostRegex": "#HOST_REGEX#",
"scheme": "http|https",
"method": "GET|HEAD",
"priority": 2,
"class": "Symfony\\Bundle\\FrameworkBundle\\Tests\\Console\\Descriptor\\RouteStub",
"defaults": {
"name": "Joseph"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
- Host Regex: #HOST_REGEX#
- Scheme: http|https
- Method: GET|HEAD
- Priority: 2
- Class: Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub
- Defaults:
- Defaults:
- `name`: Joseph
- Requirements:
- Requirements:
- `name`: [a-z]+
- Options:
- Options:
- `compiler_class`: Symfony\Component\Routing\RouteCompiler
- `opt1`: val1
- `opt2`: val2
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
| Host Regex | #HOST_REGEX# |
| Scheme | http|https |
| Method | GET|HEAD |
| Priority | 2 |
| Requirements | name: [a-z]+ |
| Class | Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub |
| Defaults | name: Joseph |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<scheme>https</scheme>
<method>GET</method>
<method>HEAD</method>
<priority>2</priority>
<defaults>
<default key="name">Joseph</default>
</defaults>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"hostRegex": "#HOST_REGEX#",
"scheme": "http|https",
"method": "PUT|POST",
"priority": 1,
"class": "Symfony\\Bundle\\FrameworkBundle\\Tests\\Console\\Descriptor\\RouteStub",
"defaults": [],
"requirements": "NO CUSTOM",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
- Host Regex: #HOST_REGEX#
- Scheme: http|https
- Method: PUT|POST
- Priority: 1
- Class: Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub
- Defaults: NONE
- Requirements: NO CUSTOM
- Options:
- Options:
- `compiler_class`: Symfony\Component\Routing\RouteCompiler
- `opt1`: val1
- `opt2`: val2
- Condition: context.getMethod() in ['GET', 'HEAD', 'POST']
- Condition: context.getMethod() in ['GET', 'HEAD', 'POST']
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
| Host Regex | #HOST_REGEX# |
| Scheme | http|https |
| Method | PUT|POST |
| Priority | 1 |
| Requirements | NO CUSTOM |
| Class | Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub |
| Defaults | NONE |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<scheme>https</scheme>
<method>PUT</method>
<method>POST</method>
<priority>1</priority>
<options>
<option key="compiler_class">Symfony\Component\Routing\RouteCompiler</option>
<option key="opt1">val1</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"hostRegex": "#HOST_REGEX#",
"scheme": "http|https",
"method": "GET|HEAD",
"priority": 2,
"class": "Symfony\\Bundle\\FrameworkBundle\\Tests\\Console\\Descriptor\\RouteStub",
"defaults": {
"name": "Joseph"
Expand All @@ -26,6 +27,7 @@
"hostRegex": "#HOST_REGEX#",
"scheme": "http|https",
"method": "PUT|POST",
"priority": 1,
"class": "Symfony\\Bundle\\FrameworkBundle\\Tests\\Console\\Descriptor\\RouteStub",
"defaults": [],
"requirements": "NO CUSTOM",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ route_1
- Host Regex: #HOST_REGEX#
- Scheme: http|https
- Method: GET|HEAD
- Priority: 2
- Class: Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub
- Defaults:
- Defaults:
- `name`: Joseph
- Requirements:
- Requirements:
- `name`: [a-z]+
- Options:
- Options:
- `compiler_class`: Symfony\Component\Routing\RouteCompiler
- `opt1`: val1
- `opt2`: val2
Expand All @@ -27,10 +28,11 @@ route_2
- Host Regex: #HOST_REGEX#
- Scheme: http|https
- Method: PUT|POST
- Priority: 1
- Class: Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub
- Defaults: NONE
- Requirements: NO CUSTOM
- Options:
- Options:
- `compiler_class`: Symfony\Component\Routing\RouteCompiler
- `opt1`: val1
- `opt2`: val2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--------- ---------- ------------ ----------- ---------------
 Name   Method   Scheme   Host   Path 
--------- ---------- ------------ ----------- ---------------
route_1 GET|HEAD http|https localhost /hello/{name}
route_2 PUT|POST http|https localhost /name/add
--------- ---------- ------------ ----------- ---------------
--------- ---------- ------------ ----------- ---------- ---------------
 Name   Method   Scheme   Host   Priority   Path 
--------- ---------- ------------ ----------- ---------- ---------------
route_1 GET|HEAD http|https localhost 2 /hello/{name}
route_2 PUT|POST http|https localhost 1 /name/add
--------- ---------- ------------ ----------- ---------- ---------------

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<scheme>https</scheme>
<method>GET</method>
<method>HEAD</method>
<priority>2</priority>
<defaults>
<default key="name">Joseph</default>
</defaults>
Expand All @@ -26,6 +27,7 @@
<scheme>https</scheme>
<method>PUT</method>
<method>POST</method>
<priority>1</priority>
<options>
<option key="compiler_class">Symfony\Component\Routing\RouteCompiler</option>
<option key="opt1">val1</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ protected function addRoute(RouteCollection $collection, $annot, array $globals,
}

foreach ($paths as $locale => $path) {
$route = $this->createRoute($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
$route = $this->createRoute($path, $defaults, $requirements, $options, $host, $schemes, $methods,
$condition, $priority);
$this->configureRoute($route, $class, $method, $annot);
if (0 !== $locale) {
$route->setDefault('_locale', $locale);
Expand Down Expand Up @@ -325,9 +326,10 @@ private function resetGlobals(): array
];
}

protected function createRoute(string $path, array $defaults, array $requirements, array $options, ?string $host, array $schemes, array $methods, ?string $condition)
protected function createRoute(string $path, array $defaults, array $requirements, array $options, ?string $host,
array $schemes, array $methods, ?string $condition, ?int $priority)
{
return new Route($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
return new Route($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition, $priority);
}

abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot);
Expand Down
32 changes: 31 additions & 1 deletion src/Symfony/Component/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Route implements \Serializable
private $requirements = [];
private $options = [];
private $condition = '';
private $priority = 0;

/**
* @var CompiledRoute|null
Expand All @@ -49,8 +50,11 @@ class Route implements \Serializable
* @param string|string[] $schemes A required URI scheme or an array of restricted schemes
* @param string|string[] $methods A required HTTP method or an array of restricted methods
* @param string|null $condition A condition that should evaluate to true for the route to match
* @param int|null $priority
*/
public function __construct(string $path, array $defaults = [], array $requirements = [], array $options = [], ?string $host = '', $schemes = [], $methods = [], ?string $condition = '')
public function __construct(string $path, array $defaults = [], array $requirements = [], array $options = [],
?string $host = '', $schemes = [], $methods = [], ?string $condition = '', ?int
$priority = 0)
{
$this->setPath($path);
$this->addDefaults($defaults);
Expand All @@ -60,6 +64,7 @@ public function __construct(string $path, array $defaults = [], array $requireme
$this->setSchemes($schemes);
$this->setMethods($methods);
$this->setCondition($condition);
$this->setPriority($priority);
}

public function __serialize(): array
Expand All @@ -74,6 +79,7 @@ public function __serialize(): array
'methods' => $this->methods,
'condition' => $this->condition,
'compiled' => $this->compiled,
'priority' => $this->priority,
];
}

Expand Down Expand Up @@ -101,6 +107,7 @@ public function __unserialize(array $data): void
if (isset($data['compiled'])) {
$this->compiled = $data['compiled'];
}
$this->priority = $data['priority'];
}

/**
Expand Down Expand Up @@ -507,6 +514,29 @@ public function setCondition(?string $condition)
return $this;
}

/**
* Returns the priority.
*
* @return integer The priority
*/
public function getPriority(): ?int
Copy link
Member
@nicolas-grekas nicolas-grekas Mar 18, 2020

Choose a reason for hiding this comment

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

As you can see in the implementation, routes don't have priorities on their own right now.
The command is not enough a reason to introduce this concept on routes IMHO.
I looked for alternatives and I propose to add a RouteCollection::getPriorities() method, which would just return $this->priorities. That should allow building the command without introducing any new concepts (since "priority" is already a concept of RouteCollection)

{
return $this->priority;
}

/**
* Sets the priority.
*
* @return $this
*/
public function setPriority(?int $priority)
{
$this->priority = (int) $priority;
$this->compiled = null;

return $this;
}

/**
* Compiles the route.
*
Expand Down
0