|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class ArgumentParser | ArgumentParser.php |
|
4
|
|
|
* @package ORM\Console |
|
5
|
|
|
* @author Florian Knapp <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Faulancer\Console; |
|
8
|
|
|
|
|
9
|
|
|
use Faulancer\Service\Config; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class ArgumentParser |
|
13
|
|
|
*/ |
|
14
|
|
|
class ArgumentParser |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** @var array */ |
|
18
|
|
|
protected $arguments = []; |
|
19
|
|
|
|
|
20
|
|
|
/** @var array */ |
|
21
|
|
|
protected $config; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* ArgumentParser constructor. |
|
25
|
|
|
* @param array $argv |
|
26
|
|
|
* @param Config $config |
|
27
|
|
|
* @codeCoverageIgnore |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct($argv, $config) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->config = $config; |
|
|
|
|
|
|
32
|
|
|
$this->parseInput($argv); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param array $argv |
|
37
|
|
|
* @return ConsoleInterface |
|
38
|
|
|
* @throws \Exception |
|
39
|
|
|
* @codeCoverageIgnore |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function parseInput(array $argv) |
|
42
|
|
|
{ |
|
43
|
|
|
$args = array_splice($argv, 1, count($argv)); |
|
44
|
|
|
|
|
45
|
|
|
if (empty($args)) { |
|
46
|
|
|
throw new \Exception('Not enough parameters given.'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
for ($i = 0; $i < count($args); $i++) { |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
if (strpos($args[$i], '-') === false) { |
|
52
|
|
|
continue; |
|
53
|
|
|
} else if (empty($args[$i+1])) { |
|
54
|
|
|
break; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->set(str_replace('-', '', $args[$i]), $args[$i+1]); |
|
58
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (strpos($args[0], ':') !== false) { |
|
62
|
|
|
|
|
63
|
|
|
$parts = explode(':', $args[0]); |
|
64
|
|
|
$class = $parts[0]; |
|
65
|
|
|
$method = $parts[1] . 'Action'; |
|
66
|
|
|
$ns = '\Faulancer\Console\Handler\\' . ucfirst($class); |
|
67
|
|
|
|
|
68
|
|
|
$class = new $ns($this->config, $this); |
|
69
|
|
|
|
|
70
|
|
|
return call_user_func([$class, $method], $this); |
|
71
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
throw new \Exception('No matching handler found'); |
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string $arg |
|
80
|
|
|
* @param string $value |
|
81
|
|
|
* @codeCoverageIgnore |
|
82
|
|
|
*/ |
|
83
|
|
|
public function set($arg, $value) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->arguments[$arg] = $value; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $arg |
|
90
|
|
|
* @return string |
|
91
|
|
|
* @codeCoverageIgnore |
|
92
|
|
|
*/ |
|
93
|
|
|
public function get($arg) |
|
94
|
|
|
{ |
|
95
|
|
|
if (empty($this->arguments[$arg])) { |
|
96
|
|
|
return ''; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $this->arguments[$arg]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..