Skip to content

Commit c8498d8

Browse files
committed
GetHooksTable is not fully cached to avoid hooks:status to appear bad
1 parent e78f622 commit c8498d8

File tree

2 files changed

+42
-50
lines changed

2 files changed

+42
-50
lines changed

src/Console/Commands/HooksStatusCommand.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Console\Command;
66
use QuixLabs\LaravelHookSystem\Facades\HookManager;
7+
use QuixLabs\LaravelHookSystem\Hook;
78
use QuixLabs\LaravelHookSystem\Hooks\GetHooksTable;
89
use QuixLabs\LaravelHookSystem\Utils\CommandTable;
910

@@ -19,7 +20,27 @@ public function handle(): int
1920
$this->components->warn('Hooks are actually cached!');
2021
}
2122

22-
$rows = [];
23+
$hooks = collect(HookManager::getHooks())
24+
->mapWithKeys(fn (string|Hook $hook) => [$hook => HookManager::getInterceptorsForHook($hook)])->toArray();
25+
$rows = collect($hooks)->map(function (array $interceptors, string $hookClass) {
26+
$callables = collect($interceptors)
27+
->map(fn (array $callables, int $priority) => implode('<br/>', array_map(
28+
fn (callable $callable) => static::_callableToString($callable), $callables)
29+
))->join('<br/>');
30+
31+
$priorities = collect($interceptors)
32+
->map(fn (array $callables, int $priority) => implode('<br/>', array_fill(0, count($callables), $priority)))
33+
->join('<br/>');
34+
35+
return [
36+
'Hook' => $hookClass,
37+
'Interceptors' => $callables,
38+
'Priority' => $priorities,
39+
'Fully Cacheable' => HookManager::isFullyCacheable($hookClass) ? 'YES' : 'NO',
40+
'Fully Cached' => HookManager::isFullyCached($hookClass) ? 'YES' : 'NO',
41+
];
42+
})->toArray();
43+
2344
GetHooksTable::send($rows);
2445
if (count($rows) > 0) {
2546
$this->output->writeln(CommandTable::asString($rows));
@@ -29,4 +50,23 @@ public function handle(): int
2950

3051
return self::SUCCESS;
3152
}
53+
54+
public static function _callableToString(callable $callable): string
55+
{
56+
if (is_array($callable)) {
57+
$class = is_object($callable[0]) ? get_class($callable[0]) : $callable[0];
58+
$method = $callable[1];
59+
} elseif (is_string($callable) && str_contains($callable, '::')) {
60+
[$class, $method] = explode('::', $callable);
61+
} else {
62+
$class = get_class($callable);
63+
$method = '__invoke';
64+
}
65+
66+
$reflection = new \ReflectionMethod($class, $method);
67+
$line = $reflection->getStartLine();
68+
69+
return sprintf('%s@%s:%d', $class, $method, $line);
70+
71+
}
3272
}

src/Hooks/GetHooksTable.php

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,11 @@
22

33
namespace QuixLabs\LaravelHookSystem\Hooks;
44

5-
use QuixLabs\LaravelHookSystem\Facades\HookManager;
65
use QuixLabs\LaravelHookSystem\Hook;
7-
use QuixLabs\LaravelHookSystem\Interfaces\FullyCacheable;
86

9-
class GetHooksTable extends Hook implements FullyCacheable
7+
class GetHooksTable extends Hook
108
{
119
public function __construct(public array &$rows)
1210
{
1311
}
14-
15-
public static function initialInstance(): static
16-
{
17-
$hooks = collect(HookManager::getHooks())
18-
->mapWithKeys(fn (string|Hook $hook) => [$hook => HookManager::getInterceptorsForHook($hook)])->toArray();
19-
$rows = collect($hooks)->map(function (array $interceptors, string $hookClass) {
20-
$callables = collect($interceptors)
21-
->map(fn (array $callables, int $priority) => implode('<br/>', array_map(
22-
fn (callable $callable) => static::_callableToString($callable), $callables)
23-
))->join('<br/>');
24-
25-
$priorities = collect($interceptors)
26-
->map(fn (array $callables, int $priority) => implode('<br/>', array_fill(0, count($callables), $priority)))
27-
->join('<br/>');
28-
29-
return [
30-
'Hook' => $hookClass,
31-
'Interceptors' => $callables,
32-
'Priority' => $priorities,
33-
'Fully Cacheable' => HookManager::isFullyCacheable($hookClass) ? 'YES' : 'NO',
34-
'Fully Cached' => HookManager::isFullyCached($hookClass) ? 'YES' : 'NO',
35-
];
36-
})->toArray();
37-
38-
/** @phpstan-ignore-next-line */
39-
return new static($rows);
40-
}
41-
42-
public static function _callableToString(callable $callable): string
43-
{
44-
if (is_array($callable)) {
45-
$class = is_object($callable[0]) ? get_class($callable[0]) : $callable[0];
46-
$method = $callable[1];
47-
} elseif (is_string($callable) && str_contains($callable, '::')) {
48-
[$class, $method] = explode('::', $callable);
49-
} else {
50-
$class = get_class($callable);
51-
$method = '__invoke';
52-
}
53-
54-
$reflection = new \ReflectionMethod($class, $method);
55-
$line = $reflection->getStartLine();
56-
57-
return sprintf('%s@%s:%d', $class, $method, $line);
58-
59-
}
6012
}

0 commit comments

Comments
 (0)