Skip to content

Commit f502892

Browse files
author
Christian Benthake
committed
BC: add conditionals for builder and command
1 parent 3cc213d commit f502892

6 files changed

Lines changed: 106 additions & 12 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"license": "MIT",
1818
"authors": [
1919
{
20-
"name": "Chris Ben",
20+
"name": "Christian Rodriguez Benthake",
2121
"email": "git@cben.co"
2222
}
2323
],

src/ShellBuilder.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
final class ShellBuilder implements ShellInterface, \JsonSerializable
1919
{
20+
use ShellConditional;
21+
2022
/** @var array<ShellInterface> */
2123
private $commandList = [];
2224
/** @var int */
@@ -282,7 +284,7 @@ public function createCommandSubstition(): self
282284

283285
public function hasCommands(): bool
284286
{
285-
return empty($this->commandList) && empty($this->variables);
287+
return empty($this->commandList) === false || empty($this->variables) === false;
286288
}
287289

288290
/**
@@ -352,7 +354,7 @@ public function __toString(): string
352354
}
353355
if ($this->groupType === GroupType::SAMESHELL_GROUP) {
354356
return sprintf(
355-
'%s%s;%s',
357+
'%s %s;%s',
356358
ControlOperator::CURLY_BLOCK_DEFINITON_OPEN,
357359
$result,
358360
ControlOperator::CURLY_BLOCK_DEFINITON_CLOSE

src/ShellCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
final class ShellCommand implements ShellInterface
2020
{
21+
use ShellConditional;
22+
2123
/**
2224
* @var ShellWord
2325
* @psalm-readonly

src/ShellConditional.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPSu\ShellCommandBuilder;
6+
7+
trait ShellConditional
8+
{
9+
public function if(bool $condition, callable $callback, callable $alternativeCallback = null): self
10+
{
11+
if ($condition) {
12+
$result = $callback($this);
13+
assert($result instanceof self || $result === null);
14+
return $result ?? $this;
15+
}
16+
if ($alternativeCallback) {
17+
$alternativeResult = $alternativeCallback($this);
18+
assert($alternativeResult instanceof self || $alternativeResult === null);
19+
return $alternativeResult ?? $this;
20+
}
21+
return $this;
22+
}
23+
24+
public function ifThis(callable $callOnThis, callable $callback, callable $alternativeCallback = null): self
25+
{
26+
return $this->if($callOnThis($this) === true, $callback, $alternativeCallback);
27+
}
28+
}

tests/ShellBuilderTest.php

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final class ShellBuilderTest extends TestCase
1717
{
1818
public function testBuilderConcept(): void
1919
{
20-
$result = 'a && b | c || d |& f && (g && h) || {i || j;}';
20+
$result = 'a && b | c || d |& f && (g && h) || { i || j;}';
2121
$builder = new ShellBuilder();
2222
$a = $builder->createCommand('a');
2323
$b = $builder->createCommand('b');
@@ -46,7 +46,7 @@ public function testBuilderConcept(): void
4646

4747
public function testBuilderConceptWithShortcut(): void
4848
{
49-
$result = 'a && b | c || d |& f && (g && h) || {i || j;}';
49+
$result = 'a && b | c || d |& f && (g && h) || { i || j;}';
5050
$builder = new ShellBuilder();
5151
$builder
5252
->add('a')
@@ -146,7 +146,7 @@ public function testShellBuilderGroupSameShell(): void
146146
->add(
147147
$builder->createCommand('echo')->addArgument('hello')
148148
)->and('cat'));
149-
$this->assertEquals("{echo 'hello' && cat;}", (string)$builder);
149+
$this->assertEquals("{ echo 'hello' && cat;}", (string)$builder);
150150
}
151151

152152
public function testSimpleSshCommand(): void
@@ -669,7 +669,7 @@ public function testCoprocessWithShellGroupAndRedirections(): void
669669
->addToBuilder()
670670
->redirectDescriptor('', true, null, 3);
671671
$this->assertEquals(
672-
'{coproc tee {tee logfile;} >&3 ;} 3>&1',
672+
'{ coproc tee { tee logfile;} >&3 ;} 3>&1',
673673
(string)ShellBuilder::new()->add($builder)->redirectDescriptor('', true, 3, 1)
674674
);
675675
}
@@ -686,11 +686,60 @@ public function testNamedCoprocessWithShellGroupAndRedirections(): void
686686
->addToBuilder())
687687
->redirectDescriptor('', true, null, 3);
688688
$this->assertEquals(
689-
'{coproc mycoproc {awk \'{print "foo" $0;fflush()}\';} >&3 ;} 3>&1',
689+
'{ coproc mycoproc { awk \'{print "foo" $0;fflush()}\';} >&3 ;} 3>&1',
690690
(string)ShellBuilder::new()->add($builder)->redirectDescriptor('', true, 3, 1)
691691
);
692692
}
693693

694+
public function testCondiditionalArguments(): void
695+
{
696+
// if false
697+
$builder = ShellBuilder::new()
698+
->if(false, static function (ShellBuilder $builder) {
699+
return $builder->add('echo');
700+
})
701+
->ifThis(static function (ShellBuilder $builder) {
702+
return $builder->hasCommands() === false;
703+
}, static function (ShellBuilder $builder) {
704+
return $builder->add('print');
705+
});
706+
static::assertEquals('print', (string)$builder);
707+
708+
// if true
709+
$builder = ShellBuilder::new()
710+
->if(true, static function (ShellBuilder $builder) {
711+
return $builder->add('echo');
712+
})
713+
->ifThis(static function (ShellBuilder $builder) {
714+
return $builder->hasCommands() === false;
715+
}, static function (ShellBuilder $builder) {
716+
return $builder->add('print');
717+
});
718+
static::assertEquals('echo', (string)$builder);
719+
}
720+
721+
public function testComplexCondiditionalArguments(): void
722+
{
723+
$builder = ShellBuilder::new()
724+
->if(
725+
false,
726+
static function (ShellBuilder $builder) {
727+
return $builder->add('echo');
728+
},
729+
static function (ShellBuilder $builder) {
730+
return $builder->add('awk');
731+
}
732+
)
733+
->ifThis(static function (ShellBuilder $builder) {
734+
return $builder->hasCommands() === false;
735+
}, static function (ShellBuilder $builder) {
736+
return $builder->add('print');
737+
}, static function (ShellBuilder $builder) {
738+
return $builder->and('print');
739+
});
740+
static::assertEquals('awk && print', (string)$builder);
741+
}
742+
694743
public function testSimpleAsyncShellBuilder(): void
695744
{
696745
$this->assertEquals(
@@ -775,18 +824,18 @@ public function testCommandVariableWithConditionalAndCommand(): void
775824
public function testShellBuilderIsEmpty(): void
776825
{
777826
$builder = ShellBuilder::new();
778-
$this->assertTrue($builder->hasCommands());
827+
$this->assertFalse($builder->hasCommands());
779828
}
780829

781830
public function testShellBuilderIsNotEmpty(): void
782831
{
783832
$builder = ShellBuilder::new();
784833
$builder->addVariable('a', 'b');
785-
$this->assertFalse($builder->hasCommands());
786-
$builder->removeVariable('a');
787834
$this->assertTrue($builder->hasCommands());
788-
$builder->add('echo');
835+
$builder->removeVariable('a');
789836
$this->assertFalse($builder->hasCommands());
837+
$builder->add('echo');
838+
$this->assertTrue($builder->hasCommands());
790839
}
791840

792841
public function testAddVariableWithoutSemicolon(): void

tests/ShellCommandTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPSu\ShellCommandBuilder\Definition\GroupType;
88
use PHPSu\ShellCommandBuilder\Exception\ShellBuilderException;
9+
use PHPSu\ShellCommandBuilder\ShellBuilder;
910
use PHPSu\ShellCommandBuilder\ShellCommand;
1011
use PHPUnit\Framework\TestCase;
1112

@@ -171,6 +172,18 @@ public function testShellCommandWithCommandSubstitutionToArray(): void
171172
], $command['arguments']);
172173
}
173174

175+
public function testConditionalArguments()
176+
{
177+
$command = ShellBuilder::command('test')
178+
->if(1 + 1 === 3, static function (ShellCommand $command) {
179+
return $command->addOption('f', '1 + 1 = 3');
180+
})
181+
->if(1 + 1 === 2, static function (ShellCommand $command) {
182+
return $command->addOption('t', '1 + 1 = 2');
183+
});
184+
static::assertEquals((string)$command, 'test --t \'1 + 1 = 2\'');
185+
}
186+
174187
public function testUnEscapedOption(): void
175188
{
176189
$command = (new ShellCommand('ls'))->addOption('color', 'true', false, true);

0 commit comments

Comments
 (0)