[Process] Deprecate Process::setStdin in favor of Process::setInput

This commit is contained in:
Romain Neutron 2014-05-17 20:36:38 +02:00
parent 75ccdb0e37
commit 53b9d737f1
6 changed files with 67 additions and 28 deletions

View File

@ -966,4 +966,8 @@ UPGRADE FROM 2.x to 3.0
``` ```
Yaml::parse(file_get_contents($fileName)); Yaml::parse(file_get_contents($fileName));
```
### Process
* Process::setStdin() and Process::getStdin() have been removed. Use
Process::setInput() and Process::getInput() that works the same way.

View File

@ -6,6 +6,8 @@ CHANGELOG
* added support for PTY mode * added support for PTY mode
* added the convenience method "mustRun" * added the convenience method "mustRun"
* deprecation: Process::setStdin() is deprecated in favor of Process::setInput()
* deprecation: Process::getStdin() is deprecated in favor of Process::getInput()
2.4.0 2.4.0
----- -----

View File

@ -45,7 +45,7 @@ class Process
private $commandline; private $commandline;
private $cwd; private $cwd;
private $env; private $env;
private $stdin; private $input;
private $starttime; private $starttime;
private $lastOutputTime; private $lastOutputTime;
private $timeout; private $timeout;
@ -128,7 +128,7 @@ class Process
* @param string $commandline The command line to run * @param string $commandline The command line to run
* @param string|null $cwd The working directory or null to use the working dir of the current PHP process * @param string|null $cwd The working directory or null to use the working dir of the current PHP process
* @param array|null $env The environment variables or null to inherit * @param array|null $env The environment variables or null to inherit
* @param string|null $stdin The STDIN content * @param string|null $input The input
* @param int|float|null $timeout The timeout in seconds or null to disable * @param int|float|null $timeout The timeout in seconds or null to disable
* @param array $options An array of options for proc_open * @param array $options An array of options for proc_open
* *
@ -136,7 +136,7 @@ class Process
* *
* @api * @api
*/ */
public function __construct($commandline, $cwd = null, array $env = null, $stdin = null, $timeout = 60, array $options = array()) public function __construct($commandline, $cwd = null, array $env = null, $input = null, $timeout = 60, array $options = array())
{ {
if (!function_exists('proc_open')) { if (!function_exists('proc_open')) {
throw new RuntimeException('The Process class relies on proc_open, which is not available on your PHP installation.'); throw new RuntimeException('The Process class relies on proc_open, which is not available on your PHP installation.');
@ -156,7 +156,7 @@ class Process
$this->setEnv($env); $this->setEnv($env);
} }
$this->stdin = $stdin; $this->input = $input;
$this->setTimeout($timeout); $this->setTimeout($timeout);
$this->useFileHandles = defined('PHP_WINDOWS_VERSION_BUILD'); $this->useFileHandles = defined('PHP_WINDOWS_VERSION_BUILD');
$this->pty = false; $this->pty = false;
@ -224,7 +224,7 @@ class Process
} }
/** /**
* Starts the process and returns after sending the STDIN. * Starts the process and returns after writing the input to STDIN.
* *
* This method blocks until all STDIN data is sent to the process then it * This method blocks until all STDIN data is sent to the process then it
* returns while the process runs in the background. * returns while the process runs in the background.
@ -288,7 +288,7 @@ class Process
return; return;
} }
$this->processPipes->write(false, $this->stdin); $this->processPipes->write(false, $this->input);
$this->updateStatus(false); $this->updateStatus(false);
$this->checkTimeout(); $this->checkTimeout();
} }
@ -1038,10 +1038,23 @@ class Process
* Gets the contents of STDIN. * Gets the contents of STDIN.
* *
* @return string|null The current contents * @return string|null The current contents
*
* @deprecated Deprecated since version 2.5, to be removed in 3.0.
* This method is deprecated in favor of getInput.
*/ */
public function getStdin() public function getStdin()
{ {
return $this->stdin; return $this->getInput();
}
/**
* Gets the Process input.
*
* @return null|string The Process input
*/
public function getInput()
{
return $this->input;
} }
/** /**
@ -1052,14 +1065,33 @@ class Process
* @return self The current Process instance * @return self The current Process instance
* *
* @throws LogicException In case the process is running * @throws LogicException In case the process is running
*
* @deprecated Deprecated since version 2.5, to be removed in 3.0.
* This method is deprecated in favor of setInput.
*/ */
public function setStdin($stdin) public function setStdin($stdin)
{
return $this->setInput($stdin);
}
/**
* Sets the input.
*
* This content will be passed to the underlying process standard input.
*
* @param string|null $input The content
*
* @return self The current Process instance
*
* @throws LogicException In case the process is running
*/
public function setInput($input)
{ {
if ($this->isRunning()) { if ($this->isRunning()) {
throw new LogicException('STDIN can not be set while the process is running.'); throw new LogicException('Input can not be set while the process is running.');
} }
$this->stdin = $stdin; $this->input = $input;
return $this; return $this;
} }

View File

@ -24,7 +24,7 @@ class ProcessBuilder
private $arguments; private $arguments;
private $cwd; private $cwd;
private $env = array(); private $env = array();
private $stdin; private $input;
private $timeout = 60; private $timeout = 60;
private $options = array(); private $options = array();
private $inheritEnv = true; private $inheritEnv = true;
@ -156,13 +156,13 @@ class ProcessBuilder
/** /**
* Sets the input of the process. * Sets the input of the process.
* *
* @param string $stdin The input as a string * @param string $input The input as a string
* *
* @return ProcessBuilder * @return ProcessBuilder
*/ */
public function setInput($stdin) public function setInput($input)
{ {
$this->stdin = $stdin; $this->input = $input;
return $this; return $this;
} }
@ -261,7 +261,7 @@ class ProcessBuilder
$env = $this->env; $env = $this->env;
} }
$process = new Process($script, $this->cwd, $env, $this->stdin, $this->timeout, $options); $process = new Process($script, $this->cwd, $env, $this->input, $this->timeout, $options);
if ($this->outputDisabled) { if ($this->outputDisabled) {
$process->disableOutput(); $process->disableOutput();

View File

@ -151,23 +151,23 @@ abstract class AbstractProcessTest extends \PHPUnit_Framework_TestCase
$expectedLength = (1024 * $size) + 1; $expectedLength = (1024 * $size) + 1;
$p = $this->getProcess(sprintf('php -r %s', escapeshellarg($code))); $p = $this->getProcess(sprintf('php -r %s', escapeshellarg($code)));
$p->setStdin($expected); $p->setInput($expected);
$p->run(); $p->run();
$this->assertEquals($expectedLength, strlen($p->getOutput())); $this->assertEquals($expectedLength, strlen($p->getOutput()));
$this->assertEquals($expectedLength, strlen($p->getErrorOutput())); $this->assertEquals($expectedLength, strlen($p->getErrorOutput()));
} }
public function testSetStdinWhileRunningThrowsAnException() public function testSetInputWhileRunningThrowsAnException()
{ {
$process = $this->getProcess('php -r "usleep(500000);"'); $process = $this->getProcess('php -r "usleep(500000);"');
$process->start(); $process->start();
try { try {
$process->setStdin('foobar'); $process->setInput('foobar');
$process->stop(); $process->stop();
$this->fail('A LogicException should have been raised.'); $this->fail('A LogicException should have been raised.');
} catch (LogicException $e) { } catch (LogicException $e) {
$this->assertEquals('STDIN can not be set while the process is running.', $e->getMessage()); $this->assertEquals('Input can not be set while the process is running.', $e->getMessage());
} }
$process->stop(); $process->stop();
} }
@ -993,6 +993,7 @@ abstract class AbstractProcessTest extends \PHPUnit_Framework_TestCase
array('WorkingDirectory'), array('WorkingDirectory'),
array('Env'), array('Env'),
array('Stdin'), array('Stdin'),
array('Input'),
array('Options') array('Options')
); );
@ -1000,14 +1001,14 @@ abstract class AbstractProcessTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @param string $commandline * @param string $commandline
* @param null $cwd * @param null|string $cwd
* @param array $env * @param null|array $env
* @param null $stdin * @param null|string $input
* @param int $timeout * @param int $timeout
* @param array $options * @param array $options
* *
* @return Process * @return Process
*/ */
abstract protected function getProcess($commandline, $cwd = null, array $env = null, $stdin = null, $timeout = 60, array $options = array()); abstract protected function getProcess($commandline, $cwd = null, array $env = null, $input = null, $timeout = 60, array $options = array());
} }

View File

@ -150,9 +150,9 @@ class SimpleProcessTest extends AbstractProcessTest
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function getProcess($commandline, $cwd = null, array $env = null, $stdin = null, $timeout = 60, array $options = array()) protected function getProcess($commandline, $cwd = null, array $env = null, $input = null, $timeout = 60, array $options = array())
{ {
return new Process($commandline, $cwd, $env, $stdin, $timeout, $options); return new Process($commandline, $cwd, $env, $input, $timeout, $options);
} }
private function skipIfPHPSigchild() private function skipIfPHPSigchild()