Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf60704c

Browse files
committed
Enrich Router's MissingMandatoryParametersException
1 parentae3b078 commitf60704c

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-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+
* Add`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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,41 @@
1919
*/
2020
class MissingMandatoryParametersExceptionextends \InvalidArgumentExceptionimplements ExceptionInterface
2121
{
22+
privatestring$routeName ='';
23+
privatearray$missingParameters = [];
24+
25+
/**
26+
* @param string $routeName
27+
* @param string[] $missingParameters
28+
* @param int $code
29+
*/
30+
publicfunction__construct(string$routeName ='',$missingParameters =null,$code =0,\Throwable$previous =null)
31+
{
32+
if (\is_array($missingParameters)) {
33+
$this->missingParameters =$missingParameters;
34+
$this->routeName =$routeName;
35+
$message =sprintf('Some mandatory parameters are missing ("%s") to generate a URL for route "%s".',implode('", "',$missingParameters),$routeName
36+
);
37+
}else {
38+
trigger_deprecation('symfony/routing','6.1','Construction of "%s" with an exception message is deprecated, provide the route name and an array of missing parameters instead.',__CLASS__);
39+
$message =$routeName;
40+
$previous =$codeinstanceof \Throwable ?$code :null;
41+
$code = (int)$missingParameters;
42+
}
43+
44+
parent::__construct($message,$code,$previous);
45+
}
46+
47+
/**
48+
* @return string[]
49+
*/
50+
publicfunctiongetMissingParameters():array
51+
{
52+
return$this->missingParameters;
53+
}
54+
55+
publicfunctiongetRouteName():string
56+
{
57+
return$this->routeName;
58+
}
2259
}

‎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-
thrownewMissingMandatoryParametersException(sprintf('Some mandatory parameters are missing ("%s") to generate a URL for route "%s".',implode('", "',array_keys($diff)),$name));
174+
thrownewMissingMandatoryParametersException($name,array_keys($diff));
175175
}
176176

177177
$url ='';

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,33 @@ public function testGenerateWithInvalidLocale()
321321
$generator->generate($name);
322322
}
323323

324+
/**
325+
* @group legacy
326+
*/
327+
publicfunctiontestLegacyThrowingMissingMandatoryParameters()
328+
{
329+
$this->expectDeprecation('Since symfony/routing 6.1: Construction of "Symfony\Component\Routing\Exception\MissingMandatoryParametersException" with an exception message is deprecated, provide the route name and an array of missing parameters instead.');
330+
331+
$exception =newMissingMandatoryParametersException('expected legacy message');
332+
$this->assertSame('expected legacy message',$exception->getMessage());
333+
}
334+
335+
/**
336+
* @group legacy
337+
*/
338+
publicfunctiontestLegacyThrowingMissingMandatoryParametersWithAllParameters()
339+
{
340+
$this->expectDeprecation('Since symfony/routing 6.1: Construction of "Symfony\Component\Routing\Exception\MissingMandatoryParametersException" with an exception message is deprecated, provide the route name and an array of missing parameters instead.');
341+
342+
$exception =newMissingMandatoryParametersException('expected legacy message',256,new \Exception());
343+
$this->assertSame('expected legacy message',$exception->getMessage());
344+
$this->assertInstanceOf(\Exception::class,$exception->getPrevious());
345+
}
346+
324347
publicfunctiontestGenerateForRouteWithoutMandatoryParameter()
325348
{
326349
$this->expectException(MissingMandatoryParametersException::class);
350+
$this->expectExceptionMessage('Some mandatory parameters are missing ("foo") to generate a URL for route "test".');
327351
$routes =$this->getRoutes('test',newRoute('/testing/{foo}'));
328352
$this->getGenerator($routes)->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL);
329353
}
@@ -554,6 +578,7 @@ public function testImportantVariable()
554578
publicfunctiontestImportantVariableWithNoDefault()
555579
{
556580
$this->expectException(MissingMandatoryParametersException::class);
581+
$this->expectExceptionMessage('Some mandatory parameters are missing ("_format") to generate a URL for route "test".');
557582
$routes =$this->getRoutes('test',newRoute('/{page}.{!_format}'));
558583
$generator =$this->getGenerator($routes);
559584

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp