lorisleiva / laravel-actions

⚡️ Laravel components that take care of one specific task
https://laravelactions.com
MIT License
2.52k stars 124 forks source link

Cannot access protected property Lorisleiva\Actions\Decorators\CommandDecorator::$output #261

Closed andruu closed 1 year ago

andruu commented 1 year ago

Hello, I'm trying to pass the $command variable into the handle method to update progress from artisan command line. I keep on running into the error: Cannot access protected property Lorisleiva\Actions\Decorators\CommandDecorator::$output

Here is the code that I'm using:

<?php
public function asCommand(Command $command): void
{
    $command->info('Processing data');
    $inputData = $command->ask('Enter input data');
    $summary = $this->handle($inputData, $command);

    $command->info('Processed ' . $summary['pages'] . ' pages');
    $command->info('Processed ' . count($summary['data_links_1']) . ' data links 1');
    $command->info('Processed ' . count($summary['data_links_2']) . ' data links 2');
}

public function handle(string $inputData, Command $commandLine = null): array
{
    $dataLinks1 = collect();
    $dataLinks2 = collect();

    $this->setupBrowser();
    $crawler = $this->makeRequest($inputData, 1);
    $params = $this->parseResponse($crawler);
    $pages = $this->extractNumberOfPages($crawler);
    $dataLinks1 = $dataLinks1->merge($this->generateDataLinks1($params));
    $dataLinks2 = $dataLinks2->merge($this->generateDataLinks2($params));

    $commandLine?->output->progressStart($pages);

    collect()->range(2, $pages)->each(function ($page) use ($inputData, $commandLine, &$dataLinks1, &$dataLinks2) {
        $crawler = $this->makeRequest($inputData, $page);
        $params = $this->parseResponse($crawler);
        $dataLinks1 = $dataLinks1->merge($this->generateDataLinks1($params));
        $dataLinks2 = $dataLinks2->merge($this->generateDataLinks2($params));

        $commandLine?->output->progressAdvance();
    });

    $commandLine?->output->progressFinish();

    return [
        'pages' => $pages,
        'data_links_1' => $dataLinks1,
        'data_links_2' => $dataLinks2,
    ];
}
?>
andruu commented 1 year ago

I figured it out instead of $command->output I needed to use $command->getOutput()