Skip to content

Commit 37d5bc1

Browse files
[FrameworkBundle][Routing] Remove remaining deprecations
1 parent f362aa4 commit 37d5bc1

7 files changed

+45
-196
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ CHANGELOG
66

77
* Add argument `$routeParameters` to `UrlMatcher::handleRouteRequirements()`
88
* Remove Doctrine annotations support in favor of native attributes
9-
* Change the constructor signature of `AnnotationClassLoader` to `__construct(?string $env = null)`, passing an annotation reader as first argument is not supported anymore
9+
* Remove `AnnotationClassLoader`, use `AttributeClassLoader` instead
10+
* Remove `AnnotationDirectoryLoader`, use `AttributeDirectoryLoader` instead
11+
* Remove `AnnotationFileLoader`, use `AttributeFileLoader` instead
1012

1113
6.4
1214
---

Loader/AnnotationDirectoryLoader.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

Loader/AttributeClassLoader.php

Lines changed: 39 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\Routing\Loader;
1313

14-
use Doctrine\Common\Annotations\Reader;
1514
use Symfony\Component\Config\Loader\LoaderInterface;
1615
use Symfony\Component\Config\Loader\LoaderResolverInterface;
1716
use Symfony\Component\Config\Resource\FileResource;
@@ -53,57 +52,18 @@
5352
*/
5453
abstract class AttributeClassLoader implements LoaderInterface
5554
{
56-
/**
57-
* @var Reader|null
58-
*
59-
* @deprecated in Symfony 6.4, this property will be removed in Symfony 7.
60-
*/
61-
protected $reader;
62-
63-
/**
64-
* @var string|null
65-
*/
66-
protected $env;
67-
68-
/**
69-
* @var string
70-
*/
71-
protected $routeAnnotationClass = RouteAnnotation::class;
55+
protected string $routeAnnotationClass = RouteAnnotation::class;
56+
protected int $defaultRouteIndex = 0;
7257

73-
/**
74-
* @var int
75-
*/
76-
protected $defaultRouteIndex = 0;
77-
78-
private bool $hasDeprecatedAnnotations = false;
79-
80-
/**
81-
* @param string|null $env
82-
*/
83-
public function __construct($env = null)
84-
{
85-
if ($env instanceof Reader || null === $env && \func_num_args() > 1 && null !== func_get_arg(1)) {
86-
trigger_deprecation('symfony/routing', '6.4', 'Passing an instance of "%s" as first and the environment as second argument to "%s" is deprecated. Pass the environment as first argument instead.', Reader::class, __METHOD__);
87-
88-
$this->reader = $env;
89-
$env = \func_num_args() > 1 ? func_get_arg(1) : null;
90-
}
91-
92-
if (\is_string($env) || null === $env) {
93-
$this->env = $env;
94-
} elseif ($env instanceof \Stringable || \is_scalar($env)) {
95-
$this->env = (string) $env;
96-
} else {
97-
throw new \TypeError(__METHOD__.sprintf(': Parameter $env was expected to be a string or null, "%s" given.', get_debug_type($env)));
98-
}
58+
public function __construct(
59+
protected readonly ?string $env = null,
60+
) {
9961
}
10062

10163
/**
10264
* Sets the annotation class to read route properties from.
103-
*
104-
* @return void
10565
*/
106-
public function setRouteAnnotationClass(string $class)
66+
public function setRouteAnnotationClass(string $class): void
10767
{
10868
$this->routeAnnotationClass = $class;
10969
}
@@ -124,59 +84,47 @@ public function load(mixed $class, string $type = null): RouteCollection
12484
throw new \InvalidArgumentException(sprintf('Annotations from class "%s" cannot be read as it is abstract.', $class->getName()));
12585
}
12686

127-
$this->hasDeprecatedAnnotations = false;
128-
129-
try {
130-
$globals = $this->getGlobals($class);
131-
$collection = new RouteCollection();
132-
$collection->addResource(new FileResource($class->getFileName()));
133-
if ($globals['env'] && $this->env !== $globals['env']) {
134-
return $collection;
135-
}
136-
$fqcnAlias = false;
137-
foreach ($class->getMethods() as $method) {
138-
$this->defaultRouteIndex = 0;
139-
$routeNamesBefore = array_keys($collection->all());
140-
foreach ($this->getAnnotations($method) as $annot) {
141-
$this->addRoute($collection, $annot, $globals, $class, $method);
142-
if ('__invoke' === $method->name) {
143-
$fqcnAlias = true;
144-
}
145-
}
146-
147-
if (1 === $collection->count() - \count($routeNamesBefore)) {
148-
$newRouteName = current(array_diff(array_keys($collection->all()), $routeNamesBefore));
149-
$collection->addAlias(sprintf('%s::%s', $class->name, $method->name), $newRouteName);
150-
}
151-
}
152-
if (0 === $collection->count() && $class->hasMethod('__invoke')) {
153-
$globals = $this->resetGlobals();
154-
foreach ($this->getAnnotations($class) as $annot) {
155-
$this->addRoute($collection, $annot, $globals, $class, $class->getMethod('__invoke'));
87+
$globals = $this->getGlobals($class);
88+
$collection = new RouteCollection();
89+
$collection->addResource(new FileResource($class->getFileName()));
90+
if ($globals['env'] && $this->env !== $globals['env']) {
91+
return $collection;
92+
}
93+
$fqcnAlias = false;
94+
foreach ($class->getMethods() as $method) {
95+
$this->defaultRouteIndex = 0;
96+
$routeNamesBefore = array_keys($collection->all());
97+
foreach ($this->getAnnotations($method) as $annot) {
98+
$this->addRoute($collection, $annot, $globals, $class, $method);
99+
if ('__invoke' === $method->name) {
156100
$fqcnAlias = true;
157101
}
158102
}
159-
if ($fqcnAlias && 1 === $collection->count()) {
160-
$collection->addAlias($class->name, $invokeRouteName = key($collection->all()));
161-
$collection->addAlias(sprintf('%s::__invoke', $class->name), $invokeRouteName);
162-
}
163103

164-
if ($this->hasDeprecatedAnnotations) {
165-
trigger_deprecation('symfony/routing', '6.4', 'Class "%s" uses Doctrine Annotations to configure routes, which is deprecated. Use PHP attributes instead.', $class->getName());
104+
if (1 === $collection->count() - \count($routeNamesBefore)) {
105+
$newRouteName = current(array_diff(array_keys($collection->all()), $routeNamesBefore));
106+
$collection->addAlias(sprintf('%s::%s', $class->name, $method->name), $newRouteName);
166107
}
167-
} finally {
168-
$this->hasDeprecatedAnnotations = false;
108+
}
109+
if (0 === $collection->count() && $class->hasMethod('__invoke')) {
110+
$globals = $this->resetGlobals();
111+
foreach ($this->getAnnotations($class) as $annot) {
112+
$this->addRoute($collection, $annot, $globals, $class, $class->getMethod('__invoke'));
113+
$fqcnAlias = true;
114+
}
115+
}
116+
if ($fqcnAlias && 1 === $collection->count()) {
117+
$collection->addAlias($class->name, $invokeRouteName = key($collection->all()));
118+
$collection->addAlias(sprintf('%s::__invoke', $class->name), $invokeRouteName);
169119
}
170120

171121
return $collection;
172122
}
173123

174124
/**
175125
* @param RouteAnnotation $annot or an object that exposes a similar interface
176-
*
177-
* @return void
178126
*/
179-
protected function addRoute(RouteCollection $collection, object $annot, array $globals, \ReflectionClass $class, \ReflectionMethod $method)
127+
protected function addRoute(RouteCollection $collection, object $annot, array $globals, \ReflectionClass $class, \ReflectionMethod $method): void
180128
{
181129
if ($annot->getEnv() && $annot->getEnv() !== $this->env) {
182130
return;
@@ -263,11 +211,7 @@ protected function addRoute(RouteCollection $collection, object $annot, array $g
263211

264212
public function supports(mixed $resource, string $type = null): bool
265213
{
266-
if ('annotation' === $type) {
267-
trigger_deprecation('symfony/routing', '6.4', 'The "annotation" route type is deprecated, use the "attribute" route type instead.');
268-
}
269-
270-
return \is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource) && (!$type || \in_array($type, ['annotation', 'attribute'], true));
214+
return \is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource) && (!$type || 'attribute' === $type);
271215
}
272216

273217
public function setResolver(LoaderResolverInterface $resolver): void
@@ -280,10 +224,8 @@ public function getResolver(): LoaderResolverInterface
280224

281225
/**
282226
* Gets the default route name for a class method.
283-
*
284-
* @return string
285227
*/
286-
protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method)
228+
protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method): string
287229
{
288230
$name = str_replace('\\', '_', $class->name).'_'.$method->name;
289231
$name = \function_exists('mb_strtolower') && preg_match('//u', $name) ? mb_strtolower($name, 'UTF-8') : strtolower($name);
@@ -298,19 +240,13 @@ protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMetho
298240
/**
299241
* @return array<string, mixed>
300242
*/
301-
protected function getGlobals(\ReflectionClass $class)
243+
protected function getGlobals(\ReflectionClass $class): array
302244
{
303245
$globals = $this->resetGlobals();
304246

305-
$annot = null;
306247
if ($attribute = $class->getAttributes($this->routeAnnotationClass, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null) {
307248
$annot = $attribute->newInstance();
308-
}
309-
if (!$annot && $annot = $this->reader?->getClassAnnotation($class, $this->routeAnnotationClass)) {
310-
$this->hasDeprecatedAnnotations = true;
311-
}
312249

313-
if ($annot) {
314250
if (null !== $annot->getName()) {
315251
$globals['name'] = $annot->getName();
316252
}
@@ -380,18 +316,12 @@ private function resetGlobals(): array
380316
];
381317
}
382318

383-
/**
384-
* @return Route
385-
*/
386-
protected function createRoute(string $path, array $defaults, array $requirements, array $options, ?string $host, array $schemes, array $methods, ?string $condition)
319+
protected function createRoute(string $path, array $defaults, array $requirements, array $options, ?string $host, array $schemes, array $methods, ?string $condition): Route
387320
{
388321
return new Route($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
389322
}
390323

391-
/**
392-
* @return void
393-
*/
394-
abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot);
324+
abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot): void;
395325

396326
/**
397327
* @return iterable<int, RouteAnnotation>
@@ -401,25 +331,5 @@ private function getAnnotations(\ReflectionClass|\ReflectionMethod $reflection):
401331
foreach ($reflection->getAttributes($this->routeAnnotationClass, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
402332
yield $attribute->newInstance();
403333
}
404-
405-
if (!$this->reader) {
406-
return;
407-
}
408-
409-
$annotations = $reflection instanceof \ReflectionClass
410-
? $this->reader->getClassAnnotations($reflection)
411-
: $this->reader->getMethodAnnotations($reflection);
412-
413-
foreach ($annotations as $annotation) {
414-
if ($annotation instanceof $this->routeAnnotationClass) {
415-
$this->hasDeprecatedAnnotations = true;
416-
417-
yield $annotation;
418-
}
419-
}
420334
}
421335
}
422-
423-
if (!class_exists(AnnotationClassLoader::class, false)) {
424-
class_alias(AttributeClassLoader::class, AnnotationClassLoader::class);
425-
}

Loader/AttributeDirectoryLoader.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,7 @@ public function supports(mixed $resource, string $type = null): bool
6767
return false;
6868
}
6969

70-
if (\in_array($type, ['annotation', 'attribute'], true)) {
71-
if ('annotation' === $type) {
72-
trigger_deprecation('symfony/routing', '6.4', 'The "annotation" route type is deprecated, use the "attribute" route type instead.');
73-
}
74-
70+
if ('attribute' === $type) {
7571
return true;
7672
}
7773

@@ -86,7 +82,3 @@ public function supports(mixed $resource, string $type = null): bool
8682
}
8783
}
8884
}
89-
90-
if (!class_exists(AnnotationDirectoryLoader::class, false)) {
91-
class_alias(AttributeDirectoryLoader::class, AnnotationDirectoryLoader::class);
92-
}

Loader/AttributeFileLoader.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*/
2626
class AttributeFileLoader extends FileLoader
2727
{
28-
protected $loader;
28+
protected AttributeClassLoader $loader;
2929

3030
public function __construct(FileLocatorInterface $locator, AttributeClassLoader $loader)
3131
{
@@ -65,11 +65,7 @@ public function load(mixed $file, string $type = null): ?RouteCollection
6565

6666
public function supports(mixed $resource, string $type = null): bool
6767
{
68-
if ('annotation' === $type) {
69-
trigger_deprecation('symfony/routing', '6.4', 'The "annotation" route type is deprecated, use the "attribute" route type instead.');
70-
}
71-
72-
return \is_string($resource) && 'php' === pathinfo($resource, \PATHINFO_EXTENSION) && (!$type || \in_array($type, ['annotation', 'attribute'], true));
68+
return \is_string($resource) && 'php' === pathinfo($resource, \PATHINFO_EXTENSION) && (!$type || 'attribute' === $type);
7369
}
7470

7571
/**
@@ -139,7 +135,3 @@ protected function findClass(string $file): string|false
139135
return false;
140136
}
141137
}
142-
143-
if (!class_exists(AnnotationFileLoader::class, false)) {
144-
class_alias(AttributeFileLoader::class, AnnotationFileLoader::class);
145-
}

Tests/Loader/AttributeDirectoryLoaderTest.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,6 @@ public function testSupports()
5959
$this->assertFalse($this->loader->supports($fixturesDir, 'foo'), '->supports() checks the resource type if specified');
6060
}
6161

62-
/**
63-
* @group legacy
64-
*/
65-
public function testSupportsAnnotations()
66-
{
67-
$fixturesDir = __DIR__.'/../Fixtures';
68-
69-
$this->expectDeprecation('Since symfony/routing 6.4: The "annotation" route type is deprecated, use the "attribute" route type instead.');
70-
$this->assertTrue($this->loader->supports($fixturesDir, 'annotation'), '->supports() checks the resource type if specified');
71-
}
72-
7362
public function testItSupportsAnyAttribute()
7463
{
7564
$this->assertTrue($this->loader->supports(__DIR__.'/../Fixtures/even-with-not-existing-folder', 'attribute'));

Tests/Loader/AttributeFileLoaderTest.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,6 @@ public function testSupports()
8484
$this->assertFalse($this->loader->supports($fixture, 'foo'), '->supports() checks the resource type if specified');
8585
}
8686

87-
/**
88-
* @group legacy
89-
*/
90-
public function testSupportsAnnotations()
91-
{
92-
$fixture = __DIR__.'/../Fixtures/annotated.php';
93-
94-
$this->expectDeprecation('Since symfony/routing 6.4: The "annotation" route type is deprecated, use the "attribute" route type instead.');
95-
$this->assertTrue($this->loader->supports($fixture, 'annotation'), '->supports() checks the resource type if specified');
96-
}
97-
9887
public function testLoadAttributesClassAfterComma()
9988
{
10089
self::assertCount(0, $this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamAfterCommaController.php'));

0 commit comments

Comments
 (0)