817292ed73086fe711776a44025ff7308e1fa96c
[yaffs-website] / vendor / symfony / console / Input / Input.php
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
29 {
30     /**
31      * @var InputDefinition
32      */
33     protected $definition;
34     protected $options = array();
35     protected $arguments = array();
36     protected $interactive = true;
37
38     /**
39      * Constructor.
40      *
41      * @param InputDefinition|null $definition A InputDefinition instance
42      */
43     public function __construct(InputDefinition $definition = null)
44     {
45         if (null === $definition) {
46             $this->definition = new InputDefinition();
47         } else {
48             $this->bind($definition);
49             $this->validate();
50         }
51     }
52
53     /**
54      * {@inheritdoc}
55      */
56     public function bind(InputDefinition $definition)
57     {
58         $this->arguments = array();
59         $this->options = array();
60         $this->definition = $definition;
61
62         $this->parse();
63     }
64
65     /**
66      * Processes command line arguments.
67      */
68     abstract protected function parse();
69
70     /**
71      * {@inheritdoc}
72      */
73     public function validate()
74     {
75         $definition = $this->definition;
76         $givenArguments = $this->arguments;
77
78         $missingArguments = array_filter(array_keys($definition->getArguments()), function ($argument) use ($definition, $givenArguments) {
79             return !array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired();
80         });
81
82         if (count($missingArguments) > 0) {
83             throw new RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments)));
84         }
85     }
86
87     /**
88      * {@inheritdoc}
89      */
90     public function isInteractive()
91     {
92         return $this->interactive;
93     }
94
95     /**
96      * {@inheritdoc}
97      */
98     public function setInteractive($interactive)
99     {
100         $this->interactive = (bool) $interactive;
101     }
102
103     /**
104      * {@inheritdoc}
105      */
106     public function getArguments()
107     {
108         return array_merge($this->definition->getArgumentDefaults(), $this->arguments);
109     }
110
111     /**
112      * {@inheritdoc}
113      */
114     public function getArgument($name)
115     {
116         if (!$this->definition->hasArgument($name)) {
117             throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
118         }
119
120         return isset($this->arguments[$name]) ? $this->arguments[$name] : $this->definition->getArgument($name)->getDefault();
121     }
122
123     /**
124      * {@inheritdoc}
125      */
126     public function setArgument($name, $value)
127     {
128         if (!$this->definition->hasArgument($name)) {
129             throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
130         }
131
132         $this->arguments[$name] = $value;
133     }
134
135     /**
136      * {@inheritdoc}
137      */
138     public function hasArgument($name)
139     {
140         return $this->definition->hasArgument($name);
141     }
142
143     /**
144      * {@inheritdoc}
145      */
146     public function getOptions()
147     {
148         return array_merge($this->definition->getOptionDefaults(), $this->options);
149     }
150
151     /**
152      * {@inheritdoc}
153      */
154     public function getOption($name)
155     {
156         if (!$this->definition->hasOption($name)) {
157             throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
158         }
159
160         return isset($this->options[$name]) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
161     }
162
163     /**
164      * {@inheritdoc}
165      */
166     public function setOption($name, $value)
167     {
168         if (!$this->definition->hasOption($name)) {
169             throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
170         }
171
172         $this->options[$name] = $value;
173     }
174
175     /**
176      * {@inheritdoc}
177      */
178     public function hasOption($name)
179     {
180         return $this->definition->hasOption($name);
181     }
182
183     /**
184      * Escapes a token through escapeshellarg if it contains unsafe chars.
185      *
186      * @param string $token
187      *
188      * @return string
189      */
190     public function escapeToken($token)
191     {
192         return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token);
193     }
194 }