|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the "php-ipfs" package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Robert Schönthal <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace IPFS\Console; |
|
15
|
|
|
|
|
16
|
|
|
use ArgumentsResolver\NamedArgumentsResolver; |
|
17
|
|
|
use IPFS\Api; |
|
18
|
|
|
use IPFS\Client; |
|
19
|
|
|
use IPFS\Driver\Driver; |
|
20
|
|
|
use IPFS\Utils\AnnotationReader; |
|
21
|
|
|
use IPFS\Utils\CaseFormatter; |
|
22
|
|
|
use Symfony\Component\Console\Command\Command; |
|
23
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
24
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
|
25
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
26
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
27
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
28
|
|
|
|
|
29
|
|
|
class CommandBuilder |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var array|Api\Api[] |
|
33
|
|
|
*/ |
|
34
|
|
|
private $apis; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var AnnotationReader |
|
37
|
|
|
*/ |
|
38
|
|
|
private $reader; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var array|Driver[] |
|
41
|
|
|
*/ |
|
42
|
|
|
private $drivers = []; |
|
43
|
|
|
|
|
44
|
|
|
public function __construct(array $apis, AnnotationReader $reader) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->apis = $apis; |
|
47
|
|
|
$this->reader = $reader; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return array|ApiCommand[] |
|
52
|
|
|
*/ |
|
53
|
1 |
|
public function generateCommands(): array |
|
54
|
|
|
{ |
|
55
|
1 |
|
$commands = []; |
|
56
|
1 |
|
|
|
57
|
|
|
foreach ($this->apis as $class) { |
|
58
|
1 |
|
$api = new \ReflectionClass($class); |
|
59
|
1 |
|
|
|
60
|
|
|
foreach ($api->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
|
61
|
|
|
if ($this->reader->isApi($method)) { |
|
62
|
|
|
$command = $this->generateCommand($class, $api->getShortName(), $method); |
|
63
|
|
|
$commands[$command->getName()] = $command; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $commands; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function generateCommand(Api\Api $api, string $name, \ReflectionMethod $method): Command |
|
72
|
|
|
{ |
|
73
|
|
|
$command = new ApiCommand(strtolower($this->reader->getName($method))); |
|
74
|
|
|
|
|
75
|
|
|
return $command |
|
76
|
|
|
->setDefinition($this->buildDefinition($method)) |
|
77
|
|
|
->setDescription($this->reader->getDescription($method)) |
|
78
|
|
|
->setCode($this->createCode($api, $name, $method)) |
|
79
|
|
|
; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function buildDefinition(\ReflectionMethod $method): InputDefinition |
|
83
|
|
|
{ |
|
84
|
|
|
$definition = new InputDefinition(); |
|
85
|
|
|
|
|
86
|
|
|
$parameters = $this->reader->getParameters($method); |
|
87
|
|
|
|
|
88
|
|
|
foreach ($parameters as $name => $param) { |
|
89
|
|
|
$name = CaseFormatter::camelToDash($name); |
|
90
|
|
|
|
|
91
|
|
|
if ($param->hasDefault()) { |
|
92
|
|
|
$mode = InputOption::VALUE_NONE; |
|
93
|
|
|
$default = $param->getDefault(); |
|
94
|
|
|
|
|
95
|
|
|
if (true === $default || is_string($default)) { |
|
96
|
|
|
$mode = InputOption::VALUE_OPTIONAL; |
|
97
|
|
|
} elseif (is_bool($default)) { |
|
98
|
|
|
$default = null; |
|
99
|
|
|
} elseif (!is_string($default)) { |
|
100
|
|
|
$mode = InputOption::VALUE_REQUIRED; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$definition->addOption(new InputOption($name, null, $mode, $param->getDescription(), $default)); |
|
104
|
|
|
|
|
105
|
|
|
continue; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$definition->addArgument(new InputArgument($name, InputArgument::REQUIRED, $param->getDescription(), null)); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $definition; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
private function createCode(Api\Api $api, $name, \ReflectionMethod $method): \Closure |
|
115
|
|
|
{ |
|
116
|
|
|
return function (InputInterface $input, OutputInterface $output) use ($name, $method, $api) { |
|
117
|
|
|
$fn = $method->getClosure($api); |
|
118
|
|
|
|
|
119
|
|
|
$options = CaseFormatter::dashToCamelArray($input->getOptions()); |
|
120
|
|
|
$arguments = CaseFormatter::dashToCamelArray($input->getArguments()); |
|
121
|
|
|
|
|
122
|
|
|
$args = (new NamedArgumentsResolver($method))->resolve(array_merge($options, $arguments)); |
|
123
|
|
|
$args = $this->sanitizeArguments($args); |
|
124
|
|
|
|
|
125
|
|
|
$client = new Client($this->chooseClient($input->getOption('driver'))); |
|
126
|
|
|
$response = $client->execute($fn(...$args)); |
|
127
|
|
|
|
|
128
|
|
|
$output->writeln($response); |
|
129
|
|
|
}; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function addDriver(Driver $driver): CommandBuilder |
|
133
|
|
|
{ |
|
134
|
|
|
$this->drivers[get_class($driver)] = $driver; |
|
135
|
|
|
|
|
136
|
|
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
private function chooseClient(string $class): Driver |
|
140
|
|
|
{ |
|
141
|
|
|
if (!isset($this->drivers[$class])) { |
|
142
|
|
|
throw new \InvalidArgumentException(sprintf('"%s" is an unknown Driver, please add it with "addDriver"', $class)); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $this->drivers[$class]; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
private function sanitizeArguments(array $args): array |
|
149
|
|
|
{ |
|
150
|
|
|
foreach ($args as $index => $value) { |
|
151
|
|
|
$args[$index] = CaseFormatter::stringToBool($value); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return $args; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|