Skip to content

Commit

Permalink
Merge pull request #6 from quix-labs/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
alancolant authored Jun 20, 2024
2 parents e78f622 + ddf767c commit d8f2271
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 76 deletions.
42 changes: 41 additions & 1 deletion src/Console/Commands/HooksStatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Console\Command;
use QuixLabs\LaravelHookSystem\Facades\HookManager;
use QuixLabs\LaravelHookSystem\Hook;
use QuixLabs\LaravelHookSystem\Hooks\GetHooksTable;
use QuixLabs\LaravelHookSystem\Utils\CommandTable;

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

$rows = [];
$hooks = collect(HookManager::getHooks())
->mapWithKeys(fn (string|Hook $hook) => [$hook => HookManager::getInterceptorsForHook($hook)])->toArray();
$rows = collect($hooks)->map(function (array $interceptors, string $hookClass) {
$callables = collect($interceptors)
->map(fn (array $callables, int $priority) => implode('<br/>', array_map(
fn (callable $callable) => static::_callableToString($callable), $callables)
))->join('<br/>');

$priorities = collect($interceptors)
->map(fn (array $callables, int $priority) => implode('<br/>', array_fill(0, count($callables), $priority)))
->join('<br/>');

return [
'Hook' => $hookClass,
'Interceptors' => $callables,
'Priority' => $priorities,
'Fully Cacheable' => HookManager::isFullyCacheable($hookClass) ? 'YES' : 'NO',
'Fully Cached' => HookManager::isFullyCached($hookClass) ? 'YES' : 'NO',
];
})->toArray();

GetHooksTable::send($rows);
if (count($rows) > 0) {
$this->output->writeln(CommandTable::asString($rows));
Expand All @@ -29,4 +50,23 @@ public function handle(): int

return self::SUCCESS;
}

public static function _callableToString(callable $callable): string
{
if (is_array($callable)) {
$class = is_object($callable[0]) ? get_class($callable[0]) : $callable[0];
$method = $callable[1];
} elseif (is_string($callable) && str_contains($callable, '::')) {
[$class, $method] = explode('::', $callable);
} else {
$class = get_class($callable);
$method = '__invoke';
}

$reflection = new \ReflectionMethod($class, $method);
$line = $reflection->getStartLine();

return sprintf('%s@%s:%d', $class, $method, $line);

}
}
54 changes: 2 additions & 52 deletions src/Hooks/GetHooksTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,9 @@

namespace QuixLabs\LaravelHookSystem\Hooks;

use QuixLabs\LaravelHookSystem\Facades\HookManager;
use QuixLabs\LaravelHookSystem\Hook;
use QuixLabs\LaravelHookSystem\Interfaces\FullyCacheable;

class GetHooksTable extends Hook implements FullyCacheable
class GetHooksTable extends Hook
{
public function __construct(public array &$rows)
{
}

public static function initialInstance(): static
{
$hooks = collect(HookManager::getHooks())
->mapWithKeys(fn (string|Hook $hook) => [$hook => HookManager::getInterceptorsForHook($hook)])->toArray();
$rows = collect($hooks)->map(function (array $interceptors, string $hookClass) {
$callables = collect($interceptors)
->map(fn (array $callables, int $priority) => implode('<br/>', array_map(
fn (callable $callable) => static::_callableToString($callable), $callables)
))->join('<br/>');

$priorities = collect($interceptors)
->map(fn (array $callables, int $priority) => implode('<br/>', array_fill(0, count($callables), $priority)))
->join('<br/>');

return [
'Hook' => $hookClass,
'Interceptors' => $callables,
'Priority' => $priorities,
'Fully Cacheable' => HookManager::isFullyCacheable($hookClass) ? 'YES' : 'NO',
'Fully Cached' => HookManager::isFullyCached($hookClass) ? 'YES' : 'NO',
];
})->toArray();

/** @phpstan-ignore-next-line */
return new static($rows);
}

public static function _callableToString(callable $callable): string
{
if (is_array($callable)) {
$class = is_object($callable[0]) ? get_class($callable[0]) : $callable[0];
$method = $callable[1];
} elseif (is_string($callable) && str_contains($callable, '::')) {
[$class, $method] = explode('::', $callable);
} else {
$class = get_class($callable);
$method = '__invoke';
}

$reflection = new \ReflectionMethod($class, $method);
$line = $reflection->getStartLine();

return sprintf('%s@%s:%d', $class, $method, $line);

}
public function __construct(public array &$rows) {}
}
3 changes: 1 addition & 2 deletions src/Utils/Intercept.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ public function __construct(
public ActionWhenMissing $actionWhenMissing = ActionWhenMissing::THROW_ERROR,
public int $priority = 0,
public bool $preventFullCache = false
) {
}
) {}
}
4 changes: 1 addition & 3 deletions workbench/app/Hooks/GetArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@

class GetArray extends Hook
{
public function __construct(public array &$array)
{
}
public function __construct(public array &$array) {}
}
4 changes: 1 addition & 3 deletions workbench/app/Hooks/GetString.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@

class GetString extends Hook
{
public function __construct(public string &$string)
{
}
public function __construct(public string &$string) {}
}
4 changes: 1 addition & 3 deletions workbench/app/Hooks/GetStringFullyCacheable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

class GetStringFullyCacheable extends Hook implements FullyCacheable
{
public function __construct(public string &$string)
{
}
public function __construct(public string &$string) {}

public static function initialInstance(): static
{
Expand Down
5 changes: 1 addition & 4 deletions workbench/app/Interceptors/RegisterNonExistingHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@
class RegisterNonExistingHook
{
#[Intercept(InvalidHook::class, ActionWhenMissing::REGISTER_HOOK, 0)]
public static function handleNonExistingHook(InvalidHook $hook)
{

}
public static function handleNonExistingHook(InvalidHook $hook) {}
}
5 changes: 1 addition & 4 deletions workbench/app/Interceptors/SkipNonExistingHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@
class SkipNonExistingHook
{
#[Intercept(InvalidHook::class, ActionWhenMissing::SKIP, 0)]
public static function handleNonExistingHook(InvalidHook $hook)
{

}
public static function handleNonExistingHook(InvalidHook $hook) {}
}
5 changes: 1 addition & 4 deletions workbench/app/Interceptors/ThrowNonExistingHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@
class ThrowNonExistingHook
{
#[Intercept(InvalidHook::class, ActionWhenMissing::THROW_ERROR, 0)]
public static function handleNonExistingHook(InvalidHook $hook)
{

}
public static function handleNonExistingHook(InvalidHook $hook) {}
}

0 comments on commit d8f2271

Please sign in to comment.