|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the webmozart/console package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Webmozart\Console\Adapter; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
17
|
|
|
use Webmozart\Console\Api\Args\Format\ArgsFormat; |
|
18
|
|
|
use Webmozart\Console\Api\Args\Format\Argument; |
|
19
|
|
|
use Webmozart\Console\Api\Args\Format\CommandName; |
|
20
|
|
|
use Webmozart\Console\Api\Args\Format\CommandOption; |
|
21
|
|
|
use Webmozart\Console\Api\Args\Format\Option; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Adapts an {@link ArgsFormat} instance to Symfony's {@link InputDefinition} |
|
25
|
|
|
* API. |
|
26
|
|
|
* |
|
27
|
|
|
* @since 1.0 |
|
28
|
|
|
* |
|
29
|
|
|
* @author Bernhard Schussek <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
|
|
class ArgsFormatInputDefinition extends InputDefinition |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var CommandName[] |
|
35
|
|
|
*/ |
|
36
|
|
|
private $commandNames = array(); |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Creates a new adapter. |
|
40
|
|
|
* |
|
41
|
|
|
* @param ArgsFormat $format The adapted format. |
|
42
|
|
|
*/ |
|
43
|
322 |
|
public function __construct(ArgsFormat $format) |
|
44
|
|
|
{ |
|
45
|
322 |
|
parent::__construct(); |
|
46
|
|
|
|
|
47
|
322 |
|
$i = 1; |
|
48
|
|
|
|
|
49
|
322 |
|
foreach ($format->getCommandNames() as $commandName) { |
|
50
|
|
|
do { |
|
51
|
308 |
|
$argName = 'cmd'.$i++; |
|
52
|
308 |
|
} while ($format->hasArgument($argName)); |
|
53
|
|
|
|
|
54
|
308 |
|
$this->addArgument($argument = $this->adaptCommandName($commandName, $argName)); |
|
55
|
|
|
|
|
56
|
308 |
|
$this->commandNames[$argument->getName()] = $commandName; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
322 |
|
foreach ($format->getCommandOptions() as $commandOption) { |
|
60
|
87 |
|
$this->addOption($this->adaptCommandOption($commandOption)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
322 |
|
foreach ($format->getOptions() as $option) { |
|
64
|
272 |
|
$this->addOption($this->adaptOption($option)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
322 |
|
foreach ($format->getArguments() as $argument) { |
|
68
|
269 |
|
$this->addArgument($this->adaptArgument($argument)); |
|
69
|
|
|
} |
|
70
|
322 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Returns the command names indexed by their argument names. |
|
74
|
|
|
* |
|
75
|
|
|
* @return CommandName[] The command names. |
|
76
|
|
|
*/ |
|
77
|
297 |
|
public function getCommandNamesByArgumentName() |
|
78
|
|
|
{ |
|
79
|
297 |
|
return $this->commandNames; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Creates an input argument for the given command name. |
|
84
|
|
|
* |
|
85
|
|
|
* @param CommandName $commandName The command name. |
|
86
|
|
|
* @param string $argName The name of the added argument. |
|
87
|
|
|
* |
|
88
|
|
|
* @return InputArgument The created input argument. |
|
89
|
|
|
*/ |
|
90
|
308 |
|
private function adaptCommandName(CommandName $commandName, $argName) |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
308 |
|
return new InputArgument($argName, InputArgument::REQUIRED); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Creates an input option for the given command option. |
|
97
|
|
|
* |
|
98
|
|
|
* @param CommandOption $commandOption The command option. |
|
99
|
|
|
* |
|
100
|
|
|
* @return InputOption The created input option. |
|
101
|
|
|
*/ |
|
102
|
87 |
|
private function adaptCommandOption(CommandOption $commandOption) |
|
103
|
|
|
{ |
|
104
|
87 |
|
return new InputOption($commandOption->getLongName(), $commandOption->getShortName()); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Creates an input option for the given option. |
|
109
|
|
|
* |
|
110
|
|
|
* @param Option $option The option. |
|
111
|
|
|
* |
|
112
|
|
|
* @return InputOption The created input option. |
|
113
|
|
|
*/ |
|
114
|
272 |
|
private function adaptOption(Option $option) |
|
115
|
|
|
{ |
|
116
|
272 |
|
$mode = null; |
|
117
|
|
|
|
|
118
|
272 |
|
if ($option->isMultiValued()) { |
|
119
|
1 |
|
$mode |= InputOption::VALUE_IS_ARRAY; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
272 |
|
if ($option->isValueOptional()) { |
|
123
|
264 |
|
$mode |= InputOption::VALUE_OPTIONAL; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
272 |
|
if ($option->isValueRequired()) { |
|
127
|
4 |
|
$mode |= InputOption::VALUE_REQUIRED; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
272 |
|
return new InputOption($option->getLongName(), $option->getShortName(), $mode, $option->getDescription(), $option->getDefaultValue()); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Creates an input argument for the given argument. |
|
135
|
|
|
* |
|
136
|
|
|
* @param Argument $argument The argument. |
|
137
|
|
|
* |
|
138
|
|
|
* @return InputArgument The created input argument. |
|
139
|
|
|
*/ |
|
140
|
269 |
|
private function adaptArgument(Argument $argument) |
|
141
|
|
|
{ |
|
142
|
269 |
|
$mode = null; |
|
143
|
|
|
|
|
144
|
269 |
|
if ($argument->isMultiValued()) { |
|
145
|
5 |
|
$mode |= InputArgument::IS_ARRAY; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
269 |
|
if ($argument->isOptional()) { |
|
149
|
261 |
|
$mode |= InputArgument::OPTIONAL; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
269 |
|
if ($argument->isRequired()) { |
|
153
|
8 |
|
$mode |= InputArgument::REQUIRED; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
269 |
|
return new InputArgument($argument->getName(), $mode, $argument->getDescription(), $argument->getDefaultValue()); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.