Skip to content

Commit

Permalink
refactor(HasOptions): update getSerializedEscapedNormalizedOptions me…
Browse files Browse the repository at this point in the history
…thod

  - Update getSerializedEscapedNormalizedOptions method in the HasOptions trait
  - Add escape_argument function to support escaping shell arguments
  - Use escape_argument function in getSerializedEscapedNormalizedOptions method
  • Loading branch information
guanguans committed Jul 15, 2023
1 parent 37513d6 commit 231149d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
11 changes: 10 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.30.0@d0bc6e25d89f649e4f36a534f330f8bb4643dd69"/>
<files psalm-version="4.30.0@d0bc6e25d89f649e4f36a534f330f8bb4643dd69">
<file src="src/Support/helpers.php">
<UnusedClosureParam occurrences="1">
<code>$argument</code>
</UnusedClosureParam>
<InvalidScope occurrences="1">
<code>$this</code>
</InvalidScope>
</file>
</files>
18 changes: 16 additions & 2 deletions src/Concerns/HasOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1499,11 +1499,25 @@ public function getOption(string $key, $default = null)
return $this->options[$key] ?? $default;
}

public function getSerializedNormalizedOptions(): string
/**
* @throws InvalidOptionException
*/
public function getSerializedEscapedNormalizedOptions(): string
{
return implode(' ', $this->getNormalizedOptions());
return implode(' ', $this->getEscapedNormalizedOptions());
}

/**
* @throws InvalidOptionException
*/
public function getEscapedNormalizedOptions(): array
{
return array_map('escape_argument', $this->getNormalizedOptions());
}

/**
* @throws InvalidOptionException
*/
public function getNormalizedOptions(): array
{
return $this->normalizeOptions($this->options);
Expand Down
7 changes: 5 additions & 2 deletions src/Concerns/WithRunable.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ protected function exec($withOptions = [], ?callable $processTapper = null, ?cal
private function createProcess($withOptions = [], ?callable $processTapper = null): Process
{
$process = \is_string($withOptions)
? Process::fromShellCommandline("$this->soarPath {$this->getSerializedNormalizedOptions()} $withOptions")
: new Process(array_merge([$this->soarPath], $this->clone()->mergeOptions($withOptions)->getNormalizedOptions(), []));
? Process::fromShellCommandline(sprintf(
"%s {$this->getSerializedEscapedNormalizedOptions()} $withOptions",
escape_argument($this->soarPath)
))
: new Process(array_merge([$this->soarPath], $this->clone()->mergeOptions($withOptions)->getNormalizedOptions()));

if ($this->shouldApplySudoPassword()) {
$process = Process::fromShellCommandline(sprintf(
Expand Down
17 changes: 17 additions & 0 deletions src/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@
* This source file is subject to the MIT license that is bundled.
*/

use Guanguans\SoarPHP\Support\EscapeArg;
use Symfony\Component\Process\Process;

if (! function_exists('escape_argument')) {
/**
* Escapes a string to be used as a shell argument.
*/
function escape_argument(?string $argument): string
{
return method_exists(Process::class, 'escapeArgument')
? (function (?string $argument): string {
return $this->escapeArgument($argument);
})->call(new Process([]), $argument)
: EscapeArg::escape((string) $argument);
}
}

if (! function_exists('array_reduce_with_keys')) {
function array_reduce_with_keys(array $array, callable $callback, $carry = null)
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Concerns/HasOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ public function testGetNormalizedOptions(): void
$this->assertIsArray($soar->getNormalizedOptions());
}

public function testGetSerializedNormalizedOptions(): void
public function testGetSerializedEscapedNormalizedOptions(): void
{
$soar = Soar::create();

$this->assertIsString($soar->getSerializedNormalizedOptions());
$this->assertIsString($soar->getSerializedEscapedNormalizedOptions());
}

public function testGetOptions(): void
Expand Down

0 comments on commit 231149d

Please sign in to comment.