Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
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 Symfony\Component\Console\Input;
13
 
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Completion\CompletionInput;
16
use Symfony\Component\Console\Completion\CompletionSuggestions;
17
use Symfony\Component\Console\Completion\Suggestion;
18
use Symfony\Component\Console\Exception\InvalidArgumentException;
19
use Symfony\Component\Console\Exception\LogicException;
20
 
21
/**
22
 * Represents a command line option.
23
 *
24
 * @author Fabien Potencier <fabien@symfony.com>
25
 */
26
class InputOption
27
{
28
    /**
29
     * Do not accept input for the option (e.g. --yell). This is the default behavior of options.
30
     */
31
    public const VALUE_NONE = 1;
32
 
33
    /**
34
     * A value must be passed when the option is used (e.g. --iterations=5 or -i5).
35
     */
36
    public const VALUE_REQUIRED = 2;
37
 
38
    /**
39
     * The option may or may not have a value (e.g. --yell or --yell=loud).
40
     */
41
    public const VALUE_OPTIONAL = 4;
42
 
43
    /**
44
     * The option accepts multiple values (e.g. --dir=/foo --dir=/bar).
45
     */
46
    public const VALUE_IS_ARRAY = 8;
47
 
48
    /**
49
     * The option may have either positive or negative value (e.g. --ansi or --no-ansi).
50
     */
51
    public const VALUE_NEGATABLE = 16;
52
 
53
    private string $name;
54
    private string|array|null $shortcut;
55
    private int $mode;
56
    private string|int|bool|array|null|float $default;
57
    private array|\Closure $suggestedValues;
58
    private string $description;
59
 
60
    /**
688 lars 61
     * @param string|array|null                                                             $shortcut        The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
62
     * @param int|null                                                                      $mode            The option mode: One of the VALUE_* constants
63
     * @param string|bool|int|float|array|null                                              $default         The default value (must be null for self::VALUE_NONE)
148 lars 64
     * @param array|\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion> $suggestedValues The values used for input completion
65
     *
66
     * @throws InvalidArgumentException If option mode is invalid or incompatible
67
     */
68
    public function __construct(string $name, string|array $shortcut = null, int $mode = null, string $description = '', string|bool|int|float|array $default = null, array|\Closure $suggestedValues = [])
69
    {
70
        if (str_starts_with($name, '--')) {
71
            $name = substr($name, 2);
72
        }
73
 
74
        if (empty($name)) {
75
            throw new InvalidArgumentException('An option name cannot be empty.');
76
        }
77
 
78
        if (empty($shortcut)) {
79
            $shortcut = null;
80
        }
81
 
82
        if (null !== $shortcut) {
83
            if (\is_array($shortcut)) {
84
                $shortcut = implode('|', $shortcut);
85
            }
86
            $shortcuts = preg_split('{(\|)-?}', ltrim($shortcut, '-'));
87
            $shortcuts = array_filter($shortcuts);
88
            $shortcut = implode('|', $shortcuts);
89
 
90
            if (empty($shortcut)) {
91
                throw new InvalidArgumentException('An option shortcut cannot be empty.');
92
            }
93
        }
94
 
95
        if (null === $mode) {
96
            $mode = self::VALUE_NONE;
97
        } elseif ($mode >= (self::VALUE_NEGATABLE << 1) || $mode < 1) {
98
            throw new InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode));
99
        }
100
 
101
        $this->name = $name;
102
        $this->shortcut = $shortcut;
103
        $this->mode = $mode;
104
        $this->description = $description;
105
        $this->suggestedValues = $suggestedValues;
106
 
107
        if ($suggestedValues && !$this->acceptValue()) {
108
            throw new LogicException('Cannot set suggested values if the option does not accept a value.');
109
        }
110
        if ($this->isArray() && !$this->acceptValue()) {
111
            throw new InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.');
112
        }
113
        if ($this->isNegatable() && $this->acceptValue()) {
114
            throw new InvalidArgumentException('Impossible to have an option mode VALUE_NEGATABLE if the option also accepts a value.');
115
        }
