Skip to content

Commit e086669

Browse files
nicolas-grekaskeradus
authored andcommitted
Allow Symfony 5 + fix using the Process class (#3)
1 parent 6c42d7e commit e086669

7 files changed

+24
-21
lines changed

.php_cs.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ return PhpCsFixer\Config::create()
1515
'@PHPUnit60Migration:risky' => true,
1616
'@Symfony' => true,
1717
'@Symfony:risky' => true,
18-
'array_syntax' => array('syntax' => 'long'),
18+
'array_syntax' => array('syntax' => 'short'),
1919
'combine_consecutive_unsets' => true,
2020
// one should use PHPUnit methods to set up expected exception instead of annotations
2121
'general_phpdoc_annotation_remove' => array('annotations' => array('expectedException', 'expectedExceptionMessage', 'expectedExceptionMessageRegExp')),

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
}
1111
],
1212
"require": {
13-
"php": "^5.3.6 || ^7.0",
14-
"symfony/process": "^2.3 || ^3 || ^4"
13+
"php": "^5.5.9 || ^7.0",
14+
"symfony/process": "^3.4 || ^4 || ^5"
1515
},
1616
"require-dev": {
1717
"friendsofphp/php-cs-fixer": "^2.9",
1818
"maglnet/composer-require-checker": "^0.1.6",
1919
"phpunit/phpunit": "^5.7.27 || ^6.5.8 || ^7.1",
20-
"symfony/phpunit-bridge": "^4.0"
20+
"symfony/phpunit-bridge": "^4.0 || ^5.0"
2121
},
2222
"config": {
2323
"optimize-autoloader": true,

src/BashScriptExecutor.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class BashScriptExecutor
2828
public function __construct($scriptParts, $cwd)
2929
{
3030
@trigger_error('`\Keradus\CliExecutor\BashScriptExecutor` is deprecated, use `\Keradus\CliExecutor\ScriptExecutor` instead.', E_USER_DEPRECATED);
31-
$this->scriptExecutor = new ScriptExecutor($scriptParts, $cwd, array('#!/usr/bin/env bash', 'set -e', ''));
31+
$this->scriptExecutor = new ScriptExecutor($scriptParts, $cwd, ['#!/usr/bin/env bash', 'set -e', '']);
3232
}
3333

3434
/**

src/CommandExecutor.php

+7-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
final class CommandExecutor
1717
{
1818
/**
19-
* @var string
19+
* @var string|array
2020
*/
2121
private $command;
2222

@@ -31,8 +31,8 @@ final class CommandExecutor
3131
private $result;
3232

3333
/**
34-
* @param string $command
35-
* @param string $cwd
34+
* @param string|array $command
35+
* @param string $cwd
3636
*/
3737
public function __construct($command, $cwd)
3838
{
@@ -41,8 +41,8 @@ public function __construct($command, $cwd)
4141
}
4242

4343
/**
44-
* @param string $command
45-
* @param string $cwd
44+
* @param string|array $command
45+
* @param string $cwd
4646
*
4747
* @return self
4848
*/
@@ -61,8 +61,7 @@ public static function create($command, $cwd)
6161
public function getResult($checkCode = true)
6262
{
6363
if (null === $this->result) {
64-
// for symfony/process:^4.2
65-
if (method_exists(Process::class, 'fromShellCommandline')) {
64+
if (\is_string($this->command) && method_exists(Process::class, 'fromShellCommandline')) {
6665
$process = Process::fromShellCommandline($this->command, $this->cwd);
6766
} else {
6867
$process = new Process($this->command, $this->cwd);
@@ -81,7 +80,7 @@ public function getResult($checkCode = true)
8180
$this->result,
8281
sprintf(
8382
"Cannot execute `%s`:\nCode: %s\nExit text: %s\nError output: %s\nDetails:\n%s",
84-
$this->command,
83+
$process->getCommandLine(),
8584
$this->result->getCode(),
8685
isset(Process::$exitCodes[$this->result->getCode()]) ? Process::$exitCodes[$this->result->getCode()] : 'Unknown exit code',
8786
$this->result->getError(),

src/ScriptExecutor.php

+2-7
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __construct($scriptParts, $cwd, array $scriptInit = null)
5454
{
5555
$this->scriptParts = $scriptParts;
5656
$this->cwd = $cwd;
57-
$this->scriptInit = null !== $scriptInit ? $scriptInit : array('#!/bin/sh', 'set -eu', '');
57+
$this->scriptInit = null !== $scriptInit ? $scriptInit : ['#!/bin/sh', 'set -eu', ''];
5858
}
5959

6060
public function __destruct()
@@ -93,12 +93,7 @@ public function getResult($checkCode = true)
9393
chmod($this->tmpFilePath, 0777);
9494
$command = './'.$tmpFileName;
9595

96-
// for symfony/process:^4.2
97-
if (method_exists(Process::class, 'fromShellCommandline')) {
98-
$process = Process::fromShellCommandline($command, $this->cwd);
99-
} else {
100-
$process = new Process($command, $this->cwd);
101-
}
96+
$process = new Process([$command], $this->cwd);
10297
$process->run();
10398

10499
$this->result = new CliResult(

tests/CommandExecutorTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,13 @@ public function testSimpleExecution()
2929

3030
$this->assertContains(basename(__FILE__), $cliResult->getOutput());
3131
}
32+
33+
public function testSimpleExecutionWithArray()
34+
{
35+
$scriptExecutor = CommandExecutor::create(['ls', '-l'], __DIR__);
36+
37+
$cliResult = $scriptExecutor->getResult();
38+
39+
$this->assertContains(basename(__FILE__), $cliResult->getOutput());
40+
}
3241
}

tests/ScriptExecutorTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ScriptExecutorTest extends TestCase
2323
{
2424
public function testSimpleExecution()
2525
{
26-
$scriptExecutor = ScriptExecutor::create(array('ls'), __DIR__);
26+
$scriptExecutor = ScriptExecutor::create(['ls'], __DIR__);
2727

2828
$cliResult = $scriptExecutor->getResult();
2929

0 commit comments

Comments
 (0)