1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yarak\Console\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
use Symfony\Component\Console\Input\InputOption; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
|
9
|
|
|
class SignatureParser |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The command to build. |
13
|
|
|
* |
14
|
|
|
* @var Command |
15
|
|
|
*/ |
16
|
|
|
protected $command; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Construct. |
20
|
|
|
* |
21
|
|
|
* @param Command $command |
22
|
|
|
*/ |
23
|
|
|
public function __construct(Command $command) |
24
|
|
|
{ |
25
|
|
|
$this->command = $command; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Parse the command signature. |
30
|
|
|
* |
31
|
|
|
* @param string $signature |
32
|
|
|
*/ |
33
|
|
|
public function parse($signature) |
34
|
|
|
{ |
35
|
|
|
$this->setName($signature); |
36
|
|
|
|
37
|
|
|
$argumentsOptions = $this->extractArgumentsOptions($signature); |
38
|
|
|
|
39
|
|
|
foreach ($argumentsOptions as $value) { |
40
|
|
|
if (substr($value, 0, 2) !== '--') { |
41
|
|
|
$parser = new ArgumentParser($this->command); |
42
|
|
|
} else { |
43
|
|
|
$parser = new OptionParser($this->command); |
44
|
|
|
|
45
|
|
|
$value = trim($value, '--'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$parser->handle($value); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Set the command name. |
54
|
|
|
* |
55
|
|
|
* @param string $signature |
56
|
|
|
*/ |
57
|
|
|
protected function setName($signature) |
58
|
|
|
{ |
59
|
|
|
$this->command->setName(preg_split('/\s+/', $signature)[0]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Extract arguments and options from signature. |
64
|
|
|
* |
65
|
|
|
* @param string $signature |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
protected function extractArgumentsOptions($signature) |
70
|
|
|
{ |
71
|
|
|
preg_match_all('/{(.*?)}/', $signature, $argumentsOption); |
72
|
|
|
|
73
|
|
|
return array_map(function ($item) { |
74
|
|
|
return trim($item, '{}'); |
75
|
|
|
}, $argumentsOption[1]); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|