-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -991,14 +991,25 @@ public function checkTimeout() | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Sets the process pipes to use. | ||
| * | ||
| * @param ProcessPipes $pipes | ||
| */ | ||
| public function setProcessPipes(ProcessPipes $pipes) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. run php-cs-fixer |
||
| $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()) { | ||
|
|
| 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"; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?php | ||
|
|
||
|
|
||
| namespace Symfony\Component\Process\Tests; | ||
|
|
||
| use Symfony\Component\Process\NullProcessPipes; | ||
| use Symfony\Component\Process\Process; | ||
|
|
||
| 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'); | ||
| if (defined('PHP_WINDOWS_VERSION_BUILD')) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do have the special
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, My initial commit didn't support windows, the test was written for that - then i found this issue which had the windows friendly code, so it was added. Removed the skip. |
||
| $this->markTestSkipped('Windows does not support NullProcessPipes'); | ||
| } | ||
| $process->setProcessPipes(new NullProcessPipes()); | ||
| $process->start(); | ||
|
|
||
| while($process->isRunning()) { | ||
| usleep(100e3); | ||
| } | ||
|
|
||
| // No output! | ||
| $this->assertEquals('', $process->getOutput()); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra line break