master

laravel/framework

Last updated at: 29/12/2023 09:23

InvokedProcess.php

TLDR

The InvokedProcess class in the provided file is a concrete implementation of the InvokedProcessContract interface. It represents an invoked process and provides methods for interacting with it.

Methods

__construct(Process $process)

This method is the constructor of the InvokedProcess class. It initializes the underlying process instance.

id(): ?int

This method returns the process ID if the process is still running, or null otherwise.

signal(int $signal): InvokedProcess

This method sends a signal to the process. It takes an integer parameter representing the signal to be sent and returns the current instance of InvokedProcess, allowing for method chaining.

running(): bool

This method checks whether the process is still running. It returns true if the process is running and false otherwise.

output(): string

This method returns the standard output of the process as a string.

errorOutput(): string

This method returns the error output of the process as a string.

latestOutput(): string

This method returns the latest standard output of the process as a string. It differs from output() in that it only returns the output since the last call to latestOutput().

latestErrorOutput(): string

This method returns the latest error output of the process as a string. It differs from errorOutput() in that it only returns the error output since the last call to latestErrorOutput().

wait(callable $output = null): ProcessResult

This method waits for the process to finish. It optionally takes a callable parameter for capturing the output during the wait. It returns a ProcessResult instance representing the result of the process.

Classes

None

<?php

namespace Illuminate\Process;

use Illuminate\Contracts\Process\InvokedProcess as InvokedProcessContract;
use Illuminate\Process\Exceptions\ProcessTimedOutException;
use Symfony\Component\Process\Exception\ProcessTimedOutException as SymfonyTimeoutException;
use Symfony\Component\Process\Process;

class InvokedProcess implements InvokedProcessContract
{
    /**
     * The underlying process instance.
     *
     * @var \Symfony\Component\Process\Process
     */
    protected $process;

    /**
     * Create a new invoked process instance.
     *
     * @param  \Symfony\Component\Process\Process  $process
     * @return void
     */
    public function __construct(Process $process)
    {
        $this->process = $process;
    }

    /**
     * Get the process ID if the process is still running.
     *
     * @return int|null
     */
    public function id()
    {
        return $this->process->getPid();
    }

    /**
     * Send a signal to the process.
     *
     * @param  int  $signal
     * @return $this
     */
    public function signal(int $signal)
    {
        $this->process->signal($signal);

        return $this;
    }

    /**
     * Determine if the process is still running.
     *
     * @return bool
     */
    public function running()
    {
        return $this->process->isRunning();
    }

    /**
     * Get the standard output for the process.
     *
     * @return string
     */
    public function output()
    {
        return $this->process->getOutput();
    }

    /**
     * Get the error output for the process.
     *
     * @return string
     */
    public function errorOutput()
    {
        return $this->process->getErrorOutput();
    }

    /**
     * Get the latest standard output for the process.
     *
     * @return string
     */
    public function latestOutput()
    {
        return $this->process->getIncrementalOutput();
    }

    /**
     * Get the latest error output for the process.
     *
     * @return string
     */
    public function latestErrorOutput()
    {
        return $this->process->getIncrementalErrorOutput();
    }

    /**
     * Wait for the process to finish.
     *
     * @param  callable|null  $output
     * @return \Illuminate\Process\ProcessResult
     *
     * @throws \Illuminate\Process\Exceptions\ProcessTimedOutException
     */
    public function wait(callable $output = null)
    {
        try {
            $this->process->wait($output);

            return new ProcessResult($this->process);
        } catch (SymfonyTimeoutException $e) {
            throw new ProcessTimedOutException($e, new ProcessResult($this->process));
        }
    }
}