Skip to content
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

Add PHP8 attributes for our existing service annotations #3619

Merged
merged 7 commits into from Nov 18, 2021

Conversation

aschempp
Copy link
Member

Replaces terminal42/service-annotation-bundle#6

The new naming follows the Symfony convention

These attribute names follow the pattern #[As...]. Symfony 5.3 provides #[AsCommand] to define a PHP class as a Symfony console command, #[AsEventListener] to define a PHP class as a Symfony event listener, etc.

@aschempp aschempp added this to the 4.13 milestone Oct 26, 2021
@aschempp aschempp requested review from m-vo and a team October 26, 2021 16:38
@aschempp aschempp self-assigned this Oct 26, 2021
@leofeyer
Copy link
Member

leofeyer commented Nov 9, 2021

Can you give an example of how to use the new feature?

@aschempp
Copy link
Member Author

aschempp commented Nov 11, 2021

// register hook
#[AsHook("outputFrontendPage")]

// registering two hooks for different methods
#[AsHook("outputFrontendPage", method: "foo")]
#[AsHook("parseTemplate", method: "bar")]

// register content element
#[AsContentElement(category: "texts")]
#[AsContentElement("my_type", category: "texts")]

Using named arguments you can skip the optional arguments 😊
Unfortunately, Symfonys service attributes are not supported on methods, but the same can be done through the method argument.

@aschempp aschempp marked this pull request as ready for review November 11, 2021 06:32
@aschempp aschempp requested a review from a team November 11, 2021 06:32
Copy link
Member

@leofeyer leofeyer left a comment

Choose a reason for hiding this comment

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

Maybe I am missing something but the constructor arguments do not match the annotations in the ServiceAnnotation namespace, do they? E. g.

// AsContentElement
string $type = null,
string $category,
string $template = null,
string $method = null,
string $renderer = null,
array ...$attributes
// ContentElement
@Attributes({
    @Attribute("value", type = "string"),
    @Attribute("category", required = true, type = "string"),
    @Attribute("template", type = "string"),
    @Attribute("renderer", type = "string"),
    @Attribute("attributes", type = "array"),
})

Is this correct?


Edit: As discussed with @aschempp, this is correct. The value attribute corresponds to the $type property and the $method property is used to assign the attribute to a method, because

Unfortunately, Symfonys service attributes are not supported on methods, but the same can be done through the method argument.

@leofeyer leofeyer changed the title Added PHP8 attributes for our existing service annotations Add PHP8 attributes for our existing service annotations Nov 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants