Skip to content

Commit

Permalink
[Process] Fix docblocks, remove return from PhpProcess#start() as…
Browse files Browse the repository at this point in the history
… parent returns nothing, cleaned up `ExecutableFinder`
  • Loading branch information
stloyd committed Jan 12, 2013
1 parent 78002b9 commit fa405bd
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 56 deletions.
21 changes: 11 additions & 10 deletions ExecutableFinder.php
Expand Up @@ -19,22 +19,23 @@
*/ */
class ExecutableFinder class ExecutableFinder
{ {
private static $isWindows;

private $suffixes = array('.exe', '.bat', '.cmd', '.com'); private $suffixes = array('.exe', '.bat', '.cmd', '.com');


public function __construct() /**
{ * Replaces default suffixes of executable.
if (null === self::$isWindows) { *
self::$isWindows = 0 === stripos(PHP_OS, 'win'); * @param array $suffixes
} */
}

public function setSuffixes(array $suffixes) public function setSuffixes(array $suffixes)
{ {
$this->suffixes = $suffixes; $this->suffixes = $suffixes;
} }


/**
* Adds new possible suffix to check for executable.
*
* @param string $suffix
*/
public function addSuffix($suffix) public function addSuffix($suffix)
{ {
$this->suffixes[] = $suffix; $this->suffixes[] = $suffix;
Expand Down Expand Up @@ -78,7 +79,7 @@ public function find($name, $default = null, array $extraDirs = array())
} }
foreach ($suffixes as $suffix) { foreach ($suffixes as $suffix) {
foreach ($dirs as $dir) { foreach ($dirs as $dir) {
if (is_file($file = $dir.DIRECTORY_SEPARATOR.$name.$suffix) && (self::$isWindows || is_executable($file))) { if (is_file($file = $dir.DIRECTORY_SEPARATOR.$name.$suffix) && (defined('PHP_WINDOWS_VERSION_BUILD') || is_executable($file))) {
return $file; return $file;
} }
} }
Expand Down
2 changes: 1 addition & 1 deletion PhpProcess.php
Expand Up @@ -66,6 +66,6 @@ public function start($callback = null)
$this->setCommandLine($php); $this->setCommandLine($php);
} }


return parent::start($callback); parent::start($callback);
} }
} }
33 changes: 17 additions & 16 deletions Process.php
Expand Up @@ -159,8 +159,8 @@ public function __destruct()
* The STDOUT and STDERR are also available after the process is finished * The STDOUT and STDERR are also available after the process is finished
* via the getOutput() and getErrorOutput() methods. * via the getOutput() and getErrorOutput() methods.
* *
* @param Closure|string|array $callback A PHP callback to run whenever there is some * @param callback|null $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR * output available on STDOUT or STDERR
* *
* @return integer The exit status code * @return integer The exit status code
* *
Expand Down Expand Up @@ -190,8 +190,8 @@ public function run($callback = null)
* with true as a second parameter then the callback will get all data occurred * with true as a second parameter then the callback will get all data occurred
* in (and since) the start call. * in (and since) the start call.
* *
* @param Closure|string|array $callback A PHP callback to run whenever there is some * @param callback|null $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR * output available on STDOUT or STDERR
* *
* @throws \RuntimeException When process can't be launch or is stopped * @throws \RuntimeException When process can't be launch or is stopped
* @throws \RuntimeException When process is already running * @throws \RuntimeException When process is already running
Expand Down Expand Up @@ -318,11 +318,12 @@ public function start($callback = null)
* from the output in real-time while writing the standard input to the process. * from the output in real-time while writing the standard input to the process.
* It allows to have feedback from the independent process during execution. * It allows to have feedback from the independent process during execution.
* *
* @param mixed $callback A valid PHP callback * @param callback|null $callback A valid PHP callback
* *
* @return int The exitcode of the process * @return integer The exitcode of the process
* *
* @throws \RuntimeException * @throws \RuntimeException When process timed out
* @throws \RuntimeException When process stopped after receiving signal
*/ */
public function wait($callback = null) public function wait($callback = null)
{ {
Expand Down Expand Up @@ -455,8 +456,6 @@ public function getExitCode()
* *
* @return string A string representation for the exit status code * @return string A string representation for the exit status code
* *
* @throws RuntimeException In case --enable-sigchild is activated and the sigchild compatibility mode is disabled
*
* @see http://tldp.org/LDP/abs/html/exitcodes.html * @see http://tldp.org/LDP/abs/html/exitcodes.html
* @see http://en.wikipedia.org/wiki/Unix_signal * @see http://en.wikipedia.org/wiki/Unix_signal
*/ */
Expand All @@ -472,8 +471,6 @@ public function getExitCodeText()
* *
* @return Boolean true if the process ended successfully, false otherwise * @return Boolean true if the process ended successfully, false otherwise
* *
* @throws RuntimeException In case --enable-sigchild is activated and the sigchild compatibility mode is disabled
*
* @api * @api
*/ */
public function isSuccessful() public function isSuccessful()
Expand Down Expand Up @@ -576,11 +573,9 @@ public function isRunning()
/** /**
* Stops the process. * Stops the process.
* *
* @param float $timeout The timeout in seconds * @param integer|float $timeout The timeout in seconds
* *
* @return integer The exit-code of the process * @return integer The exit-code of the process
*
* @throws \RuntimeException if the process got signaled
*/ */
public function stop($timeout=10) public function stop($timeout=10)
{ {
Expand Down Expand Up @@ -828,9 +823,9 @@ public function setEnhanceSigchildCompatibility($enhance)
* The callbacks adds all occurred output to the specific buffer and calls * The callbacks adds all occurred output to the specific buffer and calls
* the user callback (if present) with the received output. * the user callback (if present) with the received output.
* *
* @param mixed $callback The user defined PHP callback * @param callback|null $callback The user defined PHP callback
* *
* @return mixed A PHP callable * @return callback A PHP callable
*/ */
protected function buildCallback($callback) protected function buildCallback($callback)
{ {
Expand Down Expand Up @@ -870,13 +865,19 @@ protected function updateStatus()
} }
} }


/**
* Updates the current error output of the process (STDERR).
*/
protected function updateErrorOutput() protected function updateErrorOutput()
{ {
if (isset($this->pipes[self::STDERR]) && is_resource($this->pipes[self::STDERR])) { if (isset($this->pipes[self::STDERR]) && is_resource($this->pipes[self::STDERR])) {
$this->addErrorOutput(stream_get_contents($this->pipes[self::STDERR])); $this->addErrorOutput(stream_get_contents($this->pipes[self::STDERR]));
} }
} }


/**
* Updates the current output of the process (STDOUT).
*/
protected function updateOutput() protected function updateOutput()
{ {
if (defined('PHP_WINDOWS_VERSION_BUILD') && isset($this->fileHandles[self::STDOUT]) && is_resource($this->fileHandles[self::STDOUT])) { if (defined('PHP_WINDOWS_VERSION_BUILD') && isset($this->fileHandles[self::STDOUT]) && is_resource($this->fileHandles[self::STDOUT])) {
Expand Down
14 changes: 12 additions & 2 deletions Tests/AbstractProcessTest.php
Expand Up @@ -16,8 +16,6 @@
*/ */
abstract class AbstractProcessTest extends \PHPUnit_Framework_TestCase abstract class AbstractProcessTest extends \PHPUnit_Framework_TestCase
{ {
abstract protected function getProcess($commandline, $cwd = null, array $env = null, $stdin = null, $timeout = 60, array $options = array());

/** /**
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
*/ */
Expand Down Expand Up @@ -300,4 +298,16 @@ public function methodProvider()


return $defaults; return $defaults;
} }

/**
* @param string $commandline
* @param null $cwd
* @param array $env
* @param null $stdin
* @param integer $timeout
* @param array $options
*
* @return \Symfony\Component\Process\Process
*/
abstract protected function getProcess($commandline, $cwd = null, array $env = null, $stdin = null, $timeout = 60, array $options = array());
} }
20 changes: 11 additions & 9 deletions Tests/SigchildDisabledProcessTest.php
Expand Up @@ -13,15 +13,6 @@


class SigchildDisabledProcessTest extends AbstractProcessTest class SigchildDisabledProcessTest extends AbstractProcessTest
{ {

protected function getProcess($commandline, $cwd = null, array $env = null, $stdin = null, $timeout = 60, array $options = array())
{
$process = new ProcessInSigchildEnvironment($commandline, $cwd, $env, $stdin, $timeout, $options);
$process->setEnhanceSigchildCompatibility(false);

return $process;
}

/** /**
* @expectedException Symfony\Component\Process\Exception\RuntimeException * @expectedException Symfony\Component\Process\Exception\RuntimeException
*/ */
Expand Down Expand Up @@ -96,4 +87,15 @@ public function testIsNotSuccessful()
{ {
parent::testIsNotSuccessful(); parent::testIsNotSuccessful();
} }

/**
* {@inheritdoc}
*/
protected function getProcess($commandline, $cwd = null, array $env = null, $stdin = null, $timeout = 60, array $options = array())
{
$process = new ProcessInSigchildEnvironment($commandline, $cwd, $env, $stdin, $timeout, $options);
$process->setEnhanceSigchildCompatibility(false);

return $process;
}
} }
19 changes: 10 additions & 9 deletions Tests/SigchildEnabledProcessTest.php
Expand Up @@ -13,15 +13,6 @@


class SigchildEnabledProcessTest extends AbstractProcessTest class SigchildEnabledProcessTest extends AbstractProcessTest
{ {

protected function getProcess($commandline, $cwd = null, array $env = null, $stdin = null, $timeout = 60, array $options = array())
{
$process = new ProcessInSigchildEnvironment($commandline, $cwd, $env, $stdin, $timeout, $options);
$process->setEnhanceSigchildCompatibility(true);

return $process;
}

/** /**
* @expectedException Symfony\Component\Process\Exception\RuntimeException * @expectedException Symfony\Component\Process\Exception\RuntimeException
*/ */
Expand Down Expand Up @@ -62,4 +53,14 @@ public function testExitCodeText()
$this->assertInternalType('string', $process->getExitCodeText()); $this->assertInternalType('string', $process->getExitCodeText());
} }


/**
* {@inheritdoc}
*/
protected function getProcess($commandline, $cwd = null, array $env = null, $stdin = null, $timeout = 60, array $options = array())
{
$process = new ProcessInSigchildEnvironment($commandline, $cwd, $env, $stdin, $timeout, $options);
$process->setEnhanceSigchildCompatibility(true);

return $process;
}
} }
27 changes: 18 additions & 9 deletions Tests/SimpleProcessTest.php
Expand Up @@ -15,20 +15,14 @@


class SimpleProcessTest extends AbstractProcessTest class SimpleProcessTest extends AbstractProcessTest
{ {
private $enabledSigchild = false;


protected function skipIfPHPSigchild() public function setUp()
{ {
ob_start(); ob_start();
phpinfo(INFO_GENERAL); phpinfo(INFO_GENERAL);


if (false !== strpos(ob_get_clean(), '--enable-sigchild')) { $this->enabledSigchild = false !== strpos(ob_get_clean(), '--enable-sigchild');
$this->markTestSkipped('Your PHP has been compiled with --enable-sigchild, this test can not be executed');
}
}

protected function getProcess($commandline, $cwd = null, array $env = null, $stdin = null, $timeout = 60, array $options = array())
{
return new Process($commandline, $cwd, $env, $stdin, $timeout, $options);
} }


public function testGetExitCode() public function testGetExitCode()
Expand Down Expand Up @@ -84,4 +78,19 @@ public function testIsNotSuccessful()
$this->skipIfPHPSigchild(); $this->skipIfPHPSigchild();
parent::testIsNotSuccessful(); parent::testIsNotSuccessful();
} }

/**
* {@inheritdoc}
*/
protected function getProcess($commandline, $cwd = null, array $env = null, $stdin = null, $timeout = 60, array $options = array())
{
return new Process($commandline, $cwd, $env, $stdin, $timeout, $options);
}

private function skipIfPHPSigchild()
{
if ($this->enabledSigchild) {
$this->markTestSkipped('Your PHP has been compiled with --enable-sigchild, this test can not be executed');
}
}
} }

0 comments on commit fa405bd

Please sign in to comment.