-1

I'm trying to understand how to override a __construct in Process class. By default, construct only accepts an array as first parameter:

public function __construct(array $command, string $cwd = null, array $env = null, mixed $input = null, ?float $timeout = 60)

But I need for it to accepty strings aswell, which means something like this

public function __construct(array|string $command, string $cwd = null, array $env = null, mixed $input = null, ?float $timeout = 60)

Only overriding is accepted for solution tho, nothing else. I need this because of Behat tests, since when I run them, I get error "Process::__construct(): Argument #1 ($command) must be of type array, string given"

w0lfie.
  • 9
  • 2

1 Answers1

-1

As one option, you can change the incoming arguments by creating your own class extends the Process component:

use Symfony\Component\Process\Process;

class CustomProcess extends Process
{

    public function __construct(array|string $command, string $cwd = null, array $env = null, $input = null, ?float $timeout = 60)
    {
        if(is_string($command)){
            $command = [$command]; // do something with a string
        }
        parent::__construct($command, $cwd, $env, $input, $timeout);
    }
}

Then create an instance of your class in your controller like so:

$command = new CustomProcess('you command');
$command->run();
emrdev
  • 2,155
  • 3
  • 9
  • 15
  • I'm getting error 'Cannot autowire service: argument "$command" of method "Process:__construct()" is type-hinted "array", you should configure it's value explicitly." – w0lfie. Apr 11 '22 at 13:13
  • Have you created a service? If yes, then you don't need to. Just create a folder src\Process and add your class there. – emrdev Apr 11 '22 at 16:02