Skip to content

[Process] Redirect output without a shell #9726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/Symfony/Component/Process/NullProcessPipes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Process;

use Symfony\Component\Process\Exception\RuntimeException;

/**
* NullProcessPipes allow redirecting output to dev/null without a subshell. Useful for processes that communicate
* over other means.
*/
class NullProcessPipes extends ProcessPipes
{
/**
* Returns an array of descriptors for the use of proc_open.
*
* @return array
*/
public function getDescriptors()
{
$nullfile = defined('PHP_WINDOWS_VERSION_BUILD') ? 'NUL' : '/dev/null';
return array(

array('pipe', 'r'), // stdin
array('file', $nullfile, 'a+'), // stdout
array('file', $nullfile, 'a+'), //stderr
);
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra line break

18 changes: 17 additions & 1 deletion src/Symfony/Component/Process/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -991,14 +991,30 @@ public function checkTimeout()
}
}

/**
* Sets the process pipes to use.
*
* @param ProcessPipes $pipes
* @throws LogicException If called after start()
*/
public function setProcessPipes(ProcessPipes $pipes)
{
if ($this->status !== self::STATUS_READY) {
throw new LogicException('ProcessPipes cannot be changed after the process has started');
}
$this->processPipes = $pipes;
}

/**
* Creates the descriptors needed by the proc_open.
*
* @return array
*/
private function getDescriptors()
{
$this->processPipes = new ProcessPipes($this->useFileHandles);
if (!$this->processPipes) {
$this->processPipes = new ProcessPipes($this->useFileHandles);
}
$descriptors = $this->processPipes->getDescriptors();

if (!$this->useFileHandles && $this->enhanceSigchildCompatibility && $this->isSigchildEnabled()) {
Expand Down
22 changes: 21 additions & 1 deletion src/Symfony/Component/Process/ProcessBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ProcessBuilder
private $options = array();
private $inheritEnv = true;
private $prefix = array();
private $processPipes;

public function __construct(array $arguments = array())
{
Expand Down Expand Up @@ -117,6 +118,20 @@ public function setInput($stdin)
return $this;
}

/**
* Sets the process pipes to use for the new process
*
* @param ProcessPipes $pipes
*
* @return ProcessBuilder
*/
public function setProcessPipes(ProcessPipes $pipes)
{
$this->processPipes = $pipes;

return $this;
}

/**
* Sets the process timeout.
*
Expand Down Expand Up @@ -172,6 +187,11 @@ public function getProcess()
$env = $this->env;
}

return new Process($script, $this->cwd, $env, $this->stdin, $this->timeout, $options);
$process = new Process($script, $this->cwd, $env, $this->stdin, $this->timeout, $options);
if ($this->processPipes) {
$process->setProcessPipes($this->processPipes);
}

return $process;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

/**
* Runs a php script that will dump a large amount of output and then quit.
*/

for ($i = 0; $i < 100000; $i++) {
echo "Lorem ipsum dolor sit amet\n";
}
47 changes: 47 additions & 0 deletions src/Symfony/Component/Process/Tests/NullProcessPipesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Symfony\Component\Process\Tests;

use Symfony\Component\Process\NullProcessPipes;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessPipes;

class NullProcessPipesTest extends \PHPUnit_Framework_TestCase
{
public function testProcessCompletesWithNullPipes()
{
// Without null pipes the pipe would fill and the process will never complete
$process = new Process('php ' . __DIR__ . '/HeavyOutputtingProcess.php');

$process->setProcessPipes(new NullProcessPipes());
$process->start();

while ($process->isRunning()) {
usleep(100e3);
}

// No output!
$this->assertEquals('', $process->getOutput());
}

public function testReassigningPipesAfterStartIsNotAllowed()
{
$process = new Process('php -r "sleep(1);');

$process->setProcessPipes(new NullProcessPipes());
$process->start();

$this->setExpectedException('Symfony\Component\Process\Exception\LogicException');
$process->setProcessPipes(new ProcessPipes());
}

public function testRestartedProcesses() {
$process = new Process('echo asdf');

$process->run();
$this->assertEquals("asdf\n", $process->getOutput());

$process->run();
$this->assertEquals("asdf\n", $process->getOutput());
}
}