Skip to content

Commit 22092a1

Browse files
committed
Enrich Router's MissingMandatoryParametersException
1 parent ae3b078 commit 22092a1

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

src/Symfony/Component/Routing/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
6.1
55
---
66

7+
* Added `getMissingParameters` and `getRouteName` methods on `MissingMandatoryParametersException`
78
* Allow using UTF-8 parameter names
89

910
5.3

src/Symfony/Component/Routing/Exception/MissingMandatoryParametersException.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,36 @@
1919
*/
2020
class MissingMandatoryParametersException extends \InvalidArgumentException implements ExceptionInterface
2121
{
22+
private array $missingParameters = [];
23+
private string $routeName = '';
24+
25+
public function __construct(/*array*/ $missingParameters, /*string*/ $routeName, /*int*/ $code = 0, \Throwable $previous = null)
26+
{
27+
if(\is_array($this->missingParameters)) {
28+
$this->missingParameters = $missingParameters;
29+
$this->routeName = $routeName;
30+
$message = sprintf(
31+
'Some mandatory parameters are missing ("%s") to generate a URL for route "%s".',
32+
implode('", "', $missingParameters),
33+
$routeName
34+
);
35+
} else {
36+
trigger_deprecation('symfony/routing', '6.1', 'Construction of "%s" with an exception message is deprecated, provide an array of missing parameters and the route name instead.', __CLASS__);
37+
$message = (string) $missingParameters;
38+
$code = (int) $routeName;
39+
$previous = $code instanceof \Throwable ? $code : null;
40+
}
41+
42+
parent::__construct($message, $code, $previous);
43+
}
44+
45+
public function getMissingParameters(): array
46+
{
47+
return $this->missingParameters;
48+
}
49+
50+
public function getRouteName(): string
51+
{
52+
return $this->routeName;
53+
}
2254
}

src/Symfony/Component/Routing/Generator/UrlGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ protected function doGenerate(array $variables, array $defaults, array $requirem
171171

172172
// all params must be given
173173
if ($diff = array_diff_key($variables, $mergedParams)) {
174-
throw new MissingMandatoryParametersException(sprintf('Some mandatory parameters are missing ("%s") to generate a URL for route "%s".', implode('", "', array_keys($diff)), $name));
174+
throw new MissingMandatoryParametersException(array_keys($diff), $name);
175175
}
176176

177177
$url = '';

src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ public function testGenerateWithInvalidLocale()
324324
public function testGenerateForRouteWithoutMandatoryParameter()
325325
{
326326
$this->expectException(MissingMandatoryParametersException::class);
327+
$this->expectExceptionMessage('Some mandatory parameters are missing ("foo") to generate a URL for route "test".');
327328
$routes = $this->getRoutes('test', new Route('/testing/{foo}'));
328329
$this->getGenerator($routes)->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL);
329330
}
@@ -554,6 +555,7 @@ public function testImportantVariable()
554555
public function testImportantVariableWithNoDefault()
555556
{
556557
$this->expectException(MissingMandatoryParametersException::class);
558+
$this->expectExceptionMessage('Some mandatory parameters are missing ("_format") to generate a URL for route "test".');
557559
$routes = $this->getRoutes('test', new Route('/{page}.{!_format}'));
558560
$generator = $this->getGenerator($routes);
559561

0 commit comments

Comments
 (0)