|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
7
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
8
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
9
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
10
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
11
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
12
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
13
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
14
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
15
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
16
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
17
|
|
|
* |
|
18
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
19
|
|
|
* and is licensed under the MIT license. For more information, see |
|
20
|
|
|
* <https://github.com/digitalkaoz/php-ipfs> |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
namespace IPFS\Driver; |
|
24
|
|
|
|
|
25
|
|
|
use IPFS\Command\Command; |
|
26
|
|
|
use IPFS\Utils\AnnotationReader; |
|
27
|
|
|
use IPFS\Utils\CaseFormatter; |
|
28
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
|
29
|
|
|
|
|
30
|
|
|
class Cli implements Driver |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var ProcessBuilder |
|
34
|
|
|
*/ |
|
35
|
|
|
private $builder; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
private $binary; |
|
40
|
|
|
/** |
|
41
|
|
|
* @var AnnotationReader |
|
42
|
|
|
*/ |
|
43
|
|
|
private $reader; |
|
44
|
|
|
|
|
45
|
2 |
|
public function __construct(ProcessBuilder $builder, AnnotationReader $reader, $binary = 'ipfs') |
|
46
|
|
|
{ |
|
47
|
2 |
|
$this->builder = $builder; |
|
48
|
2 |
|
$this->binary = $binary; |
|
49
|
2 |
|
$this->reader = $reader; |
|
50
|
2 |
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
public function execute(Command $command) |
|
53
|
|
|
{ |
|
54
|
1 |
|
$process = $this->builder |
|
55
|
1 |
|
->setArguments($this->buildCommand($command)) |
|
56
|
1 |
|
->enableOutput() |
|
57
|
1 |
|
->inheritEnvironmentVariables() |
|
58
|
1 |
|
->setWorkingDirectory(getenv('CWD')) |
|
59
|
1 |
|
->getProcess() |
|
60
|
|
|
; |
|
61
|
|
|
|
|
62
|
1 |
|
$process->start(); |
|
63
|
1 |
|
$process->wait(); |
|
64
|
|
|
|
|
65
|
1 |
|
return $process->getOutput() ?: $process->getErrorOutput(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
private function buildCommand(Command $command): array |
|
69
|
|
|
{ |
|
70
|
1 |
|
return array_merge( |
|
71
|
1 |
|
[$this->binary], |
|
72
|
1 |
|
explode(':', str_replace('basics:', '', $command->getAction())), |
|
73
|
1 |
|
$this->parseParameters($command) |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
private function parseParameters(Command $command): array |
|
78
|
|
|
{ |
|
79
|
1 |
|
$parameters = $this->reader->getParameters($command->getMethod()); |
|
80
|
|
|
|
|
81
|
1 |
|
$parsedParameters = []; |
|
82
|
|
|
|
|
83
|
1 |
|
foreach ($command->getArguments() as $name => $value) { |
|
84
|
1 |
|
if ($parameters[$name]->hasDefault() && $parameters[$name]->getDefault() !== $value) { |
|
85
|
1 |
|
$parsedParameters[] = sprintf('--%s=%s', CaseFormatter::camelToDash($name), var_export($value, true)); |
|
86
|
1 |
|
continue; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
if (!$parameters[$name]->hasDefault()) { |
|
90
|
1 |
|
$parsedParameters[] = $value; |
|
91
|
1 |
|
continue; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
throw new \LogicException(sprintf('"%s" is neither an option nor an argument', $name)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
return $parsedParameters; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|