Skip to content

Commit eba77e9

Browse files
committed
fixed RequestMatcher deprecations
1 parent 4e044dd commit eba77e9

File tree

6 files changed

+74
-25
lines changed

6 files changed

+74
-25
lines changed

DependencyInjection/Compiler/FormatListenerRulesPass.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,20 @@ private function createRequestMatcher(ContainerBuilder $container, ?string $path
9090
if (!$container->hasDefinition($id)) {
9191
// only add arguments that are necessary
9292
if (!class_exists(ChainRequestMatcher::class)) {
93-
$container
94-
->setDefinition($id, new Definition(RequestMatcher::class, $arguments));
93+
$container->setDefinition($id, new Definition(RequestMatcher::class, $arguments));
9594
} else {
9695
$matchers = [];
97-
if (!empty($path)) {
98-
$matchers[] = new PathRequestMatcher($path);
96+
if (!is_null($path)) {
97+
$matchers[] = new Definition(PathRequestMatcher::class, [$path]);
9998
}
100-
if (!empty($host)) {
101-
$matchers[] = new HostRequestMatcher($host);
99+
if (!is_null($host)) {
100+
$matchers[] = new Definition(HostRequestMatcher::class, [$host]);
102101
}
103-
if (!empty($methods)) {
104-
$matchers[] = new MethodRequestMatcher($methods);
102+
if (!is_null($methods)) {
103+
$matchers[] = new Definition(MethodRequestMatcher::class, [$methods]);
105104
}
106-
if (!empty($attributes)) {
107-
$matchers[] = new AttributesRequestMatcher($attributes);
105+
if ($attributes !== []) {
106+
$matchers[] = new Definition(AttributesRequestMatcher::class, [$attributes]);
108107
}
109108
$container
110109
->setDefinition($id, new Definition(ChainRequestMatcher::class))

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
}

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>

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/Functional/app/ViewResponseListener/config.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
imports:
22
- { resource: ../config/default.yml }
33
- { resource: ../config/sensio_framework_extra.yml }
4-
5-
framework:
6-
serializer:
7-
enabled: true
4+
- { resource: framework.php }
85

96
fos_rest:
107
view:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRestBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
$frameworkConfig = [
13+
'serializer' => [
14+
'enabled' => true,
15+
],
16+
];
17+
18+
if (\Symfony\Component\HttpKernel\Kernel::VERSION_ID >= 60100) {
19+
$frameworkConfig['http_method_override'] = true;
20+
}
21+
22+
$container->loadFromExtension('framework', $frameworkConfig);

0 commit comments

Comments
 (0)