Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Routing] Support the "attribute" type (alias of "annotation") in annotation loaders #44615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public function testMessageCannotLoadResourceWithAnnotationType()
$this->assertEquals('Cannot load resource "resource". Make sure there is a loader supporting the "annotation" type.', $exception->getMessage());
}

public function testMessageCannotLoadResourceWithAttributeType()
{
$exception = new LoaderLoadException('resource', null, 0, null, 'attribute');
$this->assertEquals('Cannot load resource "resource". Make sure there is a loader supporting the "attribute" type.', $exception->getMessage());
}

public function testMessageCannotImportResourceFromSource()
{
$exception = new LoaderLoadException('resource', 'sourceResource');
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Routing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

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

5.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ protected function addRoute(RouteCollection $collection, object $annot, array $g
*/
public function supports(mixed $resource, string $type = null): bool
{
return \is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource) && (!$type || 'annotation' === $type);
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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function (\SplFileInfo $current) {
*/
public function supports(mixed $resource, string $type = null): bool
{
if ('annotation' === $type) {
if (\in_array($type, ['annotation', 'attribute'], true)) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function load(mixed $file, string $type = null): ?RouteCollection
*/
public function supports(mixed $resource, string $type = null): bool
{
return \is_string($resource) && 'php' === pathinfo($resource, \PATHINFO_EXTENSION) && (!$type || 'annotation' === $type);
return \is_string($resource) && 'php' === pathinfo($resource, \PATHINFO_EXTENSION) && (!$type || \in_array($type, ['annotation', 'attribute'], true));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function provideTestSupportsChecksResource()
public function testSupportsChecksTypeIfSpecified()
{
$this->assertTrue($this->loader->supports('class', 'annotation'), '->supports() checks the resource type if specified');
$this->assertTrue($this->loader->supports('class', 'attribute'), '->supports() checks the resource type if specified');
$this->assertFalse($this->loader->supports('class', 'foo'), '->supports() checks the resource type if specified');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function testSupports()
$this->assertFalse($this->loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function testSupports()
$this->assertFalse($this->loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');

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

Expand Down