Skip to content

Commit d8f2271

Browse files
authored
Merge pull request #6 from quix-labs/dev
Dev
2 parents e78f622 + ddf767c commit d8f2271

File tree

9 files changed

+50
-76
lines changed

9 files changed

+50
-76
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: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,9 @@
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
{
11-
public function __construct(public array &$rows)
12-
{
13-
}
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-
}
9+
public function __construct(public array &$rows) {}
6010
}

src/Utils/Intercept.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,5 @@ public function __construct(
1616
public ActionWhenMissing $actionWhenMissing = ActionWhenMissing::THROW_ERROR,
1717
public int $priority = 0,
1818
public bool $preventFullCache = false
19-
) {
20-
}
19+
) {}
2120
}

workbench/app/Hooks/GetArray.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,5 @@
66

77
class GetArray extends Hook
88
{
9-
public function __construct(public array &$array)
10-
{
11-
}
9+
public function __construct(public array &$array) {}
1210
}

workbench/app/Hooks/GetString.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,5 @@
66

77
class GetString extends Hook
88
{
9-
public function __construct(public string &$string)
10-
{
11-
}
9+
public function __construct(public string &$string) {}
1210
}

workbench/app/Hooks/GetStringFullyCacheable.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77

88
class GetStringFullyCacheable extends Hook implements FullyCacheable
99
{
10-
public function __construct(public string &$string)
11-
{
12-
}
10+
public function __construct(public string &$string) {}
1311

1412
public static function initialInstance(): static
1513
{

workbench/app/Interceptors/RegisterNonExistingHook.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,5 @@
88
class RegisterNonExistingHook
99
{
1010
#[Intercept(InvalidHook::class, ActionWhenMissing::REGISTER_HOOK, 0)]
11-
public static function handleNonExistingHook(InvalidHook $hook)
12-
{
13-
14-
}
11+
public static function handleNonExistingHook(InvalidHook $hook) {}
1512
}

workbench/app/Interceptors/SkipNonExistingHook.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,5 @@
88
class SkipNonExistingHook
99
{
1010
#[Intercept(InvalidHook::class, ActionWhenMissing::SKIP, 0)]
11-
public static function handleNonExistingHook(InvalidHook $hook)
12-
{
13-
14-
}
11+
public static function handleNonExistingHook(InvalidHook $hook) {}
1512
}

workbench/app/Interceptors/ThrowNonExistingHook.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,5 @@
88
class ThrowNonExistingHook
99
{
1010
#[Intercept(InvalidHook::class, ActionWhenMissing::THROW_ERROR, 0)]
11-
public static function handleNonExistingHook(InvalidHook $hook)
12-
{
13-
14-
}
11+
public static function handleNonExistingHook(InvalidHook $hook) {}
1512
}

0 commit comments

Comments
 (0)