Skip to content

Commit

Permalink
refactor/better-command-descriptions (#4)
Browse files Browse the repository at this point in the history
* showed better command descriptions for commands without names, 
* updated event list screenshot
  • Loading branch information
cdinopol authored Apr 16, 2024
1 parent c83d314 commit 243facc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Binary file modified .docs/screenshots/screenshot-events-page.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions src/Services/SchedulePoliceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Acdphp\SchedulePolice\Data\ExecResult;
use Acdphp\SchedulePolice\Data\ScheduledEvent;
use Acdphp\SchedulePolice\Models\StoppedScheduledEvent;
use Closure;
use Illuminate\Console\Scheduling\CallbackEvent;
use Illuminate\Console\Scheduling\Event;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Contracts\Console\Kernel;
Expand All @@ -14,6 +16,8 @@
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use ReflectionClass;
use ReflectionFunction;
use Throwable;

class SchedulePoliceService
Expand Down Expand Up @@ -121,6 +125,10 @@ public function stoppedEvent(Event $event): ?StoppedScheduledEvent

public function getEventKey(Event $event): string
{
if ($event instanceof CallbackEvent) {
return 'Closure at: '.$this->getClosureLocation($event);
}

return Str::of($event->command)
->after('artisan\'')
->whenEmpty(fn () => Str::of($event->description))
Expand All @@ -131,4 +139,35 @@ public function setConfig(array $config): void
{
$this->config = $config;
}

private function getClosureLocation(CallbackEvent $event): string
{
$callback = (new ReflectionClass($event))->getProperty('callback')->getValue($event);

if ($callback instanceof Closure) {
$function = new ReflectionFunction($callback);

return sprintf(
'%s:%s',
str_replace(
app()->basePath().DIRECTORY_SEPARATOR,
'',
$function->getFileName() ?: ''
),
$function->getStartLine()
);
}

if (is_string($callback)) {
return $callback;
}

if (is_array($callback)) {
$className = is_string($callback[0]) ? $callback[0] : $callback[0]::class;

return sprintf('%s::%s', $className, $callback[1]);
}

return sprintf('%s::__invoke', $callback::class);
}
}

0 comments on commit 243facc

Please sign in to comment.