Skip to content

Commit

Permalink
fix: console extender does not accept ::class attribute for schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Oct 18, 2023
1 parent db0d9cb commit 734e39e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
7 changes: 6 additions & 1 deletion framework/core/src/Extend/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
});
}
Expand Down
25 changes: 25 additions & 0 deletions framework/core/tests/integration/extenders/ConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -94,3 +111,11 @@ protected function fire(): int
return Command::SUCCESS;
}
}

class ScheduledCommandCallback
{
public function __invoke(Event $event)
{
$event->everyMinute();
}
}

0 comments on commit 734e39e

Please sign in to comment.