Skip to content

Commit f8e3af2

Browse files
authored
fix(service-alias): Service alias doesn't work with :: (#3)
1 parent 3b16709 commit f8e3af2

2 files changed

Lines changed: 24 additions & 9 deletions

File tree

src/EventListener/ReadRateLimitAnnotationListener.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,16 @@ public function onKernelController(ControllerEvent $event): void
4949
if (null === $controllerAttribute || !is_string($controllerAttribute)) {
5050
return;
5151
}
52-
$controllerAttributeParts = explode('::', $controllerAttribute);
52+
// services alias can be used with 'service.alias:functionName' or 'service.alias::functionName'
53+
$controllerAttributeParts = explode(':', str_replace('::', ':', $controllerAttribute));
5354
$controllerName = $controllerAttributeParts[0] ?? '';
5455
$methodName = $controllerAttributeParts[1] ?? null;
5556

5657
if (!class_exists($controllerName)) {
5758
// If controller attribute is an alias instead of a class name
58-
$serviceIdAttributeParts = explode(':', $controllerName);
59-
if (null === ($controllerName = $this->container->get($serviceIdAttributeParts[0]))) {
59+
if (null === ($controllerName = $this->container->get($controllerAttributeParts[0]))) {
6060
throw new \InvalidArgumentException('Parameter _controller from request : "'.$controllerAttribute.'" do not contains a valid class name');
6161
}
62-
$methodName = $serviceIdAttributeParts[1] ?? null;
6362
}
6463
$reflection = new \ReflectionClass($controllerName);
6564
$annotation = $this->annotationReader->getMethodAnnotation($reflection->getMethod((string) ($methodName ?? '__invoke')), RateLimitAnnotation::class);

tests/EventListener/ReadRateLimitAnnotationListenerTest.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,18 @@ public function testItDoesNotSetRateLimitIfNoAnnotationProvided(): void
6161
$this->assertFalse($event->getRequest()->attributes->has('_rate_limit'));
6262
}
6363

64-
public function testItSetRateLimitIfNoAnnotationProvidedAndServiceAliasIsUsed(): void
64+
/**
65+
* @dataProvider servicesAliasDataProvider
66+
*/
67+
public function testItSetRateLimitIfNoAnnotationProvidedAndServiceAliasIsUsed(string $serviceAlias): void
6568
{
6669
$this->createReadRateLimitAnnotationListerner();
6770

6871
$this->container->expects($this->once())
6972
->method('get')
7073
->willReturn(new FakeInvokableClassWithDefaultRateLimit());
7174

72-
$event = $this->createEvent(null, true);
75+
$event = $this->createEvent(null, $serviceAlias);
7376

7477
$this->annotationReader->expects($this->once())->method('getMethodAnnotation')->willReturn(null);
7578

@@ -80,6 +83,19 @@ public function testItSetRateLimitIfNoAnnotationProvidedAndServiceAliasIsUsed():
8083
$this->assertFalse($event->getRequest()->attributes->has('_rate_limit'));
8184
}
8285

86+
/**
87+
* @return \Generator<array<string>>
88+
*/
89+
public function servicesAliasDataProvider(): \Generator
90+
{
91+
yield 'service alias with :' => [
92+
'fake.invokable.class:__invoke',
93+
];
94+
yield 'service alias with ::' => [
95+
'fake.invokable.class::__invoke',
96+
];
97+
}
98+
8399
public function testItSetsRateLimitIfAnnotationProvidedWithDefaultValue(): void
84100
{
85101
$this->createReadRateLimitAnnotationListerner();
@@ -159,15 +175,15 @@ public function rateLimitConfigurationDataProvider(): \Generator
159175
];
160176
}
161177

162-
protected function createEvent(Request $request = null, bool $useAlias = false): ControllerEvent
178+
protected function createEvent(Request $request = null, string $serviceId = null): ControllerEvent
163179
{
164180
$request = $request ?? new Request();
165-
$request->attributes->set('_controller', $useAlias ? 'fake.invokable.class:__invoke' : FakeInvokableClassWithDefaultRateLimit::class);
181+
$request->attributes->set('_controller', $serviceId ?? FakeInvokableClassWithDefaultRateLimit::class);
166182

167183
return new ControllerEvent(
168184
$this->createMock(HttpKernelInterface::class),
169185
new FakeInvokableClassWithDefaultRateLimit(),
170-
$request ?? new Request(),
186+
$request,
171187
HttpKernelInterface::MASTER_REQUEST
172188
);
173189
}

0 commit comments

Comments
 (0)