jcalonso's avatar

Macros in Lumen

Is it possible to use Macros in Lumen?

I want to extend the \Illuminate\Console\Scheduling\Event class to allow something like:

  protected function schedule(Schedule $schedule)
    {
        $schedule->command('myapp:mycommand')
            ->everyMinute()
            ->thenReportUptimeMetric('xxxxx');
    }

Using a service provider like:

class ScheduledTaskMetricReporter extends ServiceProvider
{
    public function boot()
    {
        $this->registerMacros();
    }

    private function registerMacros()
    {
        Event::macro('thenReportUptimeMetric', function ($id) {
            return $this->then(function () use ($id) {
                app(MetricsClient::class)->increment($id);
            });
        });
    }
}

I am registering the service provider in bootrap/app.php but when running artisan I get:

  Method Illuminate\Console\Scheduling\Event::thenReportUptimeMetric does not exist.  
0 likes
1 reply
jcalonso's avatar

Moving the $this->registerMacros(); to the public function register(){} fixes the problem.

Please or to participate in this conversation.