Skip to content

Commit

Permalink
Merge pull request #2377 from sctr/3.x
Browse files Browse the repository at this point in the history
Fixed SF 6.1 and SF 6.2 deprecations
  • Loading branch information
goetas authored Jan 6, 2023
2 parents b888195 + b9d40f1 commit 56b6841
Show file tree
Hide file tree
Showing 20 changed files with 199 additions and 53 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ jobs:
- php-version: 8.1
composer-flags: ""
can-fail: false
symfony-require: "6.1.*"
- php-version: 8.1
composer-flags: ""
can-fail: false

steps:
- name: "Checkout"
uses: "actions/checkout@v2"
uses: "actions/checkout@v3"

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

- name: "Cache dependencies installed with composer"
uses: "actions/cache@v2"
uses: "actions/cache@v3"
with:
path: "~/.composer/cache"
key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}"
Expand Down
31 changes: 27 additions & 4 deletions DependencyInjection/Compiler/FormatListenerRulesPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@

namespace FOS\RestBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\ChainRequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcher\AttributesRequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcher\HostRequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcher\MethodRequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcher\PathRequestMatcher;

/**
* @author Eduardo Gulias Davis <me@egulias.com>
Expand Down Expand Up @@ -83,9 +89,26 @@ private function createRequestMatcher(ContainerBuilder $container, ?string $path

if (!$container->hasDefinition($id)) {
// only add arguments that are necessary
$container
->setDefinition($id, new ChildDefinition('fos_rest.format_request_matcher'))
->setArguments($arguments);
if (!class_exists(ChainRequestMatcher::class)) {
$container->setDefinition($id, new Definition(RequestMatcher::class, $arguments));
} else {
$matchers = [];
if (!is_null($path)) {
$matchers[] = new Definition(PathRequestMatcher::class, [$path]);
}
if (!is_null($host)) {
$matchers[] = new Definition(HostRequestMatcher::class, [$host]);
}
if (!is_null($methods)) {
$matchers[] = new Definition(MethodRequestMatcher::class, [$methods]);
}
if ([] !== $attributes) {
$matchers[] = new Definition(AttributesRequestMatcher::class, [$attributes]);
}
$container
->setDefinition($id, new Definition(ChainRequestMatcher::class))
->setArguments([$matchers]);
}
}

return new Reference($id);
Expand Down
30 changes: 26 additions & 4 deletions DependencyInjection/FOSRestExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\HttpFoundation\ChainRequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcher\HostRequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcher\IpsRequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcher\MethodRequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcher\PathRequestMatcher;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Validator\Constraint;
Expand Down Expand Up @@ -399,10 +405,26 @@ private function createZoneRequestMatcher(ContainerBuilder $container, ?string $
array_pop($arguments);
}

$container
->setDefinition($id, new ChildDefinition('fos_rest.zone_request_matcher'))
->setArguments($arguments)
;
if (!class_exists(ChainRequestMatcher::class)) {
$container->setDefinition($id, new Definition(RequestMatcher::class, $arguments));
} else {
$matchers = [];
if (!is_null($path)) {
$matchers[] = new Definition(PathRequestMatcher::class, [$path]);
}
if (!is_null($host)) {
$matchers[] = new Definition(HostRequestMatcher::class, [$host]);
}
if (!is_null($methods)) {
$matchers[] = new Definition(MethodRequestMatcher::class, [$methods]);
}
if (!is_null($ips)) {
$matchers[] = new Definition(IpsRequestMatcher::class, [$ips]);
}
$container
->setDefinition($id, new Definition(ChainRequestMatcher::class))
->setArguments([$matchers]);
}

return new Reference($id);
}
Expand Down
2 changes: 1 addition & 1 deletion Request/RequestBodyParamConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function apply(Request $request, ParamConverter $configuration): bool
}
$this->configureContext($context = new Context(), $arrayContext);

$format = $request->getContentType();
$format = method_exists(Request::class, 'getContentTypeFormat') ? $request->getContentTypeFormat() : $request->getContentType();
if (null === $format) {
return $this->throwException(new UnsupportedMediaTypeHttpException(), $configuration);
}
Expand Down
3 changes: 0 additions & 3 deletions Resources/config/format_listener.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>

<service id="fos_rest.format_listener" class="FOS\RestBundle\EventListener\FormatListener">
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="34" />
<argument type="service" id="fos_rest.format_negotiator" />
Expand All @@ -14,7 +13,5 @@
<service id="fos_rest.format_negotiator" class="FOS\RestBundle\Negotiation\FormatNegotiator">
<argument type="service" id="request_stack" />
</service>

<service id="fos_rest.format_request_matcher" class="Symfony\Component\HttpFoundation\RequestMatcher" public="false" abstract="true" />
</services>
</container>
2 changes: 0 additions & 2 deletions Resources/config/zone_matcher_listener.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="248" />
</service>

<service id="fos_rest.zone_request_matcher" class="Symfony\Component\HttpFoundation\RequestMatcher" public="false" />

</services>

</container>
4 changes: 2 additions & 2 deletions Serializer/Normalizer/FlattenExceptionNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
use FOS\RestBundle\Util\ExceptionValueMap;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
* @author Christian Flothmann <christian.flothmann@sensiolabs.de>
*
* @internal
*/
final class FlattenExceptionNormalizer implements ContextAwareNormalizerInterface
final class FlattenExceptionNormalizer implements NormalizerInterface
{
private $statusCodeMap;
private $messagesMap;
Expand Down
2 changes: 1 addition & 1 deletion Serializer/Normalizer/FormErrorNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function normalize($object, $format = null, array $context = []): array
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null): bool
public function supportsNormalization($data, $format = null, array $context = []): bool
{
return $data instanceof FormInterface && $data->isSubmitted() && !$data->isValid();
}
Expand Down
21 changes: 16 additions & 5 deletions Tests/DependencyInjection/FOSRestExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
use Symfony\Component\HttpFoundation\ChainRequestMatcher;

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

