generated from ergebnis/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change PopenBased finders to ProcOpenBased finders (#75)
- Loading branch information
Showing
19 changed files
with
179 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Fidry CPUCounter Config package. | ||
* | ||
* (c) Théo FIDRY <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fidry\CpuCoreCounter\Exec; | ||
|
||
use function fclose; | ||
use function is_resource; | ||
use function proc_close; | ||
use function proc_open; | ||
use function stream_get_contents; | ||
|
||
final class ProcOpen | ||
{ | ||
/** | ||
* @return array{string, string} STDOUT & STDERR tuple | ||
*/ | ||
public static function execute(string $command): ?array | ||
{ | ||
$pipes = []; | ||
|
||
$process = @proc_open( | ||
$command, | ||
[ | ||
['pipe', 'rb'], | ||
['pipe', 'wb'], // stdout | ||
['pipe', 'wb'], // stderr | ||
], | ||
$pipes | ||
); | ||
|
||
if (!is_resource($process)) { | ||
return null; | ||
} | ||
|
||
fclose($pipes[0]); | ||
|
||
$stdout = (string) stream_get_contents($pipes[1]); | ||
$stderr = (string) stream_get_contents($pipes[2]); | ||
|
||
proc_close($process); | ||
|
||
return [$stdout, $stderr]; | ||
} | ||
|
||
private function __construct() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Fidry CPUCounter Config package. | ||
* | ||
* (c) Théo FIDRY <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fidry\CpuCoreCounter\Test\Exec; | ||
|
||
use Fidry\CpuCoreCounter\Exec\ProcOpen; | ||
use PHPUnit\Framework\TestCase; | ||
use const PHP_EOL; | ||
|
||
/** | ||
* @covers \Fidry\CpuCoreCounter\Exec\ProcOpen | ||
* | ||
* @internal | ||
*/ | ||
final class ProcOpenTest extends TestCase | ||
{ | ||
public function test_it_can_execute_a_command_writing_output_to_the_stdout(): void | ||
{ | ||
$command = 'echo "Hello world!"'; | ||
|
||
$expected = ['Hello world!'.PHP_EOL, '']; | ||
$actual = ProcOpen::execute($command); | ||
|
||
self::assertSame($expected, $actual); | ||
} | ||
|
||
public function test_it_can_execute_a_command_writing_output_to_the_stderr_instead_of_the_stdout(): void | ||
{ | ||
$command = 'echo "Hello world!" 1>&2'; | ||
|
||
$expected = ['', 'Hello world!'.PHP_EOL]; | ||
$actual = ProcOpen::execute($command); | ||
|
||
self::assertSame($expected, $actual); | ||
} | ||
|
||
public function test_it_can_execute_a_command_writing_output_to_the_stdout_instead_of_the_stderr(): void | ||
{ | ||
$command = 'echoerr() { echo "$@" 1>&2; }; echoerr "Hello world!" 2>&1'; | ||
|
||
$expected = ['Hello world!'.PHP_EOL, '']; | ||
$actual = ProcOpen::execute($command); | ||
|
||
self::assertSame($expected, $actual); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.