From 94908eb9c130dd31efaa11edbf76c3fe644e6156 Mon Sep 17 00:00:00 2001 From: Ian Morland Date: Wed, 18 Oct 2023 14:13:53 +0100 Subject: [PATCH] fix: console extender does not accept ::class --- framework/core/src/Extend/Console.php | 7 +++++- .../integration/extenders/ConsoleTest.php | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/framework/core/src/Extend/Console.php b/framework/core/src/Extend/Console.php index dfa1945a2f..605202eaef 100644 --- a/framework/core/src/Extend/Console.php +++ b/framework/core/src/Extend/Console.php @@ -10,6 +10,7 @@ namespace Flarum\Extend; use Flarum\Extension\Extension; +use Flarum\Foundation\ContainerUtil; use Illuminate\Contracts\Container\Container; class Console implements ExtenderInterface @@ -61,7 +62,11 @@ public function extend(Container $container, Extension $extension = null) 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 8241118fbc..b09c71339d 100644 --- a/framework/core/tests/integration/extenders/ConsoleTest.php +++ b/framework/core/tests/integration/extenders/ConsoleTest.php @@ -75,6 +75,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 @@ -95,3 +112,11 @@ protected function fire() $this->info('Custom Command.'); } } + +class ScheduledCommandCallback +{ + public function __invoke(Event $event) + { + $event->everyMinute(); + } +}