Skip to content

Commit aa4101b

Browse files
committed
implement event name references for "AsEventListener" attribute
1 parent c82050a commit aa4101b

File tree

5 files changed

+73
-5
lines changed

5 files changed

+73
-5
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/config/php/PhpConfigReferenceContributor.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,33 @@ public void registerReferenceProviders(@NotNull PsiReferenceRegistrar psiReferen
6262
.addCall("\\Symfony\\Component\\EventDispatcher\\EventDispatcherInterface", "dispatch")
6363
);
6464

65+
// #[AsEventListener(event: '<caret>')]
66+
psiReferenceRegistrar.registerReferenceProvider(
67+
PhpElementsUtil.getAttributeNamedArgumentStringLiteralPattern("\\Symfony\\Component\\EventDispatcher\\Attribute\\AsEventListener", "event"),
68+
new PsiReferenceProvider() {
69+
@Override
70+
public PsiReference @NotNull [] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
71+
if (!Symfony2ProjectComponent.isEnabled(element) || !(element instanceof StringLiteralExpression stringLiteralExpression)) {
72+
return new PsiReference[0];
73+
}
74+
75+
return new PsiReference[]{
76+
new EventDispatcherEventReference(element, stringLiteralExpression.getContents())
77+
};
78+
}
79+
80+
public boolean acceptsTarget(@NotNull PsiElement target) {
81+
return Symfony2ProjectComponent.isEnabled(target);
82+
}
83+
}
84+
);
85+
6586
psiReferenceRegistrar.registerReferenceProvider(
6687
PhpElementsUtil.getMethodWithFirstStringOrNamedArgumentPattern(),
6788
new PsiReferenceProvider() {
6889
@NotNull
6990
@Override
7091
public PsiReference @NotNull [] getReferencesByElement(@NotNull PsiElement psiElement, @NotNull ProcessingContext processingContext) {
71-
7292
if (!Symfony2ProjectComponent.isEnabled(psiElement)) {
7393
return new PsiReference[0];
7494
}

src/main/java/fr/adrienbrault/idea/symfony2plugin/util/PhpElementsUtil.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,14 @@ public static PsiElementPattern.Capture<PsiElement> getAttributeNamedArgumentStr
489489
PhpTokenTypes.STRING_LITERAL_SINGLE_QUOTE,
490490
PhpTokenTypes.STRING_LITERAL
491491
))
492-
.withParent(PlatformPatterns.psiElement(StringLiteralExpression.class)
492+
.withParent(getAttributeNamedArgumentStringLiteralPattern(clazz, namedArgument));
493+
}
494+
495+
/**
496+
* #[Security(foobar: "is_granted('POST_SHOW')")]
497+
*/
498+
public static PsiElementPattern.@NotNull Capture<StringLiteralExpression> getAttributeNamedArgumentStringLiteralPattern(@NotNull String clazz, @NotNull String namedArgument) {
499+
return PlatformPatterns.psiElement(StringLiteralExpression.class)
493500
.afterLeafSkipping(
494501
PlatformPatterns.psiElement(PsiWhiteSpace.class),
495502
PlatformPatterns.psiElement(PhpTokenTypes.opCOLON).afterLeafSkipping(
@@ -501,8 +508,7 @@ public static PsiElementPattern.Capture<PsiElement> getAttributeNamedArgumentStr
501508
.withParent(PlatformPatterns.psiElement(PhpAttribute.class)
502509
.with(new AttributeInstancePatternCondition(clazz))
503510
)
504-
)
505-
);
511+
);
506512
}
507513

508514
/**

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/config/php/PhpConfigReferenceContributorTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,19 @@ public void testTagReferences() {
3838
"foobar"
3939
);
4040
}
41+
42+
public void testEventNameCompletionForAsEventListener() {
43+
assertCompletionContains(PhpFileType.INSTANCE, "<?php\n" +
44+
"namespace App\\EventListener;\n" +
45+
"\n" +
46+
"use Symfony\\Component\\EventDispatcher\\Attribute\\AsEventListener;\n" +
47+
"\n" +
48+
"#[AsEventListener(event: '<caret>')]\n" +
49+
"final class MyMultiListener implements \\Symfony\\Component\\EventDispatcher\\EventSubscriberInterface\n" +
50+
"{\n" +
51+
"\n" +
52+
"}",
53+
"yaml_event_2"
54+
);
55+
}
4156
}

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/config/php/fixtures/classes.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,25 @@ interface MessageSubscriberInterface
2525
public static function getHandledMessages(): iterable;
2626
}
2727
}
28+
29+
namespace Symfony\Component\EventDispatcher
30+
{
31+
interface EventSubscriberInterface
32+
{
33+
}
34+
}
35+
36+
namespace Symfony\Component\EventDispatcher\Attribute
37+
{
38+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
39+
class AsEventListener
40+
{
41+
public function __construct(
42+
public ?string $event = null,
43+
public ?string $method = null,
44+
public int $priority = 0,
45+
public ?string $dispatcher = null,
46+
) {
47+
}
48+
}
49+
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
services:
2-
my_nice_service: ~
2+
my_nice_service: ~
3+
4+
foo_service:
5+
class: DateTime
6+
tags:
7+
- { name: "kernel.event_listener", event: "yaml_event_2", method: onKernelException }

0 commit comments

Comments
 (0)