| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Console\Scheduling;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Console\Application;
|
|
|
6 |
use Illuminate\Console\Command;
|
|
|
7 |
use Symfony\Component\Console\Attribute\AsCommand;
|
|
|
8 |
|
|
|
9 |
#[AsCommand(name: 'schedule:test')]
|
|
|
10 |
class ScheduleTestCommand extends Command
|
|
|
11 |
{
|
|
|
12 |
/**
|
|
|
13 |
* The console command name.
|
|
|
14 |
*
|
|
|
15 |
* @var string
|
|
|
16 |
*/
|
|
|
17 |
protected $signature = 'schedule:test {--name= : The name of the scheduled command to run}';
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* The name of the console command.
|
|
|
21 |
*
|
|
|
22 |
* This name is used to identify the command during lazy loading.
|
|
|
23 |
*
|
|
|
24 |
* @var string|null
|
|
|
25 |
*
|
|
|
26 |
* @deprecated
|
|
|
27 |
*/
|
|
|
28 |
protected static $defaultName = 'schedule:test';
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* The console command description.
|
|
|
32 |
*
|
|
|
33 |
* @var string
|
|
|
34 |
*/
|
|
|
35 |
protected $description = 'Run a scheduled command';
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Execute the console command.
|
|
|
39 |
*
|
|
|
40 |
* @param \Illuminate\Console\Scheduling\Schedule $schedule
|
|
|
41 |
* @return void
|
|
|
42 |
*/
|
|
|
43 |
public function handle(Schedule $schedule)
|
|
|
44 |
{
|
|
|
45 |
$phpBinary = Application::phpBinary();
|
|
|
46 |
|
|
|
47 |
$commands = $schedule->events();
|
|
|
48 |
|
|
|
49 |
$commandNames = [];
|
|
|
50 |
|
|
|
51 |
foreach ($commands as $command) {
|
|
|
52 |
$commandNames[] = $command->command ?? $command->getSummaryForDisplay();
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
if (empty($commandNames)) {
|
|
|
56 |
return $this->components->info('No scheduled commands have been defined.');
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
if (! empty($name = $this->option('name'))) {
|
|
|
60 |
$commandBinary = $phpBinary.' '.Application::artisanBinary();
|
|
|
61 |
|
|
|
62 |
$matches = array_filter($commandNames, function ($commandName) use ($commandBinary, $name) {
|
|
|
63 |
return trim(str_replace($commandBinary, '', $commandName)) === $name;
|
|
|
64 |
});
|
|
|
65 |
|
|
|
66 |
if (count($matches) !== 1) {
|
|
|
67 |
$this->components->info('No matching scheduled command found.');
|
|
|
68 |
|
|
|
69 |
return;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
$index = key($matches);
|
|
|
73 |
} else {
|
|
|
74 |
$index = array_search($this->components->choice('Which command would you like to run?', $commandNames), $commandNames);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
$event = $commands[$index];
|
|
|
78 |
|
|
|
79 |
$summary = $event->getSummaryForDisplay();
|
|
|
80 |
|
|
|
81 |
$command = $event instanceof CallbackEvent
|
|
|
82 |
? $summary
|
|
|
83 |
: trim(str_replace($phpBinary, '', $event->command));
|
|
|
84 |
|
|
|
85 |
$description = sprintf(
|
|
|
86 |
'Running [%s]%s',
|
|
|
87 |
$command,
|
|
|
88 |
$event->runInBackground ? ' in background' : '',
|
|
|
89 |
);
|
|
|
90 |
|
|
|
91 |
$this->components->task($description, fn () => $event->run($this->laravel));
|
|
|
92 |
|
|
|
93 |
if (! $event instanceof CallbackEvent) {
|
|
|
94 |
$this->components->bulletList([$event->getSummaryForDisplay()]);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
$this->newLine();
|
|
|
98 |
}
|
|
|
99 |
}
|