运行 handle() 后自定义 artisan 命令的代码

Run code on custom artisan command after handle()

我正在做一个项目运行使用旧版本的Laravel (5.6)。这个项目中有很多自定义 artisan 命令。其中一些命令应该在执行结束时创建一个摘要。我的第一个想法是创建一个 parent class,所有命令都从这个 class 扩展而来,例如:

<?php

abstract class ParentCommand extends Command
{

  abstract protected function doYourStuff();
  abstract protected function prepareSummaryData();

  final public function handle()
  {
     $this->doYourStuff();
     $this->createSummary($this->prepareSummaryData());
  }

  final protected function createSummary($data)
  {
     // Summary stuff in here...
  }

}

但问题是Laravel是在handle方法中DI,所以childclasses中的handle方法可以有不同的签名,这是不允许的。 :(

知道如何在执行 handle() 方法后 运行 做些什么吗?

使用特征怎么样?

trait Summary
{
    protected function createSummary($data)
    {
        // Summary stuff in here...
    }
}

class SomeCommand extends Command
{
    use Summary;
    
    public function handle()
    {
        //do stuff
        //prepare data to summarize
        $this->createSummary($data);
    }
}