Skip to content

Commit 2a6bcce

Browse files
committed
fix: extract controller and method from callback
1 parent 74be373 commit 2a6bcce

3 files changed

Lines changed: 35 additions & 10 deletions

File tree

src/Aspect/SqlCommenterAspect.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,17 @@ private function appendSqlComments(string $query, string $dbDriver): string
7474
$comments['route'] = $request->getUri()->getPath();
7575
}
7676

77-
$dispatched = $request->getAttribute(Dispatched::class);
78-
$callback = $dispatched->handler->callback;
77+
if ($this->switchManager->isEnable('controller') || $this->switchManager->isEnable('action')) {
78+
$dispatched = $request->getAttribute(Dispatched::class);
79+
$parts = Utils::extractCallback($dispatched->handler->callback);
7980

80-
if ($this->switchManager->isEnable('controller') && is_array($callback)) {
81-
$mapController = explode('/', $callback[0]);
82-
$comments['controller'] = end($mapController);
83-
}
81+
if ($this->switchManager->isEnable('controller')) {
82+
$comments['controller'] = $parts[0];
83+
}
8484

85-
if ($this->switchManager->isEnable('action') && is_array($callback)) {
86-
$comments['action'] = $callback[1] ?? '';
85+
if ($this->switchManager->isEnable('action')) {
86+
$comments['action'] = $parts[1];
87+
}
8788
}
8889
}
8990

src/Utils.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,30 @@ public static function formatComments(array $comments): string
3636
) . '*/';
3737
}
3838

39+
/**
40+
* Extracts the controller name and method from a callback.
41+
*
42+
* @param mixed $callback the callback can be a string 'Namespace\Class@method' or an array [Class, 'method']
43+
* @return array returns an array with the controller name and method
44+
*/
45+
public static function extractCallback(mixed $callback): array
46+
{
47+
switch (gettype($callback)) {
48+
case 'string':
49+
$parts = explode('@', $callback);
50+
$method = $parts[1] ?? '';
51+
52+
$controllerNameParts = explode('\\', $parts[0]);
53+
$controllerName = end($controllerNameParts);
54+
55+
return [$controllerName, $method];
56+
case 'array':
57+
return [basename($callback[0], '.php'), $callback[1]];
58+
default:
59+
return ['', ''];
60+
}
61+
}
62+
3963
/**
4064
* Custom URL encoding to escape '%' characters for SQL compatibility.
4165
*/

tests/Aspect/SqlCommenterAspectTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testProcess(): void
5050
->willReturnOnConsecutiveCalls(true, 'TestApp');
5151

5252
$mockedSwitchManager = $this->createMock(SwitchManager::class);
53-
$mockedSwitchManager->expects($this->exactly(7))
53+
$mockedSwitchManager->expects($this->exactly(8))
5454
->method('isEnable')
5555
->willReturn(true);
5656

@@ -118,7 +118,7 @@ public function testProcess(): void
118118
$this->assertStringContainsString("application='TestApp'", $query);
119119
$this->assertStringContainsString("db_driver='mysql'", $query);
120120
$this->assertStringContainsString("route='%%2Fv1%%2Fadmin%%2Findex'", $query);
121-
$this->assertStringContainsString("controller='IndexController.php'", $query);
121+
$this->assertStringContainsString("controller='IndexController'", $query);
122122
$this->assertStringContainsString("action='index'", $query);
123123
$this->assertStringContainsString("traceparent='00-5bd66ef5095369c7b0d1f8f4bd33716a-c532cb4098ac3dd2-01'", $query);
124124
$this->assertTrue($result);

0 commit comments

Comments
 (0)