weew /
console
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Weew\Console; |
||
| 4 | |||
| 5 | use Weew\ConsoleArguments\Command; |
||
| 6 | use Weew\ConsoleArguments\Exceptions\ArgumentNotFoundException; |
||
| 7 | use Weew\ConsoleArguments\Exceptions\OptionNotFoundException; |
||
| 8 | use Weew\ConsoleArguments\ICommand; |
||
| 9 | |||
| 10 | class Input implements IInput { |
||
| 11 | /** |
||
| 12 | * @var ICommand |
||
| 13 | */ |
||
| 14 | protected $command; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var int |
||
| 18 | */ |
||
| 19 | protected $inputVerbosity; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var resource |
||
| 23 | */ |
||
| 24 | protected $inputStream; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Input constructor. |
||
| 28 | * |
||
| 29 | * @param ICommand $command |
||
| 30 | */ |
||
| 31 | public function __construct(ICommand $command = null) { |
||
| 32 | if ( ! $command instanceof ICommand) { |
||
| 33 | $command = new Command(); |
||
| 34 | } |
||
| 35 | |||
| 36 | $this->command = $command; |
||
| 37 | $this->setInputVerbosity(InputVerbosity::NORMAL); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param string $name |
||
| 42 | * @param null $default |
||
| 43 | * |
||
| 44 | * @return mixed|null |
||
| 45 | */ |
||
| 46 | public function getArgument($name, $default = null) { |
||
| 47 | try { |
||
| 48 | $argument = $this->getCommand()->findArgument($name); |
||
| 49 | $value = $argument->getValue(); |
||
| 50 | |||
| 51 | if ($value === null) { |
||
| 52 | $value = $default; |
||
| 53 | } |
||
| 54 | |||
| 55 | return $value; |
||
| 56 | } catch (ArgumentNotFoundException $ex) {} |
||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
Loading history...
|
|||
| 57 | |||
| 58 | return $default; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param string $name |
||
| 63 | * @param mixed $value |
||
| 64 | */ |
||
| 65 | public function setArgument($name, $value) { |
||
| 66 | $argument = $this->getCommand()->findArgument($name); |
||
| 67 | $argument->setValue($value); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param string $name |
||
| 72 | * |
||
| 73 | * @return bool |
||
| 74 | */ |
||
| 75 | public function hasArgument($name) { |
||
| 76 | try { |
||
| 77 | $argument = $this->getCommand()->findArgument($name); |
||
| 78 | |||
| 79 | return $argument->hasValue(); |
||
| 80 | } catch (ArgumentNotFoundException $ex) {} |
||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
|
|||
| 81 | |||
| 82 | return false; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param $nameOrAlias |
||
| 87 | * @param null $default |
||
| 88 | * |
||
| 89 | * @return mixed|null |
||
| 90 | */ |
||
| 91 | public function getOption($nameOrAlias, $default = null) { |
||
| 92 | try { |
||
| 93 | $option = $this->getCommand()->findOption($nameOrAlias); |
||
| 94 | $value = $option->getValue(); |
||
| 95 | |||
| 96 | if ($value === null) { |
||
| 97 | $value = $default; |
||
| 98 | } |
||
| 99 | |||
| 100 | return $value; |
||
| 101 | } catch (OptionNotFoundException $ex) {} |
||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
|
|||
| 102 | |||
| 103 | return $default; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param string $nameOrAlias |
||
| 108 | * @param mixed $value |
||
| 109 | */ |
||
| 110 | public function setOption($nameOrAlias, $value) { |
||
| 111 | $option = $this->getCommand()->findOption($nameOrAlias); |
||
| 112 | $option->setValue($value); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param string $nameOrAlias |
||
| 117 | * |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | public function hasOption($nameOrAlias) { |
||
| 121 | try { |
||
| 122 | $option = $this->getCommand()->findOption($nameOrAlias); |
||
| 123 | |||
| 124 | return $option->hasValue(); |
||
| 125 | } catch (OptionNotFoundException $ex) {} |
||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
|
|||
| 126 | |||
| 127 | return false; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | public function readLine() { |
||
| 134 | if ( ! $this->is(InputVerbosity::SILENT)) { |
||
| 135 | return trim(fgets($this->getInputStream())); |
||
| 136 | } |
||
| 137 | |||
| 138 | return ''; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @return string |
||
| 143 | */ |
||
| 144 | public function readChar() { |
||
| 145 | if ( ! $this->is(InputVerbosity::SILENT)) { |
||
| 146 | return fgetc($this->getInputStream()); |
||
| 147 | } |
||
| 148 | |||
| 149 | return ''; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @return ICommand |
||
| 154 | */ |
||
| 155 | public function getCommand() { |
||
| 156 | return $this->command; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param ICommand $command |
||
| 161 | */ |
||
| 162 | public function setCommand(ICommand $command) { |
||
| 163 | $this->command = $command; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return int |
||
| 168 | */ |
||
| 169 | public function getInputVerbosity() { |
||
| 170 | return $this->inputVerbosity; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param int $inputVerbosity |
||
| 175 | * |
||
| 176 | * @see InputVerbosity |
||
| 177 | */ |
||
| 178 | public function setInputVerbosity($inputVerbosity) { |
||
| 179 | $this->inputVerbosity = $inputVerbosity; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param $options |
||
| 184 | * |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | protected function is($options) { |
||
| 188 | return ($this->getInputVerbosity() & $options) === $options; |
||
| 189 | } |
||
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * @return resource |
||
| 194 | */ |
||
| 195 | protected function getInputStream() { |
||
| 196 | if ($this->inputStream === null) { |
||
| 197 | $this->inputStream = fopen('php://stdin', 'r'); |
||
| 198 | } |
||
| 199 | |||
| 200 | return $this->inputStream; |
||
| 201 | } |
||
| 202 | } |
||
| 203 |