Skip to content

Commit 9718d95

Browse files
committed
refactor: makes improvements to controller mapping
1 parent 2a6bcce commit 9718d95

4 files changed

Lines changed: 21 additions & 11 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
up:
22
docker compose up
33
down:
4-
docker compose down -v
4+
docker compose down
55
php:
66
docker exec -it hyperf-sqlcommenter-app bash
77
check:

src/Aspect/SqlCommenterAspect.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,19 @@ private function appendSqlComments(string $query, string $dbDriver): string
7575
}
7676

7777
if ($this->switchManager->isEnable('controller') || $this->switchManager->isEnable('action')) {
78+
/** @var null|Dispatched $dispatched */
7879
$dispatched = $request->getAttribute(Dispatched::class);
79-
$parts = Utils::extractCallback($dispatched->handler->callback);
8080

81-
if ($this->switchManager->isEnable('controller')) {
82-
$comments['controller'] = $parts[0];
83-
}
81+
if ($dispatched && $dispatched->isFound()) {
82+
$parts = Utils::extractCallback($dispatched->handler?->callback);
83+
84+
if ($this->switchManager->isEnable('controller')) {
85+
$comments['controller'] = $parts[0];
86+
}
8487

85-
if ($this->switchManager->isEnable('action')) {
86-
$comments['action'] = $parts[1];
88+
if ($this->switchManager->isEnable('action')) {
89+
$comments['action'] = $parts[1];
90+
}
8791
}
8892
}
8993
}

src/Utils.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,22 @@ public static function extractCallback(mixed $callback): array
4747
switch (gettype($callback)) {
4848
case 'string':
4949
$parts = explode('@', $callback);
50-
$method = $parts[1] ?? '';
50+
$method = $parts[1] ?? '__invoke';
5151

5252
$controllerNameParts = explode('\\', $parts[0]);
5353
$controllerName = end($controllerNameParts);
5454

5555
return [$controllerName, $method];
5656
case 'array':
57-
return [basename($callback[0], '.php'), $callback[1]];
57+
$method = $callback[1] ?? '__invoke';
58+
59+
$controllerNameParts = explode('\\', $callback[0]);
60+
$controllerName = end($controllerNameParts);
61+
$controllerName = str_replace('.php', '', $controllerName);
62+
63+
return [$controllerName, $method];
5864
default:
59-
return ['', ''];
65+
return ['', 'callable'];
6066
}
6167
}
6268

tests/Aspect/SqlCommenterAspectTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function testProcess(): void
6464
->with(Dispatched::class)
6565
->willReturn(new Dispatched([
6666
Dispatcher::FOUND,
67-
new Handler(['app/Controller/IndexController.php', 'index'], ''),
67+
new Handler('App\Controller\IndexController@index', ''),
6868
[],
6969
]));
7070

0 commit comments

Comments
 (0)