Skip to content

Commit

Permalink
Interpreters and factory CliTester::createPhpInterpreter() merged to …
Browse files Browse the repository at this point in the history
…class PhpInterpreter (BC break)
  • Loading branch information
dg committed Apr 17, 2016
1 parent f96569b commit 3310266
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 410 deletions.
33 changes: 3 additions & 30 deletions src/Runner/CliTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,37 +157,10 @@ private function createPhpInterpreter()
$args .= ' -d ' . Helpers::escapeArg($item);
}

// Is the executable Zend PHP or HHVM?
$proc = @proc_open( // @ is escalated to exception
$this->options['-p'] . ' --version',
[['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']],
$pipes,
NULL,
NULL,
['bypass_shell' => TRUE]
);
if ($proc === FALSE) {
throw new \Exception('Cannot run PHP interpreter ' . $this->options['-p'] . '. Use -p option.');
}
$output = stream_get_contents($pipes[1]);
$error = stream_get_contents($pipes[2]);
if (proc_close($proc)) {
throw new \Exception("Unable to run '{$this->options['-p']}': " . preg_replace('#[\r\n ]+#', ' ', $error));
}

if (preg_match('#HipHop VM#', $output)) {
$this->interpreter = new HhvmPhpInterpreter($this->options['-p'], $args);
} elseif (strpos($output, 'phpdbg') !== FALSE) {
$this->interpreter = new ZendPhpDbgInterpreter($this->options['-p'], $args);
} else {
$this->interpreter = new ZendPhpInterpreter($this->options['-p'], $args);
}
$this->interpreter = new PhpInterpreter($this->options['-p'], $args);

if ($this->interpreter->getStartupError()) {
echo Dumper::color('red', 'PHP startup error: ' . $this->interpreter->getStartupError()) . "\n";
if ($this->interpreter->isCgi()) {
echo "(note that PHP CLI generates better error messages)\n";
}
if ($error = $this->interpreter->getStartupError()) {
echo Dumper::color('red', "PHP startup error: $error") . "\n";
}
}

Expand Down
118 changes: 0 additions & 118 deletions src/Runner/HhvmPhpInterpreter.php

This file was deleted.

116 changes: 107 additions & 9 deletions src/Runner/PhpInterpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,137 @@

namespace Tester\Runner;

use Tester\Helpers;

interface PhpInterpreter

/**
* PHP command-line executable.
*/
class PhpInterpreter
{
/** @var string PHP arguments */
private $arguments;

/** @var string PHP executable */
private $path;

/** @var bool is CGI? */
private $cgi;

/** @var \stdClass created by info.php */
private $info;

/** @var string */
private $error;


public function __construct($path, $args = NULL)
{
$this->path = Helpers::escapeArg($path);
$proc = @proc_open( // @ is escalated to exception
$this->path . ' --version',
[['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']],
$pipes,
NULL,
NULL,
['bypass_shell' => TRUE]
);
if ($proc === FALSE) {
throw new \Exception("Cannot run PHP interpreter $path. Use -p option.");
}
$output = stream_get_contents($pipes[1]);
proc_close($proc);

$this->arguments = ' -n ' . $args;
if (preg_match('#HipHop VM#', $output)) {
$this->arguments = ' --php' . $this->arguments . ' -d hhvm.log.always_log_unhandled_exceptions=false'; // HHVM issue #3019
} elseif (strpos($output, 'phpdbg') !== FALSE) {
$this->arguments = ' -qrrb -S cli' . $this->arguments;
}

$proc = proc_open(
"$this->path $this->arguments " . Helpers::escapeArg(__DIR__ . '/info.php') . ' serialized',
[['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']],
$pipes,
NULL,
NULL,
['bypass_shell' => TRUE]
);
$output = stream_get_contents($pipes[1]);
$this->error = trim(stream_get_contents($pipes[2]));
if (proc_close($proc)) {
throw new \Exception("Unable to run $path: " . preg_replace('#[\r\n ]+#', ' ', $this->error));
}

$parts = explode("\r\n\r\n", $output, 2);
$this->cgi = count($parts) === 2;
if (!($this->info = @unserialize($parts[$this->cgi]))) {
throw new \Exception("Unable to detect PHP version (output: $output).");

} elseif ($this->info->hhvmVersion && version_compare($this->info->hhvmVersion, '3.3.0', '<')) {
throw new \Exception('HHVM below version 3.3.0 is not supported.');

} elseif ($this->info->phpDbgVersion && version_compare($this->info->version, '7.0.0', '<')) {
throw new \Exception('Unable to use phpdbg on PHP < 7.0.0.');

} elseif ($this->cgi && $this->error) {
$this->error .= "\n(note that PHP CLI generates better error messages)";
}
}


/**
* @param string
* @param string
* @param string
* @param string
*/
function addPhpIniOption($name, $value = NULL);
public function addPhpIniOption($name, $value = NULL)
{
$this->arguments .= ' -d ' . Helpers::escapeArg($name . ($value === NULL ? '' : "=$value"));
}


/**
* @return string
*/
function getCommandLine();
public function getCommandLine()
{
return $this->path . $this->arguments;
}


/**
* @return string
*/
function getVersion();
public function getVersion()
{
return $this->info->version;
}


/**
* @return bool
*/
function canMeasureCodeCoverage();
public function canMeasureCodeCoverage()
{
return $this->info->canMeasureCodeCoverage;
}


/**
* @return bool
*/
function isCgi();
public function isCgi()
{
return $this->cgi;
}


/**
* @return string
*/
function getStartupError();
public function getStartupError()
{
return $this->error;
}

}
Loading

0 comments on commit 3310266

Please sign in to comment.