Skip to content

Commit 6190a8f

Browse files
Merge branch '7.0' into 7.1
* 7.0: Fix merge [Routing] Fixed priority getting lost when defining prefix array [DoctrineBridge]  Fix detection of Xml/Yaml driver in DoctrineExtension [Messenger] PhpSerializer: TypeError should throw MessageDecodingFailedException
2 parents e172491 + c8389b0 commit 6190a8f

File tree

8 files changed

+84
-16
lines changed

8 files changed

+84
-16
lines changed

src/Symfony/Bridge/Doctrine/DependencyInjection/AbstractDoctrineExtension.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,9 @@ protected function registerMappingDrivers(array $objectManager, ContainerBuilder
193193
array_values($driverPaths),
194194
]);
195195
}
196-
if (str_contains($mappingDriverDef->getClass(), 'yml') || str_contains($mappingDriverDef->getClass(), 'xml')) {
196+
if (str_contains($mappingDriverDef->getClass(), 'yml') || str_contains($mappingDriverDef->getClass(), 'xml')
197+
|| str_contains($mappingDriverDef->getClass(), 'Yaml') || str_contains($mappingDriverDef->getClass(), 'Xml')
198+
) {
197199
$mappingDriverDef->setArguments([array_flip($driverPaths)]);
198200
$mappingDriverDef->addMethodCall('setGlobalBasename', ['mapping']);
199201
}

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
@@ -380,4 +380,9 @@ public function getAlias(string $name): ?Alias
380380
{
381381
return $this->aliases[$name] ?? null;
382382
}
383+
384+
public function getPriority(string $name): ?int
385+
{
386+
return $this->priorities[$name] ?? null;
387+
}
383388
}
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: attribute
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
@@ -470,6 +470,25 @@ public function testImportingAliases()
470470
$this->assertEquals($expectedRoutes('yaml'), $routes);
471471
}
472472

473+
public function testPriorityWithPrefix()
474+
{
475+
new LoaderResolver([
476+
$loader = new YamlFileLoader(new FileLocator(\dirname(__DIR__).'/Fixtures/localized')),
477+
new class() extends AttributeClassLoader {
478+
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot): void
479+
{
480+
$route->setDefault('_controller', $class->getName().'::'.$method->getName());
481+
}
482+
},
483+
]);
484+
485+
$routes = $loader->load('localized-prefix.yml');
486+
487+
$this->assertSame(2, $routes->getPriority('important.cs'));
488+
$this->assertSame(2, $routes->getPriority('important.en'));
489+
$this->assertSame(1, $routes->getPriority('also_important'));
490+
}
491+
473492
/**
474493
* @dataProvider providePsr4ConfigFiles
475494
*/

0 commit comments

Comments
 (0)