Skip to content

Commit 7bf58da

Browse files
authored
Merge pull request #6 from phpsu/task/add-shell-word
add shell word
2 parents e1c3886 + 783c141 commit 7bf58da

17 files changed

Lines changed: 736 additions & 127 deletions

src/Collection/AbstractCollection.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PHPSu\ShellCommandBuilder\Exception\ShellBuilderException;
88
use PHPSu\ShellCommandBuilder\ShellInterface;
99

10-
abstract class AbstractCollection implements CollectionInterface
10+
abstract class AbstractCollection implements ShellInterface
1111
{
1212
/** @var CollectionTuple|null */
1313
protected $tuple;
@@ -23,6 +23,18 @@ protected function toTuple($command, string $join): CollectionTuple
2323
return CollectionTuple::create($command, $join);
2424
}
2525

26+
/**
27+
* @return array<string|ShellInterface|array<mixed>>
28+
* @throws ShellBuilderException
29+
*/
30+
public function __toArray(): array
31+
{
32+
if ($this->tuple === null) {
33+
throw new ShellBuilderException('Tuple has not been set yet - collection cannot be parsed to array');
34+
}
35+
return $this->tuple->__toArray();
36+
}
37+
2638
public function __toString(): string
2739
{
2840
return (string)$this->tuple;

src/Collection/CollectionInterface.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Collection/Pipeline.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,22 @@ final class Pipeline extends AbstractCollection
1616
* @return $this
1717
* @throws ShellBuilderException
1818
*/
19-
public function pipe($command): self
19+
public static function pipe($command): self
2020
{
21-
$this->tuple = $this->toTuple($command, ControlOperator::PIPELINE);
22-
return $this;
21+
$pipeline = new self();
22+
$pipeline->tuple = $pipeline->toTuple($command, ControlOperator::PIPELINE);
23+
return $pipeline;
2324
}
2425

2526
/**
2627
* @param string|ShellInterface $command
2728
* @return $this
2829
* @throws ShellBuilderException
2930
*/
30-
public function pipeErrorForward($command): self
31+
public static function pipeErrorForward($command): self
3132
{
32-
$this->tuple = $this->toTuple($command, ControlOperator::PIPELINE_WITH_STDERR_FORWARD);
33-
return $this;
33+
$pipeline = new self();
34+
$pipeline->tuple = $pipeline->toTuple($command, ControlOperator::PIPELINE_WITH_STDERR_FORWARD);
35+
return $pipeline;
3436
}
3537
}

src/Collection/Redirection.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,35 @@ final class Redirection extends AbstractCollection
1616
* @return $this
1717
* @throws ShellBuilderException
1818
*/
19-
public function redirectOutput($value, bool $append): self
19+
public static function redirectOutput($value, bool $append): self
2020
{
21-
$this->tuple = CollectionTuple::create($value, $append ? RedirectOperator::STDOUT_LEFT_APPEND : RedirectOperator::STDOUT_LEFT_INSERT);
22-
return $this;
21+
$redirect = new self();
22+
$redirect->tuple = CollectionTuple::create($value, $append ? RedirectOperator::STDOUT_LEFT_APPEND : RedirectOperator::STDOUT_LEFT_INSERT);
23+
return $redirect;
2324
}
2425

2526
/**
2627
* @param string|ShellInterface $value
2728
* @return $this
2829
* @throws ShellBuilderException
2930
*/
30-
public function redirectInput($value): self
31+
public static function redirectInput($value): self
3132
{
32-
$this->tuple = CollectionTuple::create($value, RedirectOperator::STDIN_RIGHT);
33-
return $this;
33+
$redirect = new self();
34+
$redirect->tuple = CollectionTuple::create($value, RedirectOperator::STDIN_RIGHT);
35+
return $redirect;
3436
}
3537

3638
/**
3739
* @param string|ShellInterface $value
3840
* @return $this
3941
* @throws ShellBuilderException
4042
*/
41-
public function redirectError($value): self
43+
public static function redirectError($value): self
4244
{
43-
$this->tuple = CollectionTuple::create($value, RedirectOperator::FILE_DESCRIPTOR_ERR . RedirectOperator::STDOUT_LEFT_INSERT);
44-
return $this;
45+
$redirect = new self();
46+
$redirect->tuple = CollectionTuple::create($value, RedirectOperator::FILE_DESCRIPTOR_ERR . RedirectOperator::STDOUT_LEFT_INSERT);
47+
return $redirect;
4548
}
4649

4750
/**
@@ -50,15 +53,17 @@ public function redirectError($value): self
5053
* @return $this
5154
* @throws ShellBuilderException
5255
*/
53-
public function redirectBetweenFiles($value, bool $toLeft): self
56+
public static function redirectBetweenFiles($value, bool $toLeft): self
5457
{
55-
$this->tuple = CollectionTuple::create($value, $toLeft ? RedirectOperator::REDIRECT_LEFT : RedirectOperator::REDIRECT_RIGHT);
56-
return $this;
58+
$redirect = new self();
59+
$redirect->tuple = CollectionTuple::create($value, $toLeft ? RedirectOperator::REDIRECT_LEFT : RedirectOperator::REDIRECT_RIGHT);
60+
return $redirect;
5761
}
5862

