Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3eb26c1

Browse filesBrowse files
committed
feature #44615 [Routing] Support the "attribute" type (alias of "annotation") in annotation loaders (fancyweb)
This PR was merged into the 6.1 branch. Discussion ---------- [Routing] Support the "attribute" type (alias of "annotation") in annotation loaders | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Minor DX feature. I migrated several projects to PHP 8 and they now use the `Route` class as an attribute. They don't require `doctrine/annotations` anymore. There is no PHPDoc annotations left in the code. For example, I declare the routes like this: ```php <?php declare(strict_types=1); use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; return static function (RoutingConfigurator $routes): void { $routes ->import('../../src/Controller', 'annotation') ->namePrefix('app_'); }; ``` I'd like to use `attribute` as the type instead of `annotation` now because it makes more sense. Also, it's easier to explain it to newcomers. Commits ------- 23dc924 [Routing] Support the "attribute" type (alias of "annotation") in annotation loaders
2 parents a4241f3 + 23dc924 commit 3eb26c1
Copy full SHA for 3eb26c1

8 files changed

+13
-3
lines changed

‎src/Symfony/Component/Config/Tests/Exception/LoaderLoadExceptionTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Config/Tests/Exception/LoaderLoadExceptionTest.php
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ public function testMessageCannotLoadResourceWithAnnotationType()
3434
$this->assertEquals('Cannot load resource "resource". Make sure there is a loader supporting the "annotation" type.', $exception->getMessage());
3535
}
3636

37+
public function testMessageCannotLoadResourceWithAttributeType()
38+
{
39+
$exception = new LoaderLoadException('resource', null, 0, null, 'attribute');
40+
$this->assertEquals('Cannot load resource "resource". Make sure there is a loader supporting the "attribute" type.', $exception->getMessage());
41+
}
42+
3743
public function testMessageCannotImportResourceFromSource()
3844
{
3945
$exception = new LoaderLoadException('resource', 'sourceResource');

‎src/Symfony/Component/Routing/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Add `getMissingParameters` and `getRouteName` methods on `MissingMandatoryParametersException`
88
* Allow using UTF-8 parameter names
9+
* Support the `attribute` type (alias of `annotation`) in annotation loaders
910

1011
5.3
1112
---

‎src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ protected function addRoute(RouteCollection $collection, object $annot, array $g
237237
*/
238238
public function supports(mixed $resource, string $type = null): bool
239239
{
240-
return \is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource) && (!$type || 'annotation' === $type);
240+
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));
241241
}
242242

243243
/**

‎src/Symfony/Component/Routing/Loader/AnnotationDirectoryLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Loader/AnnotationDirectoryLoader.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function (\SplFileInfo $current) {
6969
*/
7070
public function supports(mixed $resource, string $type = null): bool
7171
{
72-
if ('annotation' === $type) {
72+
if (\in_array($type, ['annotation', 'attribute'], true)) {
7373
return true;
7474
}
7575

‎src/Symfony/Component/Routing/Loader/AnnotationFileLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Loader/AnnotationFileLoader.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function load(mixed $file, string $type = null): ?RouteCollection
6767
*/
6868
public function supports(mixed $resource, string $type = null): bool
6969
{
70-
return \is_string($resource) && 'php' === pathinfo($resource, \PATHINFO_EXTENSION) && (!$type || 'annotation' === $type);
70+
return \is_string($resource) && 'php' === pathinfo($resource, \PATHINFO_EXTENSION) && (!$type || \in_array($type, ['annotation', 'attribute'], true));
7171
}
7272

7373
/**

‎src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function provideTestSupportsChecksResource()
4646
public function testSupportsChecksTypeIfSpecified()
4747
{
4848
$this->assertTrue($this->loader->supports('class', 'annotation'), '->supports() checks the resource type if specified');
49+
$this->assertTrue($this->loader->supports('class', 'attribute'), '->supports() checks the resource type if specified');
4950
$this->assertFalse($this->loader->supports('class', 'foo'), '->supports() checks the resource type if specified');
5051
}
5152

‎src/Symfony/Component/Routing/Tests/Loader/AnnotationDirectoryLoaderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Loader/AnnotationDirectoryLoaderTest.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public function testSupports()
7878
$this->assertFalse($this->loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
7979

8080
$this->assertTrue($this->loader->supports($fixturesDir, 'annotation'), '->supports() checks the resource type if specified');
81+
$this->assertTrue($this->loader->supports($fixturesDir, 'attribute'), '->supports() checks the resource type if specified');
8182
$this->assertFalse($this->loader->supports($fixturesDir, 'foo'), '->supports() checks the resource type if specified');
8283
}
8384

‎src/Symfony/Component/Routing/Tests/Loader/AnnotationFileLoaderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Loader/AnnotationFileLoaderTest.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public function testSupports()
8383
$this->assertFalse($this->loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
8484

8585
$this->assertTrue($this->loader->supports($fixture, 'annotation'), '->supports() checks the resource type if specified');
86+
$this->assertTrue($this->loader->supports($fixture, 'attribute'), '->supports() checks the resource type if specified');
8687
$this->assertFalse($this->loader->supports($fixture, 'foo'), '->supports() checks the resource type if specified');
8788
}
8889

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.