使用LiveWire to livewire to浏览器的流程工匠命令输出

问题描述 投票:0回答:1

问题是,仅在完成命令并且页面/UI之前显示输出才显示。 我尝试使用LiveWire的

Stream

功能


use Symfony\Component\Console\Output\StreamOutput;

public function streamTest()
{
    $stream = fopen('php://output', 'w');

    $outputBuffer = new StreamOutput($stream);

    Artisan::call('list', outputBuffer: $outputBuffer);

    $this->stream(
        to: 'output',
        content: $this->output,
        replace: true,
    );

    while ($content = stream_get_contents($outputBuffer->getStream())) {
        $this->output .= $content;
    }
}

上面的行为不起作用,我不知道为什么。我在使用流方面的经验非常有限,因此在该示例中可能存在一些明显的错误,但是我在网上找不到类似的东西。

fwiw,这是页面

的刀片模板 <div> <button wire:click="streamTest">Stream</button> Output: <pre wire:stream="output">{{ $output }}</pre> </div>

我认为您无法从单个LiveWire调用中实时进行输出,因为

Artisan::call

正在阻止,并且仅在完全完成后才返回其输出。一个简单的解决方法是在后台运行工匠命令(例如,通过排队的作业)并将输出写入存储(例如文件或数据库)。然后,进行LiveWire Component Coll或收听(例如使用Laravel Echo或SSE)进行新的输出,并在到达时显示。我不知道您的项目中的总体实施想法是什么,但是如果您只想对可能的外观有一个粗略的想法,那么您可以拥有这样的工作:

public function handle() { $process = new Process(['php', 'artisan', 'list']); $process->setTimeout(0); $process->start(); foreach ($process as $type => $data) { // Append $data to a file or a database record } }
然后在LiveWire组件中,进行民意调查或订阅更新,读取新输出并将其附加到UI。这样,您就不会阻止主请求,并且会看到其生成的输出。
    
php laravel laravel-livewire
1个回答
0
投票

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.