Skip to content

Commit 56b6841

Browse files
authored
Merge pull request #2377 from sctr/3.x
Fixed SF 6.1 and SF 6.2 deprecations
2 parents b888195 + b9d40f1 commit 56b6841

File tree

20 files changed

+199
-53
lines changed

20 files changed

+199
-53
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ jobs:
4646
- php-version: 8.1
4747
composer-flags: ""
4848
can-fail: false
49+
symfony-require: "6.1.*"
50+
- php-version: 8.1
51+
composer-flags: ""
52+
can-fail: false
4953

5054
steps:
5155
- name: "Checkout"
52-
uses: "actions/checkout@v2"
56+
uses: "actions/checkout@v3"
5357

5458
- name: "Install PHP with XDebug"
5559
uses: "shivammathur/setup-php@v2"
@@ -68,7 +72,7 @@ jobs:
6872
tools: "composer:v2,flex"
6973

7074
- name: "Cache dependencies installed with composer"
71-
uses: "actions/cache@v2"
75+
uses: "actions/cache@v3"
7276
with:
7377
path: "~/.composer/cache"
7478
key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}"

DependencyInjection/Compiler/FormatListenerRulesPass.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@
1111

1212
namespace FOS\RestBundle\DependencyInjection\Compiler;
1313

14-
use Symfony\Component\DependencyInjection\ChildDefinition;
1514
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1615
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Definition;
1717
use Symfony\Component\DependencyInjection\Reference;
18+
use Symfony\Component\HttpFoundation\ChainRequestMatcher;
19+
use Symfony\Component\HttpFoundation\RequestMatcher;
20+
use Symfony\Component\HttpFoundation\RequestMatcher\AttributesRequestMatcher;
21+
use Symfony\Component\HttpFoundation\RequestMatcher\HostRequestMatcher;
22+
use Symfony\Component\HttpFoundation\RequestMatcher\MethodRequestMatcher;
23+
use Symfony\Component\HttpFoundation\RequestMatcher\PathRequestMatcher;
1824

