Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | 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\Exception\InvalidArgumentException;
15
use Symfony\Component\Console\Exception\RuntimeException;
16
 
17
/**
18
 * Input is the base class for all concrete Input classes.
19
 *
20
 * Three concrete classes are provided by default:
21
 *
22
 *  * `ArgvInput`: The input comes from the CLI arguments (argv)
23
 *  * `StringInput`: The input is provided as a string
24
 *  * `ArrayInput`: The input is provided as an array
25
 *
26
 * @author Fabien Potencier <fabien@symfony.com>
27
 */
28
abstract class Input implements InputInterface, StreamableInputInterface
29
{
30
    protected $definition;
31
    protected $stream;
32
    protected $options = [];
33
    protected $arguments = [];
34
    protected $interactive = true;
35
 
36
    public function __construct(InputDefinition $definition = null)
37
    {
38
        if (null === $definition) {
39
            $this->definition = new InputDefinition();
40
        } else {
41
            $this->bind($definition);
42
            $this->validate();
43
        }
44
    }
45
 
46
    public function bind(InputDefinition $definition)
47
    {
48
        $this->arguments = [];
49
        $this->options = [];
50
        $this->definition = $definition;
51
 
52
        $this->parse();
53
    }
54
 
55
    /**
56
     * Processes command line arguments.
57
     */
58
    abstract protected function parse();
59
 
60
    public function validate()
61
    {
62
        $definition = $this->definition;
63
        $givenArguments = $this->arguments;
64
 
65
        $missingArguments = array_filter(array_keys($definition->getArguments()), function ($argument) use ($definition, $givenArguments) {
66
            return !\array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired();
67
        });
68
 
69
        if (\count($missingArguments) > 0) {
70
            throw new RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments)));
71
        }
72
    }
73
 
74
    public function isInteractive(): bool
75
    {
76
        return $this->interactive;
77
    }
78
 
79
    public function setInteractive(bool $interactive)
80
    {
81
        $this->interactive = $interactive;
82
    }
83
 
84
    public function getArguments(): array
85
    {
86
        return array_merge($this->definition->getArgumentDefaults(), $this->arguments);
87
    }
88
 
89
    public function getArgument(string $name): mixed
90
    {
91
        if (!$this->definition->hasArgument($name)) {
92
            throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
93
        }
94
 
95
        return $this->arguments[$name] ?? $this->definition->getArgument($name)->getDefault();
96
    }
97
 
98
    public function setArgument(string $name, mixed $value)
99
    {
100
        if (!$this->definition->hasArgument($name)) {
101
            throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
102
        }
103
 
104
        $this->arguments[$name] = $value;
105
    }
106
 
107
    public function hasArgument(string $name): bool
108
    {
109
        return $this->definition->hasArgument($name);
110
    }
111
 
112
    public function getOptions(): array
113
    {
114
        return array_merge($this->definition->getOptionDefaults(), $this->options);
115
    }
116
 
117
    public function getOption(string $name): mixed
118
    {
119
        if ($this->definition->hasNegation($name)) {
120
            if (null === $value = $this->getOption($this->definition->negationToName($name))) {
121
                return $value;
122
            }
123
 
124
            return !$value;
125
        }
126
 
127
        if (!$this->definition->hasOption($name)) {
128
            throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
129
        }
130
 
131
        return \array_key_exists($name, $this->options) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
132
    }
133
 
134
    public function setOption(string $name, mixed $value)
135
    {
136
        if ($this->definition->hasNegation($name)) {
137
            $this->options[$this->definition->negationToName($name)] = !$value;
138
 
139
            return;
140
        } elseif (!$this->definition->hasOption($name)) {
141
            throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
142
        }
143
 
144
        $this->options[$name] = $value;
145
    }
146
 
147
    public function hasOption(string $name): bool
148
    {
149
        return $this->definition->hasOption($name) || $this->definition->hasNegation($name);
150
    }
151
 
152
    /**
153
     * Escapes a token through escapeshellarg if it contains unsafe chars.
154
     */
155
    public function escapeToken(string $token): string
156
    {
157
        return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token);
158
    }
159
 
160
    public function setStream($stream)
161
    {
162
        $this->stream = $stream;
163
    }
164
 
165
    public function getStream()
166
    {
167
        return $this->stream;
168
    }
169
}