1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PHPSu\ShellCommandBuilder\Collection; |
6
|
|
|
|
7
|
|
|
use PHPSu\ShellCommandBuilder\Definition\RedirectOperator; |
8
|
|
|
use PHPSu\ShellCommandBuilder\Exception\ShellBuilderException; |
9
|
|
|
use PHPSu\ShellCommandBuilder\ShellInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @internal |
13
|
|
|
* @psalm-internal PHPSu\ShellCommandBuilder |
14
|
|
|
*/ |
15
|
|
|
final class Redirection extends AbstractCollection |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param string|ShellInterface $value |
19
|
|
|
* @param bool $append |
20
|
|
|
* @return $this |
21
|
|
|
* @throws ShellBuilderException |
22
|
|
|
*/ |
23
|
|
|
public static function redirectOutput($value, bool $append): self |
24
|
|
|
{ |
25
|
|
|
$redirect = new self(); |
26
|
|
|
$redirect->tuple = CollectionTuple::create($value, $append ? RedirectOperator::STDOUT_LEFT_APPEND : RedirectOperator::STDOUT_LEFT_INSERT); |
27
|
|
|
return $redirect; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string|ShellInterface $value |
32
|
|
|
* @return $this |
33
|
|
|
* @throws ShellBuilderException |
34
|
|
|
*/ |
35
|
|
|
public static function redirectInput($value): self |
36
|
|
|
{ |
37
|
|
|
$redirect = new self(); |
38
|
|
|
$redirect->tuple = CollectionTuple::create($value, RedirectOperator::STDIN_RIGHT); |
39
|
|
|
return $redirect; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string|ShellInterface $value |
44
|
|
|
* @return $this |
45
|
|
|
* @throws ShellBuilderException |
46
|
|
|
*/ |
47
|
|
|
public static function redirectError($value): self |
48
|
|
|
{ |
49
|
|
|
$redirect = new self(); |
50
|
|
|
$redirect->tuple = CollectionTuple::create($value, RedirectOperator::FILE_DESCRIPTOR_ERR . RedirectOperator::STDOUT_LEFT_INSERT); |
51
|
|
|
return $redirect; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string|ShellInterface $value |
56
|
|
|
* @param bool $toLeft |
57
|
|
|
* @return $this |
58
|
|
|
* @throws ShellBuilderException |
59
|
|
|
*/ |
60
|
|
|
public static function redirectBetweenFiles($value, bool $toLeft): self |
61
|
|
|
{ |
62
|
|
|
return self::redirectBetweenDescriptors($value, $toLeft); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string|ShellInterface $value |
67
|
|
|
* @param bool $toLeft |
68
|
|
|
* @param int|null $firstDescriptor |
69
|
|
|
* @param int|null $secondDescriptor |
70
|
|
|
* @return static |
71
|
|
|
* @throws ShellBuilderException |
72
|
|
|
*/ |
73
|
|
|
public static function redirectBetweenDescriptors($value, bool $toLeft, int $firstDescriptor = null, int $secondDescriptor = null): self |
74
|
|
|
{ |
75
|
|
|
$redirect = new self(); |
76
|
|
|
$redirect->tuple = CollectionTuple::create($value, sprintf( |
77
|
|
|
'%s%s%s', |
78
|
|
|
$firstDescriptor ?: '', |
79
|
|
|
$toLeft ? RedirectOperator::REDIRECT_LEFT : RedirectOperator::REDIRECT_RIGHT, |
80
|
|
|
$secondDescriptor ?: '' |
81
|
|
|
)); |
82
|
|
|
return $redirect; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public static function redirectErrorToOutput(): self |
86
|
|
|
{ |
87
|
|
|
return self::redirectBetweenDescriptors('', true, 2, 1); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|