Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. DefaultPhpProcess.php

class DefaultPhpProcess

@no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit

@internal This class is not covered by the backward compatibility promise for PHPUnit

Hierarchy

  • class \PHPUnit\Util\PHP\AbstractPhpProcess
    • class \PHPUnit\Util\PHP\DefaultPhpProcess extends \PHPUnit\Util\PHP\AbstractPhpProcess

Expanded class hierarchy of DefaultPhpProcess

File

vendor/phpunit/phpunit/src/Util/PHP/DefaultPhpProcess.php, line 31

Namespace

PHPUnit\Util\PHP
View source
class DefaultPhpProcess extends AbstractPhpProcess {
    private ?string $tempFile = null;
    
    /**
     * Runs a single job (PHP code) using a separate PHP process.
     *
     * @psalm-return array{stdout: string, stderr: string}
     *
     * @throws Exception
     * @throws PhpProcessException
     */
    public function runJob(string $job, array $settings = []) : array {
        if ($this->stdin) {
            if (!($this->tempFile = tempnam(sys_get_temp_dir(), 'phpunit_')) || file_put_contents($this->tempFile, $job) === false) {
                throw new PhpProcessException('Unable to write temporary file');
            }
            $job = $this->stdin;
        }
        return $this->runProcess($job, $settings);
    }
    
    /**
     * Handles creating the child process and returning the STDOUT and STDERR.
     *
     * @psalm-return array{stdout: string, stderr: string}
     *
     * @throws Exception
     * @throws PhpProcessException
     */
    protected function runProcess(string $job, array $settings) : array {
        $env = null;
        if ($this->env) {
            $env = $_SERVER ?? [];
            unset($env['argv'], $env['argc']);
            $env = array_merge($env, $this->env);
            foreach ($env as $envKey => $envVar) {
                if (is_array($envVar)) {
                    unset($env[$envKey]);
                }
            }
        }
        $pipeSpec = [
            0 => [
                'pipe',
                'r',
            ],
            1 => [
                'pipe',
                'w',
            ],
            2 => [
                'pipe',
                'w',
            ],
        ];
        if ($this->stderrRedirection) {
            $pipeSpec[2] = [
                'redirect',
                1,
            ];
        }
        $process = proc_open($this->getCommand($settings, $this->tempFile), $pipeSpec, $pipes, null, $env);
        if (!is_resource($process)) {
            throw new PhpProcessException('Unable to spawn worker process');
        }
        if ($job) {
            $this->process($pipes[0], $job);
        }
        fclose($pipes[0]);
        $stderr = $stdout = '';
        if (isset($pipes[1])) {
            $stdout = stream_get_contents($pipes[1]);
            fclose($pipes[1]);
        }
        if (isset($pipes[2])) {
            $stderr = stream_get_contents($pipes[2]);
            fclose($pipes[2]);
        }
        proc_close($process);
        $this->cleanup();
        return [
            'stdout' => $stdout,
            'stderr' => $stderr,
        ];
    }
    
    /**
     * @param resource $pipe
     */
    protected function process($pipe, string $job) : void {
        fwrite($pipe, $job);
    }
    protected function cleanup() : void {
        if ($this->tempFile) {
            unlink($this->tempFile);
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
AbstractPhpProcess::$arguments protected property
AbstractPhpProcess::$env protected property @psalm-var array<string, string>
AbstractPhpProcess::$stderrRedirection protected property
AbstractPhpProcess::$stdin protected property
AbstractPhpProcess::factory public static function
AbstractPhpProcess::getArgs public function Returns the string of arguments to pass to the php job.
AbstractPhpProcess::getCommand public function Returns the command based into the configurations.
AbstractPhpProcess::getEnv public function Returns the array of environment variables to start the child process with.
AbstractPhpProcess::getStdin public function Returns the input string to be sent via STDIN.
AbstractPhpProcess::processChildResult private function
AbstractPhpProcess::runTestJob public function Runs a single test in a separate PHP process.
AbstractPhpProcess::setArgs public function Sets the string of arguments to pass to the php job.
AbstractPhpProcess::setEnv public function Sets the array of environment variables to start the child process with.
AbstractPhpProcess::setStdin public function Sets the input string to be sent via STDIN.
AbstractPhpProcess::settingsToParameters protected function
AbstractPhpProcess::setUseStderrRedirection public function Defines if should use STDERR redirection or not.
AbstractPhpProcess::useStderrRedirection public function Returns TRUE if uses STDERR redirection or FALSE if not.
DefaultPhpProcess::$tempFile private property
DefaultPhpProcess::cleanup protected function
DefaultPhpProcess::process protected function
DefaultPhpProcess::runJob public function Runs a single job (PHP code) using a separate PHP process. Overrides AbstractPhpProcess::runJob
DefaultPhpProcess::runProcess protected function Handles creating the child process and returning the STDOUT and STDERR.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal