1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\AnnotatedCommand\Parser\Internal; |
3
|
|
|
|
4
|
|
|
use Consolidation\AnnotatedCommand\Parser\CommandInfo; |
5
|
|
|
use Consolidation\AnnotatedCommand\Parser\DefaultsWithDescriptions; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Given a class and method name, parse the annotations in the |
9
|
|
|
* DocBlock comment, and provide accessor methods for all of |
10
|
|
|
* the elements that are needed to create an annotated Command. |
11
|
|
|
*/ |
12
|
|
|
abstract class AbstractCommandDocBlockParser |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var CommandInfo |
16
|
|
|
*/ |
17
|
|
|
protected $commandInfo; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \ReflectionMethod |
21
|
|
|
*/ |
22
|
|
|
protected $reflection; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $tagProcessors = [ |
28
|
|
|
'command' => 'processCommandTag', |
29
|
|
|
'name' => 'processCommandTag', |
30
|
|
|
'arg' => 'processArgumentTag', |
31
|
|
|
'param' => 'processParamTag', |
32
|
|
|
'return' => 'processReturnTag', |
33
|
|
|
'option' => 'processOptionTag', |
34
|
|
|
'default' => 'processDefaultTag', |
35
|
|
|
'aliases' => 'processAliases', |
36
|
|
|
'usage' => 'processUsageTag', |
37
|
|
|
'description' => 'processAlternateDescriptionTag', |
38
|
|
|
'desc' => 'processAlternateDescriptionTag', |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
public function __construct(CommandInfo $commandInfo, \ReflectionMethod $reflection) |
42
|
|
|
{ |
43
|
|
|
$this->commandInfo = $commandInfo; |
44
|
|
|
$this->reflection = $reflection; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Parse the docBlock comment for this command, and set the |
49
|
|
|
* fields of this class with the data thereby obtained. |
50
|
|
|
*/ |
51
|
|
|
abstract public function parse(); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Save any tag that we do not explicitly recognize in the |
55
|
|
|
* 'otherAnnotations' map. |
56
|
|
|
*/ |
57
|
|
|
abstract protected function processGenericTag($tag); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set the name of the command from a @command or @name annotation. |
61
|
|
|
*/ |
62
|
|
|
abstract protected function processCommandTag($tag); |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* The @description and @desc annotations may be used in |
66
|
|
|
* place of the synopsis (which we call 'description'). |
67
|
|
|
* This is discouraged. |
68
|
|
|
* |
69
|
|
|
* @deprecated |
70
|
|
|
*/ |
71
|
|
|
abstract protected function processAlternateDescriptionTag($tag); |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Store the data from a @arg annotation in our argument descriptions. |
75
|
|
|
*/ |
76
|
|
|
abstract protected function processArgumentTag($tag); |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Store the data from a @param annotation in our argument descriptions. |
80
|
|
|
*/ |
81
|
|
|
abstract protected function processParamTag($tag); |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Store the data from a @return annotation in our argument descriptions. |
85
|
|
|
*/ |
86
|
|
|
abstract protected function processReturnTag($tag); |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Store the data from an @option annotation in our option descriptions. |
90
|
|
|
*/ |
91
|
|
|
abstract protected function processOptionTag($tag); |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Store the data from a @default annotation in our argument or option store, |
95
|
|
|
* as appropriate. |
96
|
|
|
*/ |
97
|
|
|
abstract protected function processDefaultTag($tag); |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Process the comma-separated list of aliases |
101
|
|
|
*/ |
102
|
|
|
abstract protected function processAliases($tag); |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Store the data from a @usage annotation in our example usage list. |
106
|
|
|
*/ |
107
|
|
|
abstract protected function processUsageTag($tag); |
108
|
|
|
|
109
|
|
|
protected function interpretDefaultValue($defaultValue) |
110
|
|
|
{ |
111
|
|
|
$defaults = [ |
112
|
|
|
'null' => null, |
113
|
|
|
'true' => true, |
114
|
|
|
'false' => false, |
115
|
|
|
"''" => '', |
116
|
|
|
'[]' => [], |
117
|
|
|
]; |
118
|
|
|
foreach ($defaults as $defaultName => $defaultTypedValue) { |
119
|
|
|
if ($defaultValue == $defaultName) { |
120
|
|
|
return $defaultTypedValue; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
return $defaultValue; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Given a docblock description in the form "$variable description", |
128
|
|
|
* return the variable name and description via the 'match' parameter. |
129
|
|
|
*/ |
130
|
|
|
protected function pregMatchNameAndDescription($source, &$match) |
131
|
|
|
{ |
132
|
|
|
$nameRegEx = '\\$(?P<name>[^ \t]+)[ \t]+'; |
133
|
|
|
$descriptionRegEx = '(?P<description>.*)'; |
134
|
|
|
$optionRegEx = "/{$nameRegEx}{$descriptionRegEx}/s"; |
135
|
|
|
|
136
|
|
|
return preg_match($optionRegEx, $source, $match); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
141
|
|
|
* convert the data into the last of these forms. |
142
|
|
|
*/ |
143
|
|
|
protected static function convertListToCommaSeparated($text) |
144
|
|
|
{ |
145
|
|
|
return preg_replace('#[ \t\n\r,]+#', ',', $text); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Take a multiline description and convert it into a single |
150
|
|
|
* long unbroken line. |
151
|
|
|
*/ |
152
|
|
|
protected static function removeLineBreaks($text) |
153
|
|
|
{ |
154
|
|
|
return trim(preg_replace('#[ \t\n\r]+#', ' ', $text)); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|