Skip to content

Commit 783c141

Browse files
author
Christian Benthake
committed
documentation for shell list
1 parent f63e1ed commit 783c141

5 files changed

Lines changed: 56 additions & 13 deletions

File tree

src/Collection/ShellList.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
final class ShellList extends AbstractCollection
1212
{
1313
/**
14+
* Returns something like: || echo "hello world"
15+
*
1416
* @param string|ShellInterface $command
1517
* @return $this
1618
* @throws ShellBuilderException
@@ -23,6 +25,8 @@ public static function addOr($command): self
2325
}
2426

2527
/**
28+
* Returns something like: && echo "hello world"
29+
*
2630
* @param string|ShellInterface $command
2731
* @return $this
2832
* @throws ShellBuilderException
@@ -35,6 +39,8 @@ public static function addAnd($command): self
3539
}
3640

3741
/**
42+
* Returns something like: ; echo "hello world"
43+
*
3844
* @param string|ShellInterface $command
3945
* @return $this
4046
* @throws ShellBuilderException
@@ -45,4 +51,18 @@ public static function add($command): self
4551
$list->tuple = $list->toTuple($command, ControlOperator::COMMAND_DELIMITER);
4652
return $list;
4753
}
54+
55+
/**
56+
* Returns something like: & echo "hello world"
57+
*
58+
* @param string|ShellInterface $command
59+
* @return static
60+
* @throws ShellBuilderException
61+
*/
62+
public static function async($command): self
63+
{
64+
$list = new self();
65+
$list->tuple = $list->toTuple($command, ControlOperator::BASH_AMPERSAND);
66+
return $list;
67+
}
4868
}

src/ShellBuilder.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ public function add($command): self
6161
*/
6262
public function and($command): self
6363
{
64-
if (is_string($command)) {
65-
$command = $this->createCommand($command);
66-
}
6764
$this->commandList[] = ShellList::addAnd($this->parseCommand($command));
6865
return $this;
6966
}
@@ -75,23 +72,28 @@ public function and($command): self
7572
*/
7673
public function or($command): self
7774
{
78-
if (is_string($command)) {
79-
$command = $this->createCommand($command);
80-
}
8175
$this->commandList[] = ShellList::addOr($this->parseCommand($command));
8276
return $this;
8377
}
8478

79+
/**
80+
* @param string|ShellInterface $command
81+
* @return $this
82+
* @throws ShellBuilderException
83+
*/
84+
public function async($command): self
85+
{
86+
$this->commandList[] = ShellList::async($this->parseCommand($command));
87+
return $this;
88+
}
89+
8590
/**
8691
* @param string|ShellInterface $command
8792
* @return $this
8893
* @throws ShellBuilderException
8994
*/
9095
public function pipe($command): self
9196
{
92-
if (is_string($command)) {
93-
$command = $this->createCommand($command);
94-
}
9597
$this->commandList[] = Pipeline::pipe($this->parseCommand($command));
9698
return $this;
9799
}
@@ -103,9 +105,6 @@ public function pipe($command): self
103105
*/
104106
public function pipeWithForward($command): self
105107
{
106-
if (is_string($command)) {
107-
$command = $this->createCommand($command);
108-
}
109108
$this->commandList[] = Pipeline::pipeErrorForward($this->parseCommand($command));
110109
return $this;
111110
}

src/ShellCommand.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ final class ShellCommand implements ShellInterface
2727
private $isCommandSubstitution = false;
2828
/** @var ShellBuilder|null */
2929
private $parentBuilder;
30+
/** @var bool */
31+
private $invertOutput = false;
3032

3133
public function __construct(string $name, ShellBuilder $builder = null)
3234
{
@@ -48,6 +50,12 @@ public function toggleCommandSubstitution(): self
4850
return $this;
4951
}
5052

53+
public function invert(bool $invert = true): self
54+
{
55+
$this->invertOutput = $invert;
56+
return $this;
57+
}
58+
5159
/**
5260
* @param string $option
5361
* @param ShellInterface|string|mixed $value
@@ -183,7 +191,8 @@ public function __toString(): string
183191
{
184192
/** @psalm-suppress ImplicitToStringCast */
185193
$result = (sprintf(
186-
'%s%s%s',
194+
'%s%s%s%s',
195+
$this->invertOutput ? '! ' : '',
187196
empty($this->environmentVariables) ? '' : $this->environmentVariablesToString(),
188197
$this->executable,
189198
empty($this->arguments) ? '' : ' ' . $this->argumentsToString()

tests/ShellBuilderTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@ public function testAsyncCommandBuilder(): void
221221
$this->assertEquals("coproc echo 'hello'", (string)$builder);
222222
}
223223

224+
public function testAsyncListCommandBuilder(): void
225+
{
226+
$builder = new ShellBuilder();
227+
$builder->createCommand('echo')->addArgument('hello')->addToBuilder()
228+
->async('ls');
229+
$this->assertEquals("echo 'hello' & ls", (string)$builder);
230+
}
231+
224232
public function testRedirectToAppend(): void
225233
{
226234
$builder = new ShellBuilder();

tests/ShellCommandTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ public function testShellCommandWithCommandSubstitution(): void
4747
$this->assertEquals("ls -ld $(date +%B).txt", (string)$command);
4848
}
4949

50+
public function testShellCommandWithInvertedOutput(): void
51+
{
52+
$command = new ShellCommand('echo');
53+
$command->invert()->addShortOption('e', 'hello world');
54+
$this->assertEquals('! echo -e \'hello world\'', (string)$command);
55+
}
56+
5057
public function testEscapeOptionWithAssignOperator(): void
5158
{
5259
$command = (string)(new ShellCommand('ls'))->addOption('color', 'true', true, true);

0 commit comments

Comments
 (0)