116
 
117
        $this->setDefault($default);
118
    }
119
 
120
    /**
121
     * Returns the option shortcut.
122
     */
123
    public function getShortcut(): ?string
124
    {
125
        return $this->shortcut;
126
    }
127
 
128
    /**
129
     * Returns the option name.
130
     */
131
    public function getName(): string
132
    {
133
        return $this->name;
134
    }
135
 
136
    /**
137
     * Returns true if the option accepts a value.
138
     *
139
     * @return bool true if value mode is not self::VALUE_NONE, false otherwise
140
     */
141
    public function acceptValue(): bool
142
    {
143
        return $this->isValueRequired() || $this->isValueOptional();
144
    }
145
 
146
    /**
147
     * Returns true if the option requires a value.
148
     *
149
     * @return bool true if value mode is self::VALUE_REQUIRED, false otherwise
150
     */
151
    public function isValueRequired(): bool
152
    {
153
        return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode);
154
    }
155
 
156
    /**
157
     * Returns true if the option takes an optional value.
158
     *
159
     * @return bool true if value mode is self::VALUE_OPTIONAL, false otherwise
160
     */
161
    public function isValueOptional(): bool
162
    {
163
        return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode);
164
    }
165
 
166
    /**
167
     * Returns true if the option can take multiple values.
168
     *
169
     * @return bool true if mode is self::VALUE_IS_ARRAY, false otherwise
170
     */
171
    public function isArray(): bool
172
    {
173
        return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode);
174
    }
175
 
176
    public function isNegatable(): bool
177
    {
178
        return self::VALUE_NEGATABLE === (self::VALUE_NEGATABLE & $this->mode);
179
    }
180
 
181
    public function setDefault(string|bool|int|float|array $default = null)
182
    {
183
        if (1 > \func_num_args()) {
184
            trigger_deprecation('symfony/console', '6.2', 'Calling "%s()" without any arguments is deprecated, pass null explicitly instead.', __METHOD__);
185
        }
186
        if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) {
187
            throw new LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.');
188
        }
189
 
190
        if ($this->isArray()) {
191
            if (null === $default) {
192
                $default = [];
193
            } elseif (!\is_array($default)) {
194
                throw new LogicException('A default value for an array option must be an array.');
195
            }
196
        }
197
 
198
        $this->default = $this->acceptValue() || $this->isNegatable() ? $default : false;
199
    }
200
 
201
    /**
202
     * Returns the default value.
203
     */
204
    public function getDefault(): string|bool|int|float|array|null
205
    {
206
        return $this->default;
207
    }
208
 
209
    /**
210
     * Returns the description text.
211
     */
212
    public function getDescription(): string
213
    {
214
        return $this->description;
215
    }
216
 
217
    public function hasCompletion(): bool
218
    {
219
        return [] !== $this->suggestedValues;
220
    }
221
 
222
    /**
223
     * Adds suggestions to $suggestions for the current completion input.
224
     *
225
     * @see Command::complete()
226
     */
227
    public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
228
    {
229
        $values = $this->suggestedValues;
230
        if ($values instanceof \Closure && !\is_array($values = $values($input))) {
231
            throw new LogicException(sprintf('Closure for option "%s" must return an array. Got "%s".', $this->name, get_debug_type($values)));
232
        }
233
        if ($values) {
234
            $suggestions->suggestValues($values);
235
        }
236
    }
237
 
238
    /**
239
     * Checks whether the given option equals this one.
240
     */
241
    public function equals(self $option): bool
242
    {
243
        return $option->getName() === $this->getName()
244
            && $option->getShortcut() === $this->getShortcut()
245
            && $option->getDefault() === $this->getDefault()
246
            && $option->isNegatable() === $this->isNegatable()
247
            && $option->isArray() === $this->isArray()
248
            && $option->isValueRequired() === $this->isValueRequired()
249
            && $option->isValueOptional() === $this->isValueOptional()
250
        ;
251
    }
252
}