Skip to content

Commit 348031c

Browse files
nikola-jovanovic-phpdev-craftectjveldhuizenalexander-schranzHypeMC
authored
Add Symfony 8.0 support (#1)
* Update AbstractScalarParam.php Fixes "Passing an array of options to configure the \"FOS\\RestBundle\\Validator\\Constraints\\Regex\" constraint is deprecated, use named arguments instead." * Update AbstractScalarParam.php Support php <8.0 * Fix deprecations in symfony/validator, including the fix in FriendsOfSymfony#2417 Fixes FriendsOfSymfony#2416 * Fix MimeTypeTest for Symfony 7.4 * Don't use deprecated `#[Route]` methods * Switch to php for service configurations Using xml is deprecated in symfony 7.4 * Fix usage of deprecated method Request::get() * Fix deprecated usages of symfony/validator * Add support for symfony 8 * Test against PHP 8.5 * Fix noop call of deprecated setAccessible() in PHP > 8.0 * Fix build status badge * Remove broken SensioLabsInsight badge * Remove outdated scrutinizer badges * Allow usage with symfony/config v8 --------- Co-authored-by: dev-craftec <89782875+dev-craftec@users.noreply.github.com> Co-authored-by: Thijs-jan Veldhuizen <thijs-jan.veldhuizen@nevobo.nl> Co-authored-by: Alexander Schranz <alexander@sulu.io> Co-authored-by: HypeMC <hypemc@gmail.com> Co-authored-by: Asmir Mustafic <goetas@gmail.com> Co-authored-by: W0rma <beck.worma@gmail.com>
1 parent e3a8632 commit 348031c

14 files changed

Lines changed: 151 additions & 77 deletions

File tree

.github/workflows/continuous-integration.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ jobs:
5353
composer-flags: ""
5454
can-fail: false
5555
symfony-require: "6.4.*"
56+
- php-version: "8.5"
57+
composer-flags: ""
58+
can-fail: false
59+
symfony-require: "6.4.*"
5660
- php-version: "8.3"
5761
composer-flags: ""
5862
can-fail: false
@@ -68,6 +72,36 @@ jobs:
6872
can-fail: false
6973
symfony-require: "7.0.*"
7074
remove-sensio-bundle: yes # SensioFrameworkExtraBundle is not compatible with Symfony 7.0 or later
75+
- php-version: "8.3"
76+
composer-flags: ""
77+
can-fail: false
78+
symfony-require: "7.0.*"
79+
remove-sensio-bundle: yes # SensioFrameworkExtraBundle is not compatible with Symfony 7.0 or later
80+
- php-version: "8.4"
81+
composer-flags: ""
82+
can-fail: false
83+
symfony-require: "7.0.*"
84+
remove-sensio-bundle: yes # SensioFrameworkExtraBundle is not compatible with Symfony 7.0 or later
85+
- php-version: "8.4"
86+
composer-flags: ""
87+
can-fail: false
88+
symfony-require: "7.4.*"
89+
remove-sensio-bundle: yes # SensioFrameworkExtraBundle is not compatible with Symfony 7.0 or later
90+
- php-version: "8.5"
91+
composer-flags: ""
92+
can-fail: false
93+
symfony-require: "7.4.*"
94+
remove-sensio-bundle: yes # SensioFrameworkExtraBundle is not compatible with Symfony 7.0 or later
95+
- php-version: "8.4"
96+
composer-flags: ""
97+
can-fail: false
98+
symfony-require: "8.0.*"
99+
remove-sensio-bundle: yes # SensioFrameworkExtraBundle is not compatible with Symfony 7.0 or later
100+
- php-version: "8.5"
101+
composer-flags: ""
102+
can-fail: false
103+
symfony-require: "8.0.*"
104+
remove-sensio-bundle: yes # SensioFrameworkExtraBundle is not compatible with Symfony 7.0 or later
71105
- php-version: "8.3"
72106
composer-flags: ""
73107
can-fail: true # we don't want to fail the build if we are incompatible with the next (unstable) Symfony version
@@ -76,6 +110,10 @@ jobs:
76110
composer-flags: ""
77111
can-fail: true # we don't want to fail the build if we are incompatible with the next (unstable) Symfony version
78112
remove-sensio-bundle: yes # SensioFrameworkExtraBundle is not compatible with Symfony 7.0 or later
113+
- php-version: "8.5"
114+
composer-flags: ""
115+
can-fail: true # we don't want to fail the build if we are incompatible with the next (unstable) Symfony version
116+
remove-sensio-bundle: yes # SensioFrameworkExtraBundle is not compatible with Symfony 7.0 or later
79117

80118
env:
81119
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Controller/Annotations/AbstractScalarParam.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ public function getConstraints()
4141
if ($this->requirements instanceof Constraint) {
4242
$constraints[] = $this->requirements;
4343
} elseif (is_scalar($this->requirements)) {
44-
$constraints[] = new Regex([
45-
'pattern' => '#^(?:'.$this->requirements.')$#xsu',
46-
'message' => sprintf(
44+
$constraints[] = new Regex(
45+
'#^(?:'.$this->requirements.')$#xsu',
46+
sprintf(
4747
'Parameter \'%s\' value, does not match requirements \'%s\'',
4848
$this->getName(),
4949
$this->requirements
5050
),
51-
]);
51+
);
5252
} elseif (is_array($this->requirements) && isset($this->requirements['rule']) && $this->requirements['error_message']) {
53-
$constraints[] = new Regex([
54-
'pattern' => '#^(?:'.$this->requirements['rule'].')$#xsu',
55-
'message' => $this->requirements['error_message'],
56-
]);
53+
$constraints[] = new Regex(
54+
'#^(?:'.$this->requirements['rule'].')$#xsu',
55+
$this->requirements['error_message'],
56+
);
5757
} elseif (is_array($this->requirements)) {
5858
foreach ($this->requirements as $index => $requirement) {
5959
if ($requirement instanceof Constraint) {
@@ -75,9 +75,12 @@ public function getConstraints()
7575
// If the user wants to map the value, apply all constraints to every
7676
// value of the map
7777
if ($this->map) {
78-
$constraints = [
79-
new All(['constraints' => $constraints]),
80-
];
78+
if ([] !== $constraints) {
79+
$constraints = [
80+
new All($constraints),
81+
];
82+
}
83+
8184
if (false === $this->nullable) {
8285
$constraints[] = new NotNull();
8386
}

Controller/Annotations/FileParam.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,19 @@ public function getConstraints()
7979

8080
$options = is_array($this->requirements) ? $this->requirements : [];
8181
if ($this->image) {
82-
$constraints[] = new Image($options);
82+
$constraint = new Image();
8383
} else {
84-
$constraints[] = new File($options);
84+
$constraint = new File();
8585
}
86+
foreach ($options as $name => $value) {
87+
$constraint->$name = $value;
88+
}
89+
$constraints[] = $constraint;
8690

8791
// If the user wants to map the value
8892
if ($this->map) {
8993
$constraints = [
90-
new All(['constraints' => $constraints]),
94+
new All($constraints),
9195
];
9296
}
9397

Controller/Annotations/Route.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ public function __construct(
123123
);
124124
}
125125

126-
if (!$this->getMethods()) {
126+
if (isset($this->methods)) {
127+
if (!$this->methods) {
128+
$this->methods = (array) $this->getMethod();
129+
}
130+
} elseif (!$this->getMethods()) {
127131
$this->setMethods((array) $this->getMethod());
128132
}
129133
}

EventListener/AllowedMethodsListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ public function onKernelResponse(ResponseEvent $event): void
4141

4242
$allowedMethods = $this->loader->getAllowedMethods();
4343

44-
if (isset($allowedMethods[$event->getRequest()->get('_route')])) {
44+
if (isset($allowedMethods[$event->getRequest()->attributes->get('_route')])) {
4545
$event->getResponse()
4646
->headers
47-
->set('Allow', implode(', ', $allowedMethods[$event->getRequest()->get('_route')]));
47+
->set('Allow', implode(', ', $allowedMethods[$event->getRequest()->attributes->get('_route')]));
4848
}
4949
}
5050
}

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@ applications with Symfony. Features include:
1212
compatible with RFC 7807 using the Symfony Serializer component or the
1313
JMS Serializer
1414

15-
[![Build Status](https://img.shields.io/github/workflow/status/FriendsOfSymfony/FOSRestBundle/CI?style=flat-square)](https://github.com/FriendsOfSymfony/FOSRestBundle/actions?query=workflow:CI)
16-
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/FriendsOfSymfony/FOSRestBundle/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/FriendsOfSymfony/FOSRestBundle/?branch=master)
17-
[![Code Coverage](https://scrutinizer-ci.com/g/FriendsOfSymfony/FOSRestBundle/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/FriendsOfSymfony/FOSRestBundle/?branch=master)
15+
[![Build Status](https://github.com/FriendsOfSymfony/FOSRestBundle/actions/workflows/continuous-integration.yml/badge.svg)](https://github.com/FriendsOfSymfony/FOSRestBundle/actions/workflows/continuous-integration.yml)
1816
[![Total Downloads](https://poser.pugx.org/FriendsOfSymfony/rest-bundle/downloads.svg)](https://packagist.org/packages/FriendsOfSymfony/rest-bundle)
1917
[![Latest Stable Version](https://poser.pugx.org/FriendsOfSymfony/rest-bundle/v/stable.svg)](https://packagist.org/packages/FriendsOfSymfony/rest-bundle)
20-
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/0be23389-2e85-49cf-b333-caaa36d11c62/mini.png)](https://insight.sensiolabs.com/projects/0be23389-2e85-49cf-b333-caaa36d11c62)
2118

2219
Documentation
2320
-------------

Tests/Controller/Annotations/AbstractScalarParamTest.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ public function testScalarRequirements()
8080
$this->param->requirements = 'foo %bar% %%';
8181
$this->assertEquals([
8282
new NotNull(),
83-
new Regex([
84-
'pattern' => '#^(?:foo %bar% %%)$#xsu',
85-
'message' => "Parameter 'bar' value, does not match requirements 'foo %bar% %%'",
86-
]),
83+
new Regex(
84+
'#^(?:foo %bar% %%)$#xsu',
85+
"Parameter 'bar' value, does not match requirements 'foo %bar% %%'",
86+
),
8787
], $this->param->getConstraints());
8888
}
8989

@@ -95,10 +95,10 @@ public function testArrayRequirements()
9595
];
9696
$this->assertEquals([
9797
new NotNull(),
98-
new Regex([
99-
'pattern' => '#^(?:foo)$#xsu',
100-
'message' => 'bar',
101-
]),
98+
new Regex(
99+
'#^(?:foo)$#xsu',
100+
'bar',
101+
),
102102
], $this->param->getConstraints());
103103
}
104104

@@ -133,8 +133,6 @@ public function testArrayWithNoConstraintsDoesNotCreateInvalidConstraint()
133133
{
134134
$this->param->nullable = true;
135135
$this->param->map = true;
136-
$this->assertEquals([new All([
137-
'constraints' => [],
138-
])], $this->param->getConstraints());
136+
$this->assertEquals([], $this->param->getConstraints());
139137
}
140138
}

Tests/Controller/Annotations/FileParamTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,40 +75,40 @@ public function testComplexRequirements()
7575
public function testFileRequirements()
7676
{
7777
$this->param->nullable = true;
78-
$this->param->requirements = $requirements = ['mimeTypes' => 'application/json'];
78+
$this->param->requirements = ['mimeTypes' => 'application/json'];
7979
$this->assertEquals([
80-
new File($requirements),
80+
new File(null, null, null, 'application/json'),
8181
], $this->param->getConstraints());
8282
}
8383

8484
public function testImageRequirements()
8585
{
8686
$this->param->image = true;
87-
$this->param->requirements = $requirements = ['mimeTypes' => 'image/gif'];
87+
$this->param->requirements = ['mimeTypes' => ['image/*']];
8888
$this->assertEquals([
8989
new NotNull(),
90-
new Image($requirements),
90+
new Image(null, null, null, ['image/*']),
9191
], $this->param->getConstraints());
9292
}
9393

9494
public function testImageConstraintsTransformWhenParamIsAnArray()
9595
{
9696
$this->param->image = true;
9797
$this->param->map = true;
98-
$this->param->requirements = $requirements = ['mimeTypes' => 'image/gif'];
98+
$this->param->requirements = ['mimeTypes' => ['image/*']];
9999
$this->assertEquals([new All([
100100
new NotNull(),
101-
new Image($requirements),
101+
new Image(null, null, null, ['image/*']),
102102
])], $this->param->getConstraints());
103103
}
104104

105105
public function testFileConstraintsWhenParamIsAnArray()
106106
{
107107
$this->param->map = true;
108-
$this->param->requirements = $requirements = ['mimeTypes' => 'application/pdf'];
108+
$this->param->requirements = ['mimeTypes' => 'application/pdf'];
109109
$this->assertEquals([new All([
110110
new NotNull(),
111-
new File($requirements),
111+
new File(null, null, null, 'application/pdf'),
112112
])], $this->param->getConstraints());
113113
}
114114
}

Tests/Controller/Annotations/RouteTest.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,16 @@ public function testCanInstantiate()
4141
$condition
4242
);
4343

44-
$this->assertEquals($path, $route->getPath());
45-
$this->assertEquals($name, $route->getName());
46-
$this->assertEquals($requirements, $route->getRequirements());
47-
$this->assertEquals($options, $route->getOptions());
48-
$this->assertEquals($defaults, $route->getDefaults());
49-
$this->assertEquals($host, $route->getHost());
50-
$this->assertEquals($methods, $route->getMethods());
51-
$this->assertEquals($schemes, $route->getSchemes());
52-
$this->assertEquals($condition, $route->getCondition());
44+
$isPublic = isset($route->methods);
45+
46+
$this->assertEquals($path, $isPublic ? $route->path : $route->getPath());
47+
$this->assertEquals($name, $isPublic ? $route->name : $route->getName());
48+
$this->assertEquals($requirements, $isPublic ? $route->requirements : $route->getRequirements());
49+
$this->assertEquals($options, $isPublic ? $route->options : $route->getOptions());
50+
$this->assertEquals($defaults, $isPublic ? $route->defaults : $route->getDefaults());
51+
$this->assertEquals($host, $isPublic ? $route->host : $route->getHost());
52+
$this->assertEquals($methods, $isPublic ? $route->methods : $route->getMethods());
53+
$this->assertEquals($schemes, $isPublic ? $route->schemes : $route->getSchemes());
54+
$this->assertEquals($condition, $isPublic ? $route->condition : $route->getCondition());
5355
}
5456
}

Tests/Fixtures/Annotations/IdenticalToRequestParam.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace FOS\RestBundle\Tests\Fixtures\Annotations;
1313

14+
use Composer\InstalledVersions;
1415
use FOS\RestBundle\Controller\Annotations\RequestParam;
1516
use Symfony\Component\Validator\Constraints\IdenticalTo;
1617

@@ -41,6 +42,21 @@ public function __construct(
4142
bool $nullable = false,
4243
bool $allowBlank = true
4344
) {
44-
parent::__construct($name, $key, null !== $identicalTo ? new IdenticalTo($identicalTo) : null, $default, $description, $incompatibles, $strict, $map, $nullable, $allowBlank);
45+
$validatorSupportsArrayConfig = true;
46+
if (class_exists(InstalledVersions::class)) {
47+
$validatorVersion = InstalledVersions::getVersion('symfony/validator');
48+
49+
$validatorSupportsArrayConfig = version_compare($validatorVersion, '8.0', '<');
50+
}
51+
52+
if (null === $identicalTo) {
53+
$options = null;
54+
} elseif ($validatorSupportsArrayConfig) {
55+
$options = $identicalTo;
56+
} else {
57+
$options = $identicalTo['value'];
58+
}
59+
60+
parent::__construct($name, $key, null !== $options ? new IdenticalTo($options) : null, $default, $description, $incompatibles, $strict, $map, $nullable, $allowBlank);
4561
}
4662
}

0 commit comments

Comments
 (0)