Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d1b16c6

Browse files
Merge branch '6.3' into 6.4
* 6.3: [Routing] Fixed priority getting lost when defining prefix array [Messenger] PhpSerializer: TypeError should throw MessageDecodingFailedException
2 parents cb0817c + dd49cd8 commit d1b16c6

File tree

7 files changed

+81
-15
lines changed

7 files changed

+81
-15
lines changed

src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,50 +33,63 @@ public function testEncodedIsDecodable()
3333

3434
public function testDecodingFailsWithMissingBodyKey()
3535
{
36+
$serializer = $this->createPhpSerializer();
37+
3638
$this->expectException(MessageDecodingFailedException::class);
3739
$this->expectExceptionMessage('Encoded envelope should have at least a "body", or maybe you should implement your own serializer');
3840

39-
$serializer = $this->createPhpSerializer();
40-
4141
$serializer->decode([]);
4242
}
4343

4444
public function testDecodingFailsWithBadFormat()
4545
{
46+
$serializer = $this->createPhpSerializer();
47+
4648
$this->expectException(MessageDecodingFailedException::class);
4749
$this->expectExceptionMessageMatches('/Could not decode/');
4850

49-
$serializer = $this->createPhpSerializer();
50-
5151
$serializer->decode([
5252
'body' => '{"message": "bar"}',
5353
]);
5454
}
5555

5656
public function testDecodingFailsWithBadBase64Body()
5757
{
58+
$serializer = $this->createPhpSerializer();
59+
5860
$this->expectException(MessageDecodingFailedException::class);
5961
$this->expectExceptionMessageMatches('/Could not decode/');
6062

61-
$serializer = $this->createPhpSerializer();
62-
6363
$serializer->decode([
6464
'body' => 'x',
6565
]);
6666
}
6767

6868
public function testDecodingFailsWithBadClass()
6969
{
70+
$serializer = $this->createPhpSerializer();
71+
7072
$this->expectException(MessageDecodingFailedException::class);
7173
$this->expectExceptionMessageMatches('/class "ReceivedSt0mp" not found/');
7274

73-
$serializer = $this->createPhpSerializer();
74-
7575
$serializer->decode([
7676
'body' => 'O:13:"ReceivedSt0mp":0:{}',
7777
]);
7878
}
7979

80+
public function testDecodingFailsForPropertyTypeMismatch()
81+
{
82+
$serializer = $this->createPhpSerializer();
83+
$encodedEnvelope = $serializer->encode(new Envelope(new DummyMessage('true')));
84+
// Simulate a change of property type in the code base
85+
$encodedEnvelope['body'] = str_replace('s:4:\"true\"', 'b:1', $encodedEnvelope['body']);
86+
87+
$this->expectException(MessageDecodingFailedException::class);
88+
$this->expectExceptionMessageMatches('/Could not decode/');
89+
90+
$serializer->decode($encodedEnvelope);
91+
}
92+
8093
public function testEncodedSkipsNonEncodeableStamps()
8194
{
8295
$serializer = $this->createPhpSerializer();

src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,14 @@ private function safelyUnserialize(string $contents): Envelope
7575
throw new MessageDecodingFailedException('Could not decode an empty message using PHP serialization.');
7676
}
7777