1925
/**
2026
* @author Eduardo Gulias Davis <[email protected]>
@@ -83,9 +89,26 @@ private function createRequestMatcher(ContainerBuilder $container, ?string $path
8389

8490
if (!$container->hasDefinition($id)) {
8591
// only add arguments that are necessary
86-
$container
87-
->setDefinition($id, new ChildDefinition('fos_rest.format_request_matcher'))
88-
->setArguments($arguments);
92+
if (!class_exists(ChainRequestMatcher::class)) {
93+
$container->setDefinition($id, new Definition(RequestMatcher::class, $arguments));
94+
} else {
95+
$matchers = [];
96+
if (!is_null($path)) {
97+
$matchers[] = new Definition(PathRequestMatcher::class, [$path]);
98+
}
99+
if (!is_null($host)) {
100+
$matchers[] = new Definition(HostRequestMatcher::class, [$host]);
101+
}
102+
if (!is_null($methods)) {
103+
$matchers[] = new Definition(MethodRequestMatcher::class, [$methods]);
104+
}
105+
if ([] !== $attributes) {
106+
$matchers[] = new Definition(AttributesRequestMatcher::class, [$attributes]);
107+
}
108+
$container
109+
->setDefinition($id, new Definition(ChainRequestMatcher::class))
110+
->setArguments([$matchers]);
111+
}
89112
}
90113

91114
return new Reference($id);

DependencyInjection/FOSRestExtension.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2525
use Symfony\Component\DependencyInjection\Reference;
2626
use Symfony\Component\Form\Extension\Core\Type\FormType;
27+
use Symfony\Component\HttpFoundation\ChainRequestMatcher;
28+
use Symfony\Component\HttpFoundation\RequestMatcher;
29+
use Symfony\Component\HttpFoundation\RequestMatcher\HostRequestMatcher;
30+
use Symfony\Component\HttpFoundation\RequestMatcher\IpsRequestMatcher;
31+
use Symfony\Component\HttpFoundation\RequestMatcher\MethodRequestMatcher;
32+
use Symfony\Component\HttpFoundation\RequestMatcher\PathRequestMatcher;
2733
use Symfony\Component\HttpFoundation\Response;
2834
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
2935
use Symfony\Component\Validator\Constraint;
@@ -399,10 +405,26 @@ private function createZoneRequestMatcher(ContainerBuilder $container, ?string $
399405
array_pop($arguments);
400406
}
401407

402-
$container
403-
->setDefinition($id, new ChildDefinition('fos_rest.zone_request_matcher'))
404-
->setArguments($arguments)
405-
;
408+
if (!class_exists(ChainRequestMatcher::class)) {
409+
$container->setDefinition($id, new Definition(RequestMatcher::class, $arguments));
410+
} else {
411+
$matchers = [];
412+
if (!is_null($path)) {
413+
$matchers[] = new Definition(PathRequestMatcher::class, [$path]);
414+
}
415+
if (!is_null($host)) {
416+
$matchers[] = new Definition(HostRequestMatcher::class, [$host]);
417+
}
418+
if (!is_null($methods)) {
419+
$matchers[] = new Definition(MethodRequestMatcher::class, [$methods]);
420+
}
421+
if (!is_null($ips)) {
422+
$matchers[] = new Definition(IpsRequestMatcher::class, [$ips]);
423+
}
424+
$container
425+
->setDefinition($id, new Definition(ChainRequestMatcher::class))
426+
->setArguments([$matchers]);
427+
}
406428

407429
return new Reference($id);
408430
}

Request/RequestBodyParamConverter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function apply(Request $request, ParamConverter $configuration): bool
7676
}
7777
$this->configureContext($context = new Context(), $arrayContext);
7878

79-
$format = $request->getContentType();
79+
$format = method_exists(Request::class, 'getContentTypeFormat') ? $request->getContentTypeFormat() : $request->getContentType();
8080
if (null === $format) {
8181
return $this->throwException(new UnsupportedMediaTypeHttpException(), $configuration);
8282
}

Resources/config/format_listener.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<services>
8-
98
<service id="fos_rest.format_listener" class="FOS\RestBundle\EventListener\FormatListener">
109
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="34" />
1110
<argument type="service" id="fos_rest.format_negotiator" />
@@ -14,7 +13,5 @@
1413
<service id="fos_rest.format_negotiator" class="FOS\RestBundle\Negotiation\FormatNegotiator">
1514
<argument type="service" id="request_stack" />
1615
</service>
17-
18-
<service id="fos_rest.format_request_matcher" class="Symfony\Component\HttpFoundation\RequestMatcher" public="false" abstract="true" />
1916
</services>
2017
</container>

Resources/config/zone_matcher_listener.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="248" />
1111
</service>
1212

13-
<service id="fos_rest.zone_request_matcher" class="Symfony\Component\HttpFoundation\RequestMatcher" public="false" />
14-
1513
</services>
1614

1715
</container>

Serializer/Normalizer/FlattenExceptionNormalizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
use FOS\RestBundle\Util\ExceptionValueMap;
1616
use Symfony\Component\ErrorHandler\Exception\FlattenException;
1717
use Symfony\Component\HttpFoundation\Response;
18-
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
18+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
1919

2020
/**
2121
* @author Christian Flothmann <[email protected]>
2222
*
2323
* @internal
2424
*/
25-
final class FlattenExceptionNormalizer implements ContextAwareNormalizerInterface
25+
final class FlattenExceptionNormalizer implements NormalizerInterface
2626
{
2727
private $statusCodeMap;
2828
private $messagesMap;

Serializer/Normalizer/FormErrorNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function normalize($object, $format = null, array $context = []): array
3838
/**
3939
* {@inheritdoc}
4040
*/
41-
public function supportsNormalization($data, $format = null): bool
41+
public function supportsNormalization($data, $format = null, array $context = []): bool
4242
{
4343
return $data instanceof FormInterface && $data->isSubmitted() && !$data->isValid();
4444
}

Tests/DependencyInjection/FOSRestExtensionTest.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1919
use Symfony\Component\DependencyInjection\ChildDefinition;
2020
use Symfony\Component\DependencyInjection\ContainerBuilder;
21+
use Symfony\Component\DependencyInjection\Definition;
2122
use Symfony\Component\DependencyInjection\Reference;
2223
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
24+
use Symfony\Component\HttpFoundation\ChainRequestMatcher;
2325

2426
/**
2527
* FOSRestExtension test.
@@ -533,17 +535,26 @@ public function testZoneMatcherListener()
533535
$requestMatcherFirstId = (string) $addRequestMatcherCalls[0][1][0];
534536
$requestMatcherFirst = $this->container->getDefinition($requestMatcherFirstId);
535537

536-
$this->assertInstanceOf(ChildDefinition::class, $requestMatcherFirst);
537-
$this->assertEquals('/api/*', $requestMatcherFirst->getArgument(0));
538+
$this->assertInstanceOf(Definition::class, $requestMatcherFirst);
539+
if (!class_exists(ChainRequestMatcher::class)) {
540+
$this->assertEquals('/api/*', $requestMatcherFirst->getArgument(0));
541+
} else {
542+
$this->assertEquals('/api/*', $requestMatcherFirst->getArgument(0)[0]->getArgument(0));
543+
}
538544

539545
// Second zone
540546
$this->assertEquals('addRequestMatcher', $addRequestMatcherCalls[1][0]);
541547
$requestMatcherSecondId = (string) $addRequestMatcherCalls[1][1][0];
542548
$requestMatcherSecond = $this->container->getDefinition($requestMatcherSecondId);
543549

544-
$this->assertInstanceOf(ChildDefinition::class, $requestMatcherSecond);
545-
$this->assertEquals('/^second', $requestMatcherSecond->getArgument(0));
546-
$this->assertEquals(['127.0.0.1'], $requestMatcherSecond->getArgument(3));
550+
$this->assertInstanceOf(Definition::class, $requestMatcherSecond);
551+
if (!class_exists(ChainRequestMatcher::class)) {
552+
$this->assertEquals('/^second', $requestMatcherSecond->getArgument(0));
553+
$this->assertEquals(['127.0.0.1'], $requestMatcherSecond->getArgument(3));
554+
} else {
555+
$this->assertEquals('/^second', $requestMatcherSecond->getArgument(0)[0]->getArgument(0));
556+
$this->assertEquals(['127.0.0.1'], $requestMatcherSecond->getArgument(0)[2]->getArgument(0));
557+
}
547558
}
548559

549560
public function testMimeTypesArePassedArrays()

Tests/EventListener/FormatListenerTest.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
use FOS\RestBundle\FOSRestBundle;
1616
use FOS\RestBundle\Negotiation\FormatNegotiator;
1717
use PHPUnit\Framework\TestCase;
18+
use Symfony\Component\HttpFoundation\ChainRequestMatcher;
1819
use Symfony\Component\HttpFoundation\Request;
1920
use Symfony\Component\HttpFoundation\RequestMatcher;
2021
use Symfony\Component\HttpFoundation\RequestStack;
2122
use Symfony\Component\HttpKernel\Event\RequestEvent;
2223
use Symfony\Component\HttpKernel\Exception\HttpException;
2324
use Symfony\Component\HttpKernel\HttpKernelInterface;
25+
use Symfony\Component\HttpKernel\Kernel;
2426

2527
/**
2628
* Request listener test.
@@ -44,7 +46,7 @@ public function testOnKernelControllerNegotiation()
4446
$requestStack = new RequestStack();
4547
$requestStack->push($request);
4648
$formatNegotiator = new FormatNegotiator($requestStack);
47-
$formatNegotiator->add(new RequestMatcher('/'), [
49+
$formatNegotiator->add($this->getRequestMatcher('/'), [
4850
'fallback_format' => 'xml',
4951
]);
5052

@@ -72,7 +74,7 @@ public function testOnKernelControllerNoZone()
7274
->will($this->returnValue($request));
7375

7476
$formatNegotiator = new FormatNegotiator($requestStack);
75-
$formatNegotiator->add(new RequestMatcher('/'), ['fallback_format' => 'json']);
77+
$formatNegotiator->add($this->getRequestMatcher('/'), ['fallback_format' => 'json']);
7678

7779
$listener = new FormatListener($formatNegotiator);
7880

@@ -98,8 +100,8 @@ public function testOnKernelControllerNegotiationStopped()
98100
->will($this->returnValue($request));
99101

100102
$formatNegotiator = new FormatNegotiator($requestStack);
101-
$formatNegotiator->add(new RequestMatcher('/'), ['stop' => true]);
102-
$formatNegotiator->add(new RequestMatcher('/'), ['fallback_format' => 'json']);
103+
$formatNegotiator->add($this->getRequestMatcher('/'), ['stop' => true]);
104+
$formatNegotiator->add($this->getRequestMatcher('/'), ['fallback_format' => 'json']);
103105

104106
$listener = new FormatListener($formatNegotiator);
105107

@@ -157,7 +159,7 @@ public function testUseSpecifiedFormat($format, $result)
157159
$requestStack = new RequestStack();
158160
$requestStack->push($request);
159161
$formatNegotiator = new FormatNegotiator($requestStack);
160-
$formatNegotiator->add(new RequestMatcher('/'), [
162+
$formatNegotiator->add($this->getRequestMatcher('/'), [
161163
'fallback_format' => 'xml',
162164
]);
163165

@@ -202,7 +204,7 @@ public function testSfFragmentFormat()
202204
$requestStack = new RequestStack();
203205
$requestStack->push($request);
204206
$formatNegotiator = new FormatNegotiator($requestStack);
205-
$formatNegotiator->add(new RequestMatcher('/'), [
207+
$formatNegotiator->add($this->getRequestMatcher('/'), [
206208
'fallback_format' => 'json',
207209
]);
208210

@@ -212,4 +214,13 @@ public function testSfFragmentFormat()
212214

213215
$this->assertEquals($request->getRequestFormat(), 'json');
214216
}
217+
218+
private function getRequestMatcher(string $path)
219+
{
220+
if (Kernel::VERSION_ID < 60200) {
221+
return new RequestMatcher($path);
222+
}
223+
224+
return new ChainRequestMatcher([new RequestMatcher\PathRequestMatcher($path)]);
225+
}
215226
}

0 commit comments

Comments
 (0)