Php 如何从命令本身调用Laravel命令句柄?

Php 如何从命令本身调用Laravel命令句柄?,php,laravel,laravel-artisan,Php,Laravel,Laravel Artisan,我有这样的命令 class TestCommand extends Command { ..... constructor ..... public function handle() { .... command logic .... } } <?php class TestCommand extends Command { public function __construct(Handler $handler) {

我有这样的命令

class TestCommand extends Command {
 .....
 constructor
 .....

 public function handle()
 {
    ....
    command logic
    ....
 }
}
<?php

class TestCommand extends Command
{
    public function __construct(Handler $handler)
    {
        parent::__construct();

        $this->handler = $handler;
    }

    public function handle()
    {
        $this->handler->handle();
    }
}

// No need to extends from TestCommand
class ExtendedCommand extends Command
{
    public function __construct(Handler $handler)
    {
        parent::__construct();

        $this->handler = $handler;

        // do the rest
    }

    public function sync()
    {
        $this->handler->handle();
        $this->execute();
    }
}

class Handler
{
    public function handle()
    {
        // Put your logic here
    }
}
TestCommand
也注册在
Kernel.php

现在我需要扩展
TestCommand

class ExtendedCommand extends TestCommand {

  public function __construct ()
  {
    parent::__constrct();
    .....
    modify some variables
    .....
  }

  public function sync()
  {
     //trying to call the parent class handle method.

     $this->handle(); // not working & getting PHP Error:  Call to a member function getOption() on null in /home/vagrant/sinbad/vendor/laravel/framework/src/Illuminate/Console/Command.php on line 292
     $this->execute(); // obviously not working & getting TypeError: Too few arguments to function Illuminate\Console\Command::execute(),
  }
}

//instantiating the new command
   new(ExtendedCommand)->sync();
现在我需要调用
extended命令
我可以用什么方法来实现它?不在
kernel.php中注册此命令的解决方案会更好,因为我不是在寻找
Artisan::call
方式。

更像这样

class TestCommand extends Command {
 .....
 constructor
 .....

 public function handle()
 {
    ....
    command logic
    ....
 }
}
<?php

class TestCommand extends Command
{
    public function __construct(Handler $handler)
    {
        parent::__construct();

        $this->handler = $handler;
    }

    public function handle()
    {
        $this->handler->handle();
    }
}

// No need to extends from TestCommand
class ExtendedCommand extends Command
{
    public function __construct(Handler $handler)
    {
        parent::__construct();

        $this->handler = $handler;

        // do the rest
    }

    public function sync()
    {
        $this->handler->handle();
        $this->execute();
    }
}

class Handler
{
    public function handle()
    {
        // Put your logic here
    }
}

您需要将逻辑分离到另一个类。如何在@Alfa执行此操作?
(新的ExtendedCommand())->sync()
不起作用吗?但是考虑一下这个。您正在编写一个要使用的命令,就好像它只是一个普通类。它必须是一个命令吗?它不起作用,@apokryfos,这就是为什么我问这个问题:)我的答案解决了你的问题吗@ShobiPP?在你的解决方案中,
$this->execute()部件出现故障。因为它需要输入和输出参数。我自己也没能弄明白。但逻辑的分离在某种程度上帮助了我。谢谢+这只是一个伪代码。您可以将另一个参数传递给该方法。