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\Console; |
24
|
|
|
|
25
|
|
|
use ArgumentsResolver\NamedArgumentsResolver; |
26
|
|
|
use IPFS\Api; |
27
|
|
|
use IPFS\Client; |
28
|
|
|
use IPFS\Utils\AnnotationReader; |
29
|
|
|
use IPFS\Utils\CaseFormatter; |
30
|
|
|
use Pimple\Container; |
31
|
|
|
use Symfony\Component\Console\Command\Command; |
32
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
33
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
34
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
35
|
|
|
use Symfony\Component\Console\Input\InputOption; |
36
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
37
|
|
|
|
38
|
|
|
class CommandBuilder |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* @var array|Api\Api[] |
42
|
|
|
*/ |
43
|
|
|
private $apis; |
44
|
|
|
/** |
45
|
|
|
* @var AnnotationReader |
46
|
|
|
*/ |
47
|
|
|
private $reader; |
48
|
|
|
/** |
49
|
|
|
* @var Container |
50
|
|
|
*/ |
51
|
|
|
private $container; |
52
|
|
|
|
53
|
|
|
public function __construct(array $apis, Container $container) |
54
|
|
|
{ |
55
|
|
|
$this->apis = $apis; |
56
|
|
|
$this->container = $container; |
57
|
|
|
|
58
|
|
|
$this->reader = $this->container[AnnotationReader::class]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function generateCommands(): array |
62
|
|
|
{ |
63
|
|
|
$commands = []; |
64
|
|
|
|
65
|
|
|
foreach ($this->apis as $class) { |
66
|
|
|
$api = new \ReflectionClass($class); |
67
|
|
|
|
68
|
|
|
foreach ($api->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
69
|
|
|
if ($this->reader->isApi($method)) { |
70
|
|
|
$command = $this->generateCommand($class, $api->getShortName(), $method); |
71
|
|
|
$commands[$command->getName()] = $command; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $commands; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function generateCommand(Api\Api $api, string $name, \ReflectionMethod $method): Command |
80
|
|
|
{ |
81
|
|
|
$command = new ApiCommand(strtolower($this->reader->getName($method))); |
82
|
|
|
|
83
|
|
|
return $command |
84
|
|
|
->setDefinition($this->buildDefinition($method)) |
85
|
|
|
->setDescription($this->reader->getDescription($method)) |
86
|
|
|
->setCode($this->createCode($api, $name, $method)) |
87
|
|
|
; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function buildDefinition(\ReflectionMethod $method): InputDefinition |
91
|
|
|
{ |
92
|
|
|
$definition = new InputDefinition(); |
93
|
|
|
|
94
|
|
|
$parameters = $this->reader->getParameters($method); |
95
|
|
|
|
96
|
|
|
foreach ($parameters as $name => $param) { |
97
|
|
|
$name = CaseFormatter::camelToDash($name); |
98
|
|
|
|
99
|
|
|
if ($param->hasDefault()) { |
100
|
|
|
$mode = InputOption::VALUE_NONE; |
101
|
|
|
$default = $param->getDefault(); |
102
|
|
|
|
103
|
|
|
if (true === $default || is_string($default)) { |
104
|
|
|
$mode = InputOption::VALUE_OPTIONAL; |
105
|
|
|
} elseif (is_bool($default)) { |
106
|
|
|
$default = null; |
107
|
|
|
} elseif (!is_string($default)) { |
108
|
|
|
$mode = InputOption::VALUE_REQUIRED; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$definition->addOption(new InputOption($name, null, $mode, $param->getDescription(), $default)); |
112
|
|
|
|
113
|
|
|
continue; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$definition->addArgument(new InputArgument($name, InputArgument::REQUIRED, $param->getDescription(), null)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $definition; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private function createCode(Api\Api $api, $name, \ReflectionMethod $method): \Closure |
123
|
|
|
{ |
124
|
|
|
return function (InputInterface $input, OutputInterface $output) use ($name, $method, $api) { |
125
|
|
|
$fn = $method->getClosure($api); |
126
|
|
|
|
127
|
|
|
$options = CaseFormatter::dashToCamelArray($input->getOptions()); |
128
|
|
|
$arguments = CaseFormatter::dashToCamelArray($input->getArguments()); |
129
|
|
|
|
130
|
|
|
$args = (new NamedArgumentsResolver($method))->resolve(array_merge($options, $arguments)); |
131
|
|
|
$args = $this->sanitizeArguments($args); |
132
|
|
|
|
133
|
|
|
$client = new Client($this->container[$input->getOption('driver')]); |
134
|
|
|
$response = $client->execute($fn(...$args)); |
135
|
|
|
|
136
|
|
|
$output->writeln($response); |
137
|
|
|
//dump($response, json_decode($response, true)); |
|
|
|
|
138
|
|
|
//$output->writeln(print_r(json_decode($response, true), true)); |
|
|
|
|
139
|
|
|
}; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function sanitizeArguments(array $args): array |
143
|
|
|
{ |
144
|
|
|
foreach ($args as $index => $value) { |
145
|
|
|
$args[$index] = CaseFormatter::stringToBool($value); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $args; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.