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

Skip to content

[Routing] Remove @final annotation from Route attribute #53606

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
Jan 23, 2024

Conversation

tjveldhuizen
Copy link
Contributor

@tjveldhuizen tjveldhuizen commented Jan 23, 2024

To be able to create a custom attribute containing application or section level defaults, the final annotation is removed from the Route attribute.

Fixes #53467

Q A
Branch? 6.4
Bug fix? no
New feature? no
Deprecations? no
Issues Fix #53467
License MIT

Removing the @final annotations makes it possible to create custom Route annotations containing pre set properties relevant for your application. Examples are a host name, defaults, requirements.

use Symfony\Component\Routing\Attribute\Route;

#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
class DefaultSectionRoute extends Route
{
    // Limited arguments for readability of the example
    public function __construct(
        array|string $path = null,
        ?string      $name = null,
        array        $defaults = [],
    ) {
        parent::__construct(
            "/{section<(foo|bar|baz)>}" . $path,
            $name,
            [],
            [],
            array_merge(['section' => 'foo'], $defaults),
        );
    }
}
use Symfony\Component\Routing\Attribute\Route;

#[\Attribute(\Attribute::TARGET_CLASS)]
class AdminRoute extends Route
{
    public function __construct(...$params) {
        if (isset($params[5])) {
            $params[5] = 'admin.example.com';
        } else {
            $params['host'] = "admin.example.com";
        }

        parent::__construct(...$params);
    }
}

@carsonbot
Copy link

Hey!

Thanks for your PR. You are targeting branch "7.1" but it seems your PR description refers to branch "7.1 for features".
Could you update the PR description or change target branch? This helps core maintainers a lot.

Cheers!

Carsonbot

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, AttributeClassLoader already uses \ReflectionAttribute::IS_INSTANCEOF so I'm not even sure why the class is final.
Just CS changes and good to merge on my side.

@tjveldhuizen
Copy link
Contributor Author

@nicolas-grekas Given you're not sure about the reason for this class to be final, would it be an option to target the 6.4 branch? (and see it as a bug fix instead of a new feature) Due to the nature of our project, we prefer not to update to Symfony 7 yet, but we would like to convert our annotations to attributes to remove a lot of lines from our deprecation logs. I can imagine this is the case for more developers.

@tjveldhuizen tjveldhuizen force-pushed the non-final-route-attribute branch from 5f493e8 to 4aa6904 Compare January 23, 2024 12:30
Copy link
Member

@yceruto yceruto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering the same thing a few days ago when I wanted to create a #[Post] route attribute instead of using #[Route(methods: 'POST')].

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jan 23, 2024

This was done in #52544 and I think this was overzealous yes, let's target 6.4 for this one.

@nicolas-grekas nicolas-grekas modified the milestones: 7.1, 5.4, 6.4 Jan 23, 2024
To be able to create a custom attribute containing application or section level defaults, the final annotation is removed from the Route attribute.

Fixes symfony#53467
@nicolas-grekas
Copy link
Member

Thank you @tjveldhuizen.

@nicolas-grekas nicolas-grekas merged commit 54f8938 into symfony:6.4 Jan 23, 2024
@stof
Copy link
Member

stof commented Jan 23, 2024

This was done in #52544 and I think this was overzealous yes

I totally agree it was not intended. My comment requesting to add @final annotations was meant to apply to classes where the final keyword was removed in that PR, not to all attributes

@tjveldhuizen
Copy link
Contributor Author

Thanks for the merge! I have not investigated, but are there more locations where it would be better to remove the @final, then?

@tjveldhuizen tjveldhuizen deleted the non-final-route-attribute branch January 24, 2024 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Routing] Allow extending Route attribute
5 participants