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

Attribute support #6

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

Closed
wants to merge 8 commits into from
Closed

Attribute support #6

wants to merge 8 commits into from

Conversation

m-vo
Copy link

@m-vo m-vo commented Apr 25, 2021

This PR adds attribute support which basically allows doing this on PHP>=8:

- /**
-  * @ServiceTag("target_service", priority=123, bar="baz")
-  */
+ #[ServiceTag('target_service', priority: 123, bar: 'baz')]
class Foo
{
}

(This also adds phpunit for testing - not sure about your preferences herewith for this bundle. 🙂 )

@aschempp
Copy link
Member

very good! 👏

I'm not very experienced with attributes, wouldn't it make sense to use a separate class for attributes?

@m-vo
Copy link
Author

m-vo commented Apr 26, 2021

Both would work I guess. There was also a similar discussion in symfony/symfony#37474 for the Route attribute. There, now both strategies share a single class: https://github.com/symfony/symfony/blob/896f4a6eb6a986cc29e096dfe1f3f7042b03126f/src/Symfony/Component/Routing/Annotation/Route.php#L14-L26

I like that you can easily refactor your code (not even a new import needed) and the behavior stays identical. On the downside there is less type safety in the constructor. But IMHO this could/should be tweaked in AnnotationReader#getClassAnnotations() if this becomes a problem. 🤔 .

@aschempp aschempp requested a review from Toflar May 11, 2021 06:08
@aschempp aschempp requested a review from leofeyer May 11, 2021 06:35
src/Annotation/ServiceTag.php Show resolved Hide resolved
Copy link
Member

@Toflar Toflar left a comment

Choose a reason for hiding this comment

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

Looks great! Only a minor nitpick about file naming :)

phpunit.xml Outdated Show resolved Hide resolved
src/Annotation/ServiceTag.php Show resolved Hide resolved
@aschempp aschempp requested a review from ausi May 11, 2021 11:35
@ausi
Copy link
Collaborator

ausi commented May 11, 2021

Did you consider using named arguments instead of the array?

#[ServiceTag('target_service', priority: 123, bar: 'baz')]

Should work fine using the constructor signature:

__construct($name, ...$data)

@m-vo
Copy link
Author

m-vo commented May 11, 2021

Did you consider using named arguments instead of the array?

Didn't know that this works with arbitrary names. ☺️
I actually like this! Maybe it's even possible to specify type hints for these things in the future.

@Toflar @aschempp Wdyt?

@m-vo
Copy link
Author

m-vo commented May 11, 2021

Switched to named arguments in 19c6bf0.

I also renamed the first argument to $serviceName as it's less likely to clash with any of the named data arguments (in contrast to $value or $name).

@m-vo m-vo requested a review from leofeyer May 11, 2021 14:18
@aschempp
Copy link
Member

Very nice! I also didn't know about that 🙃

I also renamed the first argument to $serviceName as it's less likely to clash with any of the named data arguments (in contrast to $value or $name).

I would actually go with $value here, since that's the internal name the annotation reader uses as well, and having a value key on a tag wouldn't work with that either. WDYT?

@m-vo
Copy link
Author

m-vo commented May 11, 2021

IMHO the downside to $value is that it doesn't make it obvious what to enter when you're using auto completion in your IDE.

But I'm fine with both.

@aschempp
Copy link
Member

That's true. But $serviceName doesn't make that either? Wouldn't it be $tagName or just $name then (similar to YAML { name: kernel.event_listener, event: foobar }? Also consider that we have child annotations/attributes in Contao where the value has a different meaning (but they can use a different constructor, so that might not be relevant).

@aschempp
Copy link
Member

@Toflar just pointed me to https://symfony.com/blog/new-in-symfony-5-3-service-autoconfiguration-and-attributes
maybe that whole bundle will become obsolete?

@m-vo
Copy link
Author

m-vo commented May 12, 2021

maybe that whole bundle will become obsolete?

Not sure about this. If I get this correctly, you could get rid of the ServiceTagInterface but would still need the plumbing like so:

$container->registerAttributeForAutoconfiguration(
  ServiceTag::class, 
  static function (ChildDefinition $definition, ServiceTag $attribute): void {
    $definition->addTag($attribute->getName(), $attribute->getAttributes());
  }
);

I guess Contao (or any consumer) could implement their special attributes without this bundle but would then on the other hand need to require the Sf DI component in >= 5.3. Hmm.

(Speaking of Contao: We should consider following the As<Something> naming convention of Symfony. See https://github.com/symfony/symfony/pull/40556…)

@aschempp
Copy link
Member

yes that's what I mean. Instead of adding attributes trough this bundle, we could add support for attributes when Contao requires Symfony 5.3 (like in 4.12 or 4.13). If we agree we MUST have attribute support e.g. in 4.9, then we need to implement it in this bundle.

leofeyer pushed a commit to contao/contao that referenced this pull request Nov 18, 2021
Description
-----------

Replaces terminal42/service-annotation-bundle#6

The new naming follows the [Symfony convention](https://symfony.com/blog/new-in-symfony-5-3-service-autoconfiguration-and-attributes#autoconfigurable-attributes)

> 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.

Commits
-------

a5ea9c1 Added PHP8 attributes for our existing service annotations
44c5d82 Added missing page attribute
6bb9d1a Merge branch '4.x' into feature/php8-attributes
b9201cd Run the CS fixer
afd1d20 Update AsPage attribute to match Route attribute
2da4277 Fix the CS check
5e3e22a Fix the CS check
leofeyer pushed a commit to contao/core-bundle that referenced this pull request Nov 18, 2021
Description
-----------

Replaces terminal42/service-annotation-bundle#6

The new naming follows the [Symfony convention](https://symfony.com/blog/new-in-symfony-5-3-service-autoconfiguration-and-attributes#autoconfigurable-attributes)

> 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.

Commits
-------

a5ea9c15 Added PHP8 attributes for our existing service annotations
44c5d82c Added missing page attribute
6bb9d1ac Merge branch '4.x' into feature/php8-attributes
b9201cd2 Run the CS fixer
afd1d206 Update AsPage attribute to match Route attribute
2da42777 Fix the CS check
5e3e22a3 Fix the CS check
@aschempp
Copy link
Member

Attributes are supported natively by Symfony and Contao, therefore I'm closing this PR.

@aschempp aschempp closed this Aug 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.