This repository was archived by the owner on Jun 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDescriptor.php
More file actions
234 lines (202 loc) · 8.23 KB
/
Descriptor.php
File metadata and controls
234 lines (202 loc) · 8.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php namespace nyx\console;
// External dependencies
use nyx\diagnostics;
// Internal dependencies
use nyx\console\input\parameter\definitions;
/**
* Descriptor
*
* The particular methods are public for simplicity's sake but if you intend to use them directly, ensure you are
* checking for the type of this class, not for the interface, as the interface does not require them to be present
* for the contract to be fulfilled.
*
* @version 0.1.0
* @author Michal Chojnacki <m.chojnacki@muyo.io>
* @copyright 2012-2017 Nyx Dev Team
* @link https://github.com/unyx/nyx
*/
abstract class Descriptor implements interfaces\Descriptor
{
/**
* @var array A map of supported types to their respective instance methods, minus the 'describe' prefix.
*/
protected $types = [
Application::class => 'Application',
Suite::class => 'Suite',
Command::class => 'Command',
input\Definition::class => 'InputDefinition',
input\Argument::class => 'InputArgument',
input\Option::class => 'InputOption',
input\Value::class => 'InputValue',
];
/**
* {@inheritdoc}
*/
public function describe($object, array $options = null)
{
foreach ($this->types as $class => $type) {
if ($object instanceof $class) {
return $this->{'describe'.$type}($object, $options);
}
}
throw new \InvalidArgumentException("The type [".diagnostics\Debug::getTypeName($object)."] is not supported by this Descriptor.");
}
/**
* Describes an Application.
*
* @param Application $application The Application to describe.
* @param array $options Additional options to be considered by the Descriptor.
* @return mixed
*/
abstract public function describeApplication(Application $application, array $options = null);
/**
* Describes a Suite.
*
* @param Suite $suite The Suite to describe.
* @param array $options Additional options to be considered by the Descriptor.
* @return mixed
*/
abstract public function describeSuite(Suite $suite, array $options = null);
/**
* Describes a Command.
*
* @param Command $command The Command to describe.
* @param array $options Additional options to be considered by the Descriptor.
* @return mixed
*/
abstract public function describeCommand(Command $command, array $options = null);
/**
* Describes an Input Definition.
*
* @param input\Definition $definition The Input Definition to describe.
* @param array $options Additional options to be considered by the Descriptor.
* @return mixed
*/
abstract public function describeInputDefinition(input\Definition $definition, array $options = null);
/**
* Describes an Input Argument.
*
* @param input\Argument $argument The Input Argument to describe.
* @param array $options Additional options to be considered by the Descriptor.
* @return mixed
*/
abstract public function describeInputArgument(input\Argument $argument, array $options = null);
/**
* Describes an Input Option.
*
* @param input\Option $option The Input Option to describe.
* @param array $options Additional options to be considered by the Descriptor.
* @return mixed
*/
abstract public function describeInputOption(input\Option $option, array $options = null);
/**
* Describes an Input Value.
*
* @param input\Value $value The Input Value to describe.
* @param array $options Additional options to be considered by the Descriptor.
* @return mixed
*/
abstract public function describeInputValue(input\Value $value, array $options = null);
/**
* Provides a synopsis (ie. a string describing the usage) for the given Command.
*
* Kept in the abstract Descriptor as the generated string is rather generic, but may require overrides for
* specific formats and therefore should not be kept within the Command class.
*
* @param Command $command The Command to provide the synopsis for.
* @return mixed
*/
public function getCommandSynopsis(Command $command, array $options = null)
{
$definition = $command->getDefinition();
$inputOptions = $definition->options();
$inputArguments = $definition->arguments();
// We are following the docopt specification here. Options are followed by '--', followed by arguments.
// @link http://docopt.org
$output = $this->getInputOptionsSynopsis($inputOptions, $options);
if (!$inputOptions->isEmpty() && !$inputArguments->isEmpty()) {
$output .= ' [--] ';
}
return $output . $this->getInputArgumentsSynopsis($inputArguments, $options);
}
/**
* Provides a synopsis for the given Input Option Definitions.
*
* @param definitions\Options $definitions The Input Option Definitions to provide a synopsis for.
* @param array $options Additional options to be considered by the Descriptor.
* @return mixed
*/
public function getInputOptionsSynopsis(definitions\Options $definitions, array $options = null)
{
if ($definitions->isEmpty()) {
return '';
}
// The 'short_synopsis' option is primarily used by the Help command which lists all Input Options
// separately from the synopsis for brevity.
if ($options['short_synopsis'] ?? false) {
return '[options]';
}
$items = [];
foreach ($definitions as $option) {
$items[] = $this->getInputOptionSynopsis($option, $options);
}
return implode(' ', $items);
}
/**
* Provides a synopsis for the given Input Option.
*
* @param input\Option $definition The Input Option to provide a synopsis for.
* @param array $options Additional options to be considered by the Descriptor.
* @return mixed
*/
public function getInputOptionSynopsis(input\Option $definition, array $options = null)
{
$shortcut = $definition->getShortcut() ? sprintf('-%s|', $definition->getShortcut()) : '';
if ($value = $definition->getValue()) {
$format = $value->is(input\Value::REQUIRED) ? '%s--%s="..."' : '%s--%s[="..."]';
} else {
$format = '%s--%s';
}
return sprintf('['.$format.']', $shortcut, $definition->getName());
}
/**
* Provides a synopsis for the given Input Argument Definitions.
*
* @param definitions\Arguments $definitions The Input Argument Definitions to provide a synopsis for.
* @param array $options Additional options to be considered by the Descriptor.
* @return mixed
*/
public function getInputArgumentsSynopsis(definitions\Arguments $definitions, array $options = null)
{
if ($definitions->isEmpty()) {
return '';
}
$items = [];
/** @var input\Argument $argument */
foreach ($definitions as $argument) {
$items[] = $this->getInputArgumentSynopsis($argument, $options);
}
return implode(' ', $items);
}
/**
* Provides a synopsis for the given Input Argument.
*
* @param input\Argument $definition The Input Argument to provide a synopsis for.
* @param array $options Additional options to be considered by the Descriptor.
* @return mixed
*/
public function getInputArgumentSynopsis(input\Argument $definition, array $options = null)
{
$output = '\<'.$definition->getName().'>';
$value = $definition->getValue();
if (!$value->is(input\Value::REQUIRED)) {
$output = '['.$output.']';
} elseif ($value instanceof input\values\Multiple) {
$output = $output.' ('.$output.')';
}
if ($value instanceof input\values\Multiple) {
$output .= '...';
}
return $output;
}
}