Artisan handle函数控制台命令的返回值

8

我使用以下命令创建了一个工匠命令:

php artisan make:command --command=custom:command SomeCustomCommand

运行命令后,它创建了以下文件:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class SomeCustomCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'custom:command';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        return 0;
    }
}

我发现默认情况下,handle方法返回一个整数。这是有意义的吗?如果我返回0表示错误,那么返回1表示成功吗?我正在查看Laravel文档,但没有说明handle函数应该返回什么。
3个回答

7

虽然在CLI中返回值不会影响命令执行,但是可以从命令handle()方法返回的整数值0、1或2具有特定含义。

请查看父类。Illuminate\Console\Command扩展了Symfony\Component\Console\Command\Command,您可以在其中找到公共常量,这些常量可以在您的命令handle()方法中返回整数值时使用(我认为应该使用)。这使得您的命令在逻辑上更加完整。

public const SUCCESS = 0;
public const FAILURE = 1;
public const INVALID = 2;

我通常做以下操作:

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle(): int
    {
        $this->info('Send Toolkit Report Command');
        $this->info($this->description);
        $this->info('Processing...');

        try {
            $email = $this->argument('email');

            /** @var User|null $user */
            $user = User::query()->where('email', '=', $email)->first();

            if (!$user) {
                $warning = sprintf("User with given email '%s' does not exist", $email);
                $this->warn($warning);
                Log::warning(get_class($this));
                Log::warning($warning);

                return self::INVALID;
            }

            GenerateToolkitReport::dispatch($user);
        } catch (\Throwable $exception) {
            $this->error('Command failed with error:');
            $this->error($exception->getMessage());
            $this->error('Please check the logs!');
            Log::error($exception);

            return self::FAILURE;
        }

        $this->info('Toolkit Report generation has been dispatched to the queue.');
        $this->info('Successfully processed!');

        return self::SUCCESS;
    }

3
你可以根据调用的位置和需要返回的内容自由地返回任何东西,我曾经看到过人们返回布尔值、数据甚至什么都不返回。
我认为这更多是基于个人意见的主题。就我个人而言,我会返回一个布尔值,因为它适合我在项目中使用实时通知处理失败/异常的情况。
例如,如果你通过另一个类使用Artisan::call()调用命令,你可以决定运行与你的命令完成的操作相关的逻辑(这可以用于检查退出代码)。或者你可以捕获任何异常并处理它们。你不需要从handle()中返回任何内容来使其工作。
    try {
        Artisan::call($object->command);
    } catch (ArtisanCommandException $e) {
        Log::error($e);
    }

    //note ArtisanCommandException is custom

请参见:https://laravel.com/docs/8.x/artisan#programmatically-executing-commands


0

我已经测试过了,在handle方法中更改返回值对artisan命令输出没有影响。

它更适用于编程调用,并在文档中被称为“退出代码”:

$exitCode = Artisan::call('command');

参考:https://laravel.com/docs/8.x/artisan#programmatically-executing-commands

这可能旨在检查命令是否成功执行。

但是,正如@Ballard所说,实际上可以返回任何内容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接