diff --git a/framework/core/src/Extend/Console.php b/framework/core/src/Extend/Console.php index 741890465a..3164b86c47 100644 --- a/framework/core/src/Extend/Console.php +++ b/framework/core/src/Extend/Console.php @@ -11,6 +11,7 @@ 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 @@ public function extend(Container $container, Extension $extension = null): void 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); }); } diff --git a/framework/core/tests/integration/extenders/ConsoleTest.php b/framework/core/tests/integration/extenders/ConsoleTest.php index f1e71fb405..f536ae77a7 100644 --- a/framework/core/tests/integration/extenders/ConsoleTest.php +++ b/framework/core/tests/integration/extenders/ConsoleTest.php @@ -78,6 +78,23 @@ public function scheduled_command_exists_when_added() $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 @@ protected function fire(): int return Command::SUCCESS; } } + +class ScheduledCommandCallback +{ + public function __invoke(Event $event) + { + $event->everyMinute(); + } +}