Skip to content

Commit ae4f2ab

Browse files
xiCO2ktaylorotwell
andauthored
[9.x] Show Closure Location on the schedule:list (#41494)
* Show Closure Location on the `schedule:list`. * style: fixes * formatting * formatting * use laravel instance Co-authored-by: Taylor Otwell <[email protected]>
1 parent 2d53f70 commit ae4f2ab

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/Illuminate/Console/Scheduling/ScheduleListCommand.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Illuminate\Console\Application;
88
use Illuminate\Console\Command;
99
use Illuminate\Support\Carbon;
10+
use ReflectionClass;
11+
use ReflectionFunction;
1012
use Symfony\Component\Console\Terminal;
1113

1214
class ScheduleListCommand extends Command
@@ -59,6 +61,10 @@ public function handle(Schedule $schedule)
5961
);
6062
}
6163

64+
if ($event instanceof CallbackEvent) {
65+
$command = 'Closure at: '.$this->getClosureLocation($event);
66+
}
67+
6268
$command = mb_strlen($command) > 1 ? "{$command} " : '';
6369

6470
$nextDueDateLabel = 'Next Due:';
@@ -135,6 +141,25 @@ private function formatCronExpression($expression, $spacing)
135141
->implode(' ');
136142
}
137143

144+
/**
145+
* Get the file and line number for the event closure.
146+
*
147+
* @param \Illuminate\Console\Scheduling\CallbackEvent $event
148+
* @return string
149+
*/
150+
private function getClosureLocation(CallbackEvent $event)
151+
{
152+
$function = new ReflectionFunction(tap((new ReflectionClass($event))->getProperty('callback'))
153+
->setAccessible(true)
154+
->getValue($event));
155+
156+
return sprintf(
157+
'%s:%s',
158+
str_replace($this->laravel->basePath().DIRECTORY_SEPARATOR, '', $function->getFileName() ?: ''),
159+
$function->getStartLine()
160+
);
161+
}
162+
138163
/**
139164
* Get the terminal width.
140165
*

tests/Integration/Console/Scheduling/ScheduleListCommandTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,20 @@ protected function setUp(): void
2323

2424
public function testDisplaySchedule()
2525
{
26-
$this->schedule->call(fn () => '')->everyMinute();
2726
$this->schedule->command(FooCommand::class)->quarterly();
2827
$this->schedule->command('inspire')->twiceDaily(14, 18);
2928
$this->schedule->command('foobar', ['a' => 'b'])->everyMinute();
3029

30+
$this->schedule->call(fn () => '')->everyMinute();
31+
$closureLineNumber = __LINE__ - 1;
32+
$closureFilePath = __FILE__;
33+
3134
$this->artisan(ScheduleListCommand::class)
3235
->assertSuccessful()
33-
->expectsOutput(' * * * * * ............................ Next Due: 1 minute from now')
3436
->expectsOutput(' 0 0 1 1-12/3 * php artisan foo:command .... Next Due: 3 months from now')
3537
->expectsOutput(' 0 14,18 * * * php artisan inspire ........ Next Due: 14 hours from now')
36-
->expectsOutput(' * * * * * php artisan foobar a='.ProcessUtils::escapeArgument('b').' ... Next Due: 1 minute from now');
38+
->expectsOutput(' * * * * * php artisan foobar a='.ProcessUtils::escapeArgument('b').' ... Next Due: 1 minute from now')
39+
->expectsOutput(' * * * * * Closure at: '.$closureFilePath.':'.$closureLineNumber.' Next Due: 1 minute from now');
3740
}
3841

3942
public function testDisplayScheduleInVerboseMode()

0 commit comments

Comments
 (0)