Get response from Artisan call

后端 未结 7 1404
情深已故
情深已故 2021-02-18 22:56

When I run in terminal php artisan migrate this results in \'Nothing to migrate\' when indeed there is nothing to migrate.

When I use Artisan::call(\'

相关标签:
7条回答
  • 2021-02-18 23:02

    Yes, it's possible. To get the output of a built-in artisan command from inside a custom command, pass the OutputStream from your command into the Artisan::call. Example:

    class MyCommand extends \Illuminate\Console\Command
    {
        public function fire()
        {
            \Artisan::call('optimize', [], $this->getOutput());
        }
    }
    
    0 讨论(0)
  • 2021-02-18 23:10

    I'm able to get the output of Artisan::call() with the via the following:

    use Symfony\Component\Console\Output\StreamOutput;
    
    $stream = fopen("php://output", "w");
    Artisan::call("migrate", array(), new StreamOutput($stream));
    
    var_dump($stream);
    
    0 讨论(0)
  • 2021-02-18 23:11

    The return result of all commands is defined in the class Symfony\Component\Console\Command\Command, method run:

    return is_numeric($statusCode) ? (int) $statusCode : 0;
    

    The $statusCode variable is set by calling the command's execute method, which in artisan's case is defined in the class Illuminate\Console\Command:

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        return $this->fire();
    }
    

    The result of the fire method is left up to the individual commands, in the case of php artisan migrate command, nothing is returned from the method so the $statusCode is null (which is why you get the 0 returned from Symfony\Component\Console\Command\Command::run method)

    If you want to get a response back from a custom command, just return an integer back from your fire method and it will bubble back up into the $statusCode. You can use that to programmatically switch against different results of your custom command.

    If you specifically want to get the result from the artisan:migrate command, then I don't think there's much you can do to change the return value besides wrapping the command in your own custom command that calls it.

    0 讨论(0)
  • 2021-02-18 23:11

    Late but might be of use to someone searching for the use case.

    Let me add how i did it in my tests to print the results to console. my problem was printing output while tests are running migrations. i was using modules and wanted to see the results of the migration process.

    $this->artisan('module:migrate');
    //same as running php artisan module:migrate or
    // $this->app['Illuminate\Contracts\Console\Kernel']->call('module:migrate');
    
    echo $this->app['Illuminate\Contracts\Console\Kernel']->output();
    
    0 讨论(0)
  • 2021-02-18 23:12

    When the Artisan command output you want is issuing an echo.

    You can access this type of output with ob_start and ob_get_clean.

    For example, if your command echos JSON.

    Artisan::command('myecho:command', function () {
    
        echo json_encode(config('myconfig'), true);
    
    })->describe('outputs json');
    

    Then you can access the JSON output by wrapping the command call in a buffer:

    \ob_start();
    \Artisan::call('myecho:command');
    $output = \ob_get_clean();
    
    var_dump($output);
    
    0 讨论(0)
  • 2021-02-18 23:24

    Maybe this will save some time to someone in the future, for me worked a mix of 2 answers:

    use Symfony\Component\Console\Output\StreamOutput;
    
    $stream = fopen("php://output", "w");
    Artisan::call('migrate', [
        '--path' => 'database/migrations/customer',
        '--force' => true,
        '--database' => $connectionName
    ], new StreamOutput($stream));
    
    $callResponse = ob_get_clean();
    

    StreamOutput helps to put the response in buffer and ob functions to get the response.

    0 讨论(0)
提交回复
热议问题