59-
public function redirectErrorToOutput(): self
63+
public static function redirectErrorToOutput(): self
6064
{
61-
$this->tuple = CollectionTuple::create('', RedirectOperator::ERR_TO_OUT_REDIRECT);
62-
return $this;
65+
$redirect = new self();
66+
$redirect->tuple = CollectionTuple::create('', RedirectOperator::ERR_TO_OUT_REDIRECT);
67+
return $redirect;
6368
}
6469
}

src/Collection/ShellList.php

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,58 @@
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
1719
*/
18-
public function addOr($command): self
20+
public static function addOr($command): self
1921
{
20-
$this->tuple = $this->toTuple($command, ControlOperator::OR_OPERATOR);
21-
return $this;
22+
$list = new self();
23+
$list->tuple = $list->toTuple($command, ControlOperator::OR_OPERATOR);
24+
return $list;
2225
}
2326

2427
/**
28+
* Returns something like: && echo "hello world"
29+
*
2530
* @param string|ShellInterface $command
2631
* @return $this
2732
* @throws ShellBuilderException
2833
*/
29-
public function addAnd($command): self
34+
public static function addAnd($command): self
3035
{
31-
$this->tuple = $this->toTuple($command, ControlOperator::AND_OPERATOR);
32-
return $this;
36+
$list = new self();
37+
$list->tuple = $list->toTuple($command, ControlOperator::AND_OPERATOR);
38+
return $list;
3339
}
3440

3541
/**
42+
* Returns something like: ; echo "hello world"
43+
*
3644
* @param string|ShellInterface $command
3745
* @return $this
3846
* @throws ShellBuilderException
3947
*/
40-
public function add($command): self
48+
public static function add($command): self
4149
{
42-
$this->tuple = $this->toTuple($command, ControlOperator::COMMAND_DELIMITER);
43-
return $this;
50+
$list = new self();
51+
$list->tuple = $list->toTuple($command, ControlOperator::COMMAND_DELIMITER);
52+
return $list;
53+
}
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;
4467
}
4568
}

src/Literal/ShellArgument.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPSu\ShellCommandBuilder\Literal;
6+
7+
use PHPSu\ShellCommandBuilder\Exception\ShellBuilderException;
8+
use PHPSu\ShellCommandBuilder\ShellInterface;
9+
10+
final class ShellArgument extends ShellWord
11+
{
12+
protected $isArgument = true;
13+
protected $delimiter = '';
14+
15+
/**
16+
* ShellArgument constructor.
17+
* @param ShellInterface|string $argument
18+
*/
19+
public function __construct($argument)
20+
{
21+
parent::__construct('', $argument);
22+
}
23+
24+
protected function validate(): void
25+
{
26+
if (is_string($this->value) && empty($this->value)) {
27+
throw new ShellBuilderException('Argument cant be empty');
28+
}
29+
parent::validate();
30+
}
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPSu\ShellCommandBuilder\Literal;
6+
7+
use PHPSu\ShellCommandBuilder\ShellInterface;
8+
9+
final class ShellEnvironmentVariable extends ShellWord
10+
{
11+
protected $isEnvironmentVariable = true;
12+
protected $useAssignOperator = true;
13+
protected $nameUpperCase = true;
14+
15+
/**
16+
* ShellArgument constructor.
17+
* @param string $option
18+
* @param ShellInterface|string $value
19+
*/
20+
public function __construct(string $option, $value)
21+
{
22+
parent::__construct($option, $value);
23+
}
24+
}

src/Literal/ShellExecutable.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPSu\ShellCommandBuilder\Literal;
6+
7+
final class ShellExecutable extends ShellWord
8+
{
9+
protected $isArgument = true;
10+
protected $spaceAfterValue = false;
11+
protected $isEscaped = false;
12+
protected $delimiter = '';
13+
protected $prefix = '';
14+
protected $suffix = '';
15+
16+
public function __construct(string $executable)
17+
{
18+
parent::__construct($executable);
19+
}
20+
}

src/Literal/ShellOption.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPSu\ShellCommandBuilder\Literal;
6+
7+
use PHPSu\ShellCommandBuilder\ShellInterface;
8+
9+
final class ShellOption extends ShellWord
10+
{
11+
protected $isOption = true;
12+
protected $prefix = ShellWord::OPTION_CONTROL;
13+
14+
/**
15+
* ShellArgument constructor.
16+
* @param string $option
17+
* @param ShellInterface|string $value
18+
*/
19+
public function __construct(string $option, $value = '')
20+
{
21+
if (is_string($value) && empty($value)) {
22+
$this->delimiter = '';
23+
}
24+
parent::__construct($option, $value);
25+
}
26+
}

src/Literal/ShellShortOption.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPSu\ShellCommandBuilder\Literal;
6+
7+
use PHPSu\ShellCommandBuilder\ShellInterface;
8+
9+
final class ShellShortOption extends ShellWord
10+
{
11+
protected $isShortOption = true;
12+
protected $prefix = ShellWord::SHORT_OPTION_CONTROL;
13+
14+
/**
15+
* ShellArgument constructor.
16+
* @param string $option
17+
* @param ShellInterface|string $value
18+
*/
19+
public function __construct(string $option, $value)
20+
{
21+
if (is_string($value) && empty($value)) {
22+
$this->delimiter = '';
23+
}
24+
parent::__construct($option, $value);
25+
}
26+
}

0 commit comments

Comments
 (0)