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 use the components property on the console command class #205

Closed FrancisMawn closed 2 years ago

FrancisMawn commented 2 years ago

Hi,

I was trying to make use of the new components property on the Laravel Command class to enhance messages output to the console. So for example, with a regular command class, you can do stuff like

$this->components->info('Info message');

However, trying to access the components property on the injected Command instance throws the following exception:

Cannot access protected property Lorisleiva\Actions\Decorators\CommandDecorator::$components

Am I doing something wrong?

leandrodiogenes commented 2 years ago

Hi @FrancisMawn, try this.

$this->info('Info message');
FrancisMawn commented 2 years ago

@leandrodiogenes Not working:

Call to undefined method App\Actions\InstallApplication::info()
leandrodiogenes commented 2 years ago

Im sorry, take some time check the docs in that session. https://laravelactions.com/2.x/as-command.html#methods-used

namespace App\Actions;

use Illuminate\Console\Command;

class TestCommandAction extends Action {
    public string $commandSignature = 'test-action';

    public function asCommand(Command $command): void {
        $command->info('Done!');
    }

}
FrancisMawn commented 2 years ago

Im sorry, take some time and read my original post :)

I know I can use the $command->info() method. That's what I'm currently using. I was asking for a way to use the components instance on the Illuminate\Console\Command class.

public function asCommand(Command $command): void 
{
    $command->components->info('Done!');
}
leandrodiogenes commented 2 years ago

sorry, but im a little confused to know what the difference between the two ways of using it.

leandrodiogenes commented 2 years ago

I dont think we can access that property. As it is a protected property and there is a notice on it.

namespace Illuminate\Console\Concerns;

trait InteractsWithIO
{
    /**
     * The console components factory.
     *
     * @var \Illuminate\Console\View\Components\Factory
     *
     * @internal This property is not meant to be used or overwritten outside the framework.
     */
    protected $components;
FrancisMawn commented 2 years ago

image

As you can see, the difference is the formatting. I wanted to see if there was a way to leverage the same output as the other commands.

When using a standard Command class, you can access the components property.

leandrodiogenes commented 2 years ago

Im gonna make a pull request to solve that problem, let's hope to be accepted.

lorisleiva commented 2 years ago

Thanks for raising this and @leandrodiogenes for the PR!

I've merged and published this as part of v2.4.1.