Introduced annotation NamedArgumentConstructor#391
Conversation
916538e to
5029bc3
Compare
|
Of course. I've added a piece of documentation. 😃 |
|
Just realized we could simplify this by making the marker interface a boolean on |
|
I can give it a try. The problem that I see with that approach is that the |
|
Implemented the property as #394. Both approaches would work for me. I'll let you decide. :-) |
Sorry for the dumb questions, I am unfamiliar with the package… what do you mean by "loaded" via |
So was I until I started working on this patch. 😅
Sure, here you go:
In order to enable a class to be used as annotation, you have to annotate it with the /**
* @Annotation
*/
class Annotation {}In other words: The In #394 I had to add the |
|
With this solution, what happens when we annotate a class with |
|
I assume its the same problem with target annotation, it does nothing. Not something we need to care for anymore |
The line of code I've linked earlier is the one that determines if a class is a valid annotation. If |
|
I think this approach here is better then, i didn't realize |
|
Could we also introduce a way to indicate the |
use Attribute;
use Doctrine\Common\Annotations\NamedArgumentConstructorAnnotation;
/**
* @Annotation
* @Target({"CLASS"})
*/
#[Attribute(Attribute::TARGET_CLASS)]
class Foo implements NamedArgumentConstructorAnnotation
{
/**
* The field name.
*
* @var string
*/
public ?string $name;
public function __construct(?string $name)
{
$this->name = $name;
}
}The supported syntax are: #[Foo("ok")]
class TestClass{}
#[Foo(name: "ok")]
class TestClass{}
/** @Foo(name="ok") */
class TestClass{}but this one is not supported as it assume a /** @Foo("not ok") */
class TestClass{}We could add an attribute |
|
@Vincz Let me tinker with that idea a bit. We would need something like that for Symfony as well. But I think, we can solve this in a follow-up PR. |
|
@derrabus It is already used internally by the Annotation\Attribute::class => [
'is_annotation' => true,
'has_constructor' => false,
'targets_literal' => 'ANNOTATION_ANNOTATION',
'targets' => Target::TARGET_ANNOTATION,
'default_property' => 'name',
...
]As a workaround, we can add a property 'value' if it is not defined in the constructor and use it's value on another property but it doesn't work with required properties. I could submit a PR but I don't know where would be the best place to add this option and I wonder if it makes sense to have this feature outside the |
|
Hey guys. Any update about this? Is there anything I can do to help? |
|
Sorry, I've been a bit busy this week. I hope I can work on this on the weekend. |
|
This PR should not extend in scope for now, lets tackle this as an extra issue |
|
Ok, I created a new issue here #396 in order to handle this feature. |
5029bc3 to
0846c62
Compare
|
Thanks @derrabus ! |
|
@derrabus This is game-changer! Thank you for this feature 👏 🙇 Fun fact: I always assumed annotations work this way by default :D |
|
Thanks a lot. 😃 |
Fixes #379
This PR enables annotations to provide a constructor to be called with named arguments without requiring to implement a marker interface.