fix: console extender does not accept ::class attribute for schedule (#3903)

This commit is contained in:
IanM 2023-10-21 17:34:48 +01:00 committed by GitHub
parent db0d9cb006
commit 94de8b42b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

@ -11,6 +11,7 @@ namespace Flarum\Extend;
use Flarum\Console\AbstractCommand;
use Flarum\Extension\Extension;
use Flarum\Foundation\ContainerUtil;
use Illuminate\Contracts\Container\Container;
class Console implements ExtenderInterface
@ -62,7 +63,11 @@ class Console implements ExtenderInterface
return array_merge($existingCommands, $this->addCommands);
});
$container->extend('flarum.console.scheduled', function ($existingScheduled) {
$container->extend('flarum.console.scheduled', function ($existingScheduled) use ($container) {
foreach ($this->scheduled as &$schedule) {
$schedule['callback'] = ContainerUtil::wrapCallback($schedule['callback'], $container);
}
return array_merge($existingScheduled, $this->scheduled);
});
}

View File

@ -78,6 +78,23 @@ class ConsoleTest extends ConsoleTestCase
$this->assertStringContainsString('cache:clear', $this->runCommand($input));
}
/**
* @test
*/
public function scheduled_command_exists_when_added_with_class_syntax()
{
$this->extend(
(new Extend\Console())
->schedule('cache:clear', ScheduledCommandCallback::class)
);
$input = [
'command' => 'schedule:list'
];
$this->assertStringContainsString('cache:clear', $this->runCommand($input));
}
}
class CustomCommand extends AbstractCommand
@ -94,3 +111,11 @@ class CustomCommand extends AbstractCommand
return Command::SUCCESS;
}
}
class ScheduledCommandCallback
{
public function __invoke(Event $event)
{
$event->everyMinute();
}
}