78-
$signalingException = new MessageDecodingFailedException(sprintf('Could not decode message using PHP serialization: %s.', $contents));
79-
8078
if ($this->acceptPhpIncompleteClass) {
8179
$prevUnserializeHandler = ini_set('unserialize_callback_func', null);
8280
} else {
8381
$prevUnserializeHandler = ini_set('unserialize_callback_func', self::class.'::handleUnserializeCallback');
8482
}
85-
$prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = []) use (&$prevErrorHandler, $signalingException) {
83+
$prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = []) use (&$prevErrorHandler) {
8684
if (__FILE__ === $file) {
87-
throw $signalingException;
85+
throw new \ErrorException($msg, 0, $type, $file, $line);
8886
}
8987

9088
return $prevErrorHandler ? $prevErrorHandler($type, $msg, $file, $line, $context) : false;
@@ -93,13 +91,19 @@ private function safelyUnserialize(string $contents): Envelope
9391
try {
9492
/** @var Envelope */
9593
$envelope = unserialize($contents);
94+
} catch (\Throwable $e) {
95+
if ($e instanceof MessageDecodingFailedException) {
96+
throw $e;
97+
}
98+
99+
throw new MessageDecodingFailedException('Could not decode Envelope: '.$e->getMessage(), 0, $e);
96100
} finally {
97101
restore_error_handler();
98102
ini_set('unserialize_callback_func', $prevUnserializeHandler);
99103
}
100104

101105
if (!$envelope instanceof Envelope) {
102-
throw $signalingException;
106+
throw new MessageDecodingFailedException('Could not decode message into an Envelope.');
103107
}
104108

105109
if ($envelope->getMessage() instanceof \__PHP_Incomplete_Class) {

src/Symfony/Component/Routing/Loader/Configurator/Traits/PrefixTrait.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,21 @@ final protected function addPrefix(RouteCollection $routes, string|array $prefix
2929
}
3030
foreach ($routes->all() as $name => $route) {
3131
if (null === $locale = $route->getDefault('_locale')) {
32+
$priority = $routes->getPriority($name) ?? 0;
3233
$routes->remove($name);
3334
foreach ($prefix as $locale => $localePrefix) {
3435
$localizedRoute = clone $route;
3536
$localizedRoute->setDefault('_locale', $locale);
3637
$localizedRoute->setRequirement('_locale', preg_quote($locale));
3738
$localizedRoute->setDefault('_canonical_route', $name);
3839
$localizedRoute->setPath($localePrefix.(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath()));
39-
$routes->add($name.'.'.$locale, $localizedRoute);
40+
$routes->add($name.'.'.$locale, $localizedRoute, $priority);
4041
}
4142
} elseif (!isset($prefix[$locale])) {
4243
throw new \InvalidArgumentException(sprintf('Route "%s" with locale "%s" is missing a corresponding prefix in its parent collection.', $name, $locale));
4344
} else {
4445
$route->setPath($prefix[$locale].(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath()));
45-
$routes->add($name, $route);
46+
$routes->add($name, $route, $routes->getPriority($name) ?? 0);
4647
}
4748
}
4849

src/Symfony/Component/Routing/RouteCollection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,4 +407,9 @@ public function getAlias(string $name): ?Alias
407407
{
408408
return $this->aliases[$name] ?? null;
409409
}
410+
411+
public function getPriority(string $name): ?int
412+
{
413+
return $this->priorities[$name] ?? null;
414+
}
410415
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures;
4+
5+
use Symfony\Component\Routing\Annotation\Route;
6+
7+
class RouteWithPriorityController
8+
{
9+
#[Route('/important', name: 'important', priority: 2)]
10+
public function important()
11+
{
12+
}
13+
14+
#[Route('/also-important', name: 'also_important', priority: 1, defaults: ['_locale' => 'cs'])]
15+
public function alsoImportant()
16+
{
17+
}
18+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
important_controllers:
2+
resource: Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\RouteWithPriorityController
3+
type: annotation
4+
prefix:
5+
cs: /cs
6+
en: /en

src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,25 @@ public function testImportingAliases()
463463
$this->assertEquals($expectedRoutes('yaml'), $routes);
464464
}
465465

466+
public function testPriorityWithPrefix()
467+
{
468+
new LoaderResolver([
469+
$loader = new YamlFileLoader(new FileLocator(\dirname(__DIR__).'/Fixtures/localized')),
470+
new class() extends AnnotationClassLoader {
471+
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot): void
472+
{
473+
$route->setDefault('_controller', $class->getName().'::'.$method->getName());
474+
}
475+
},
476+
]);
477+
478+
$routes = $loader->load('localized-prefix.yml');
479+
480+
$this->assertSame(2, $routes->getPriority('important.cs'));
481+
$this->assertSame(2, $routes->getPriority('important.en'));
482+
$this->assertSame(1, $routes->getPriority('also_important'));
483+
}
484+
466485
/**
467486
* @dataProvider providePsr4ConfigFiles
468487
*/

0 commit comments

Comments
 (0)