$this->assertInstanceOf(ChildDefinition::class, $requestMatcherFirst);
$this->assertEquals('/api/*', $requestMatcherFirst->getArgument(0));
$this->assertInstanceOf(Definition::class, $requestMatcherFirst);
if (!class_exists(ChainRequestMatcher::class)) {
$this->assertEquals('/api/*', $requestMatcherFirst->getArgument(0));
} else {
$this->assertEquals('/api/*', $requestMatcherFirst->getArgument(0)[0]->getArgument(0));
}

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

$this->assertInstanceOf(ChildDefinition::class, $requestMatcherSecond);
$this->assertEquals('/^second', $requestMatcherSecond->getArgument(0));
$this->assertEquals(['127.0.0.1'], $requestMatcherSecond->getArgument(3));
$this->assertInstanceOf(Definition::class, $requestMatcherSecond);
if (!class_exists(ChainRequestMatcher::class)) {
$this->assertEquals('/^second', $requestMatcherSecond->getArgument(0));
$this->assertEquals(['127.0.0.1'], $requestMatcherSecond->getArgument(3));
} else {
$this->assertEquals('/^second', $requestMatcherSecond->getArgument(0)[0]->getArgument(0));
$this->assertEquals(['127.0.0.1'], $requestMatcherSecond->getArgument(0)[2]->getArgument(0));
}
}

public function testMimeTypesArePassedArrays()
Expand Down
23 changes: 17 additions & 6 deletions Tests/EventListener/FormatListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
use FOS\RestBundle\FOSRestBundle;
use FOS\RestBundle\Negotiation\FormatNegotiator;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\ChainRequestMatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcher;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Kernel;

/**
* Request listener test.
Expand All @@ -44,7 +46,7 @@ public function testOnKernelControllerNegotiation()
$requestStack = new RequestStack();
$requestStack->push($request);
$formatNegotiator = new FormatNegotiator($requestStack);
$formatNegotiator->add(new RequestMatcher('/'), [
$formatNegotiator->add($this->getRequestMatcher('/'), [
'fallback_format' => 'xml',
]);

Expand Down Expand Up @@ -72,7 +74,7 @@ public function testOnKernelControllerNoZone()
->will($this->returnValue($request));

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

$listener = new FormatListener($formatNegotiator);

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

$formatNegotiator = new FormatNegotiator($requestStack);
$formatNegotiator->add(new RequestMatcher('/'), ['stop' => true]);
$formatNegotiator->add(new RequestMatcher('/'), ['fallback_format' => 'json']);
$formatNegotiator->add($this->getRequestMatcher('/'), ['stop' => true]);
$formatNegotiator->add($this->getRequestMatcher('/'), ['fallback_format' => 'json']);

$listener = new FormatListener($formatNegotiator);

Expand Down Expand Up @@ -157,7 +159,7 @@ public function testUseSpecifiedFormat($format, $result)
$requestStack = new RequestStack();
$requestStack->push($request);
$formatNegotiator = new FormatNegotiator($requestStack);
$formatNegotiator->add(new RequestMatcher('/'), [
$formatNegotiator->add($this->getRequestMatcher('/'), [
'fallback_format' => 'xml',
]);

Expand Down Expand Up @@ -202,7 +204,7 @@ public function testSfFragmentFormat()
$requestStack = new RequestStack();
$requestStack->push($request);
$formatNegotiator = new FormatNegotiator($requestStack);
$formatNegotiator->add(new RequestMatcher('/'), [
$formatNegotiator->add($this->getRequestMatcher('/'), [
'fallback_format' => 'json',
]);

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

$this->assertEquals($request->getRequestFormat(), 'json');
}

private function getRequestMatcher(string $path)
{
if (Kernel::VERSION_ID < 60200) {
return new RequestMatcher($path);
}

return new ChainRequestMatcher([new RequestMatcher\PathRequestMatcher($path)]);
}
}
6 changes: 1 addition & 5 deletions Tests/Functional/app/BasicAuth/config.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
imports:
- { resource: ../config/default.yml }
- { resource: security.php }

framework:
serializer:
enabled: true
router: { resource: "%kernel.project_dir%/BasicAuth/routing.yml" }
- { resource: framework.php }

fos_rest:
zone:
Expand Down
25 changes: 25 additions & 0 deletions Tests/Functional/app/BasicAuth/framework.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

$frameworkConfig = [
'serializer' => [
'enabled' => true,
],
'router' => [
'resource' => '%kernel.project_dir%/BasicAuth/routing.yml',
],
];

if (\Symfony\Component\HttpKernel\Kernel::VERSION_ID >= 60100) {
$frameworkConfig['http_method_override'] = true;
}

$container->loadFromExtension('framework', $frameworkConfig);
7 changes: 6 additions & 1 deletion Tests/Functional/app/BasicAuth/security.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* file that was distributed with this source code.
*/

use Symfony\Component\HttpKernel\Kernel;

$securityConfig = [
'providers' => [
'in_memory' => [
Expand Down Expand Up @@ -41,7 +43,10 @@
// BC layer to avoid deprecation warnings in symfony/security-bundle < 5.3
if (class_exists(\Symfony\Bundle\SecurityBundle\RememberMe\FirewallAwareRememberMeHandler::class)) {
$securityConfig['password_hashers'] = $passwordHasherConfig;
$securityConfig['enable_authenticator_manager'] = true;
// BC layer to avoid deprecation warnings in symfony/security-bundle < 6.2
if (Kernel::VERSION_ID < 60200) {
$securityConfig['enable_authenticator_manager'] = true;
}
} else {
$securityConfig['encoders'] = $passwordHasherConfig;
}
Expand Down
10 changes: 1 addition & 9 deletions Tests/Functional/app/Configuration/config.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
imports:
- { resource: ../config/default.yml }
- { resource: ../config/sensio_framework_extra.yml }
- { resource: framework.php }
- { resource: security.php }

framework:
annotations:
enabled: true
property_access: ~
serializer:
enabled: true
router: { resource: "%kernel.project_dir%/Configuration/routing.yml" }
profiler: { only_exceptions: false }

fos_rest:
view:
mime_types: true
Expand Down
Loading

0 comments on commit 56b6841

Please sign in to comment.