tigitz /
php-spellchecker
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace PhpSpellCheck\Utils; |
||
| 6 | |||
| 7 | use PhpSpellCheck\Exception\InvalidArgumentException; |
||
| 8 | |||
| 9 | class CommandLine |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var array |
||
| 13 | */ |
||
| 14 | private $commandArgs; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @param string|array $command |
||
| 18 | */ |
||
| 19 | 19 | public function __construct($command) |
|
| 20 | { |
||
| 21 | 19 | if (is_array($command)) { |
|
| 22 | 18 | $this->commandArgs = $command; |
|
| 23 | } else { |
||
| 24 | 16 | if (is_string($command)) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 25 | 15 | $this->commandArgs = [$command]; |
|
| 26 | } else { |
||
| 27 | 1 | throw new InvalidArgumentException( |
|
| 28 | 1 | \Safe\sprintf( |
|
| 29 | 1 | 'Command should be an "array" or a "string", "%s" given', |
|
| 30 | 1 | is_object($command) ? get_class($command) : gettype($command) |
|
| 31 | ) |
||
| 32 | ); |
||
| 33 | } |
||
| 34 | } |
||
| 35 | 18 | } |
|
| 36 | |||
| 37 | 14 | public function addArg(string $arg): self |
|
| 38 | { |
||
| 39 | 14 | $args = $this->commandArgs; |
|
| 40 | 14 | $args[] = $arg; |
|
| 41 | |||
| 42 | 14 | return new self($args); |
|
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param string[] $argsToAdd |
||
| 47 | */ |
||
| 48 | 8 | public function addArgs(iterable $argsToAdd): self |
|
| 49 | { |
||
| 50 | 8 | $args = $this->commandArgs; |
|
| 51 | |||
| 52 | 8 | foreach ($argsToAdd as $arg) { |
|
| 53 | 8 | $args[] = $arg; |
|
| 54 | } |
||
| 55 | |||
| 56 | 8 | return new self($args); |
|
| 57 | } |
||
| 58 | |||
| 59 | 16 | public function getArgs(): array |
|
| 60 | { |
||
| 61 | 16 | return $this->commandArgs; |
|
| 62 | } |
||
| 63 | |||
| 64 | 1 | public function asString(): string |
|
| 65 | { |
||
| 66 | 1 | return implode(' ', array_map([$this, 'escapeArgument'], $this->commandArgs)); |
|
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Escapes a string to be used as a shell argument. |
||
| 71 | */ |
||
| 72 | 1 | //@codingStandardsIgnoreLine SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedMethod |
|
| 73 | private function escapeArgument(string $argument): string |
||
| 74 | 1 | { |
|
| 75 | 1 | if ('\\' !== \DIRECTORY_SEPARATOR) { |
|
| 76 | return "'" . str_replace("'", "'\\''", $argument) . "'"; |
||
| 77 | } |
||
| 78 | if ('' === $argument) { |
||
| 79 | return '""'; |
||
| 80 | } |
||
| 81 | if (false !== strpos($argument, "\0")) { |
||
| 82 | $argument = str_replace("\0", '?', $argument); |
||
| 83 | } |
||
| 84 | if (!\Safe\preg_match('/[\/()%!^"<>&|\s]/', $argument)) { |
||
| 85 | return $argument; |
||
| 86 | } |
||
| 87 | $argument = \Safe\preg_replace('/(\\\\+)$/', '$1$1', $argument); |
||
| 88 | |||
| 89 | return '"' . str_replace(['"', '^', '%', '!', "\n"], ['""', '"^^"', '"^%"', '"^!"', '!LF!'], $argument) . '"'; |
||
| 90 | } |
||
| 91 | //@codingStandardsIgnoreEnd |
||
| 92 | } |
||
| 93 |