Skip to content
Merged
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
9 changes: 8 additions & 1 deletion library/std/exec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ function swoole_exec(string $command, &$output = null, &$returnVar = null)
$result = Swoole\Coroutine::exec($command);
if ($result) {
$outputList = explode(PHP_EOL, $result['output']);
foreach ($outputList as &$value) {
$value = rtrim($value);
}
if ('' === ($endLine = end($outputList))) {
array_pop($outputList);
$endLine = end($outputList);
}
if ($output) {
$output = array_merge($output, $outputList);
} else {
$output = $outputList;
}
$returnVar = $result['code'];
return end($outputList);
return $endLine;
} else {
return false;
}
Expand Down
9 changes: 8 additions & 1 deletion php_swoole_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@ static const char* swoole_library_source_std_exec =
" $result = Swoole\\Coroutine::exec($command);\n"
" if ($result) {\n"
" $outputList = explode(PHP_EOL, $result['output']);\n"
" foreach ($outputList as &$value) {\n"
" $value = rtrim($value);\n"
" }\n"
" if ('' === ($endLine = end($outputList))) {\n"
" array_pop($outputList);\n"
" $endLine = end($outputList);\n"
" }\n"
" if ($output) {\n"
" $output = array_merge($output, $outputList);\n"
" } else {\n"
" $output = $outputList;\n"
" }\n"
" $returnVar = $result['code'];\n"
" return end($outputList);\n"
" return $endLine;\n"
" } else {\n"
" return false;\n"
" }\n"
Expand Down
23 changes: 23 additions & 0 deletions tests/swoole_library/exec/exec/2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
swoole_library/exec/exec: Fix $output result inconsistency
--SKIPIF--
<?php
require __DIR__ . '/../../../include/skipif.inc';
?>
--FILE--
<?php
require __DIR__ . '/../../../include/bootstrap.php';

$fileName = __DIR__ . '/exec_test.php';
file_put_contents($fileName, "1 \r\n2\r\n3\r\n");

exec('php ' . $fileName, $output1);

Swoole\Runtime::enableCoroutine();
go(function () use ($output1, $fileName) {
exec('php ' . $fileName, $output2);
Assert::same($output2, $output1);
});

?>
--EXPECT--