|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* (c) Anton Medvedev <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Deployer\Documentation; |
|
12
|
|
|
|
|
13
|
|
|
class DocRecipe |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
public $recipeName; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
public $recipePath; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
public $comment; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var string[] |
|
29
|
|
|
*/ |
|
30
|
|
|
public $require = []; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var DocConfig[] |
|
33
|
|
|
*/ |
|
34
|
|
|
public $config = []; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var DocTask[] |
|
37
|
|
|
*/ |
|
38
|
|
|
public $tasks = []; |
|
39
|
|
|
|
|
40
|
|
|
public function __construct(string $recipeName, string $recipePath) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->recipeName = $recipeName; |
|
43
|
|
|
$this->recipePath = $recipePath; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return bool|int |
|
48
|
|
|
*/ |
|
49
|
|
|
public function parse(string $content) |
|
50
|
|
|
{ |
|
51
|
|
|
$comment = ''; |
|
52
|
|
|
$desc = ''; |
|
53
|
|
|
$currentTask = null; |
|
54
|
|
|
|
|
55
|
|
|
$content = str_replace("\r\n", "\n", $content); |
|
56
|
|
|
|
|
57
|
|
|
$state = 'root'; |
|
58
|
|
|
$lines = explode("\n", $content); |
|
59
|
|
|
|
|
60
|
|
|
for ($i = 0; $i < count($lines); $i++) { |
|
|
|
|
|
|
61
|
|
|
$line = $lines[$i]; |
|
62
|
|
|
|
|
63
|
|
|
if (empty($line)) { |
|
64
|
|
|
continue; // Skip empty lines |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$m = []; |
|
68
|
|
|
$match = function ($regexp) use ($line, &$m) { |
|
69
|
|
|
return preg_match("#$regexp#", $line, $m); |
|
70
|
|
|
}; |
|
71
|
|
|
switch ($state) { |
|
72
|
|
|
case 'root': |
|
73
|
|
|
if ($match('^/\*\*?')) { |
|
74
|
|
|
$state = 'comment'; |
|
75
|
|
|
$comment .= trim_comment($line) . "\n"; |
|
76
|
|
|
break; |
|
77
|
|
|
} |
|
78
|
|
|
if ($match('^//')) { |
|
79
|
|
|
$comment .= trim_comment($line) . "\n"; |
|
80
|
|
|
break; |
|
81
|
|
|
} |
|
82
|
|
|
if ($match('^require.+?[\'"](?<recipe>.+?)[\'"]')) { |
|
83
|
|
|
$this->require[] = dirname($this->recipePath) . $m['recipe']; |
|
84
|
|
|
break; |
|
85
|
|
|
} |
|
86
|
|
|
if ($match('^set\([\'"](?<config_name>[\w_:\-/]+?)[\'"]')) { |
|
87
|
|
|
$set = new DocConfig(); |
|
88
|
|
|
$set->name = $m['config_name']; |
|
89
|
|
|
$set->comment = trim($comment); |
|
90
|
|
|
$comment = ''; |
|
91
|
|
|
$set->recipePath = $this->recipePath; |
|
92
|
|
|
$set->lineNumber = $i + 1; |
|
93
|
|
|
if (preg_match('#^set\(.+?,\s(?<value>.+?)\);$#', $line, $m)) { |
|
94
|
|
|
$set->defaultValue = $m['value']; |
|
95
|
|
|
} |
|
96
|
|
|
if (preg_match('#^set\(.+?,\s\[$#', $line, $m)) { |
|
97
|
|
|
$multiLineArray = "[\n"; |
|
98
|
|
|
$line = $lines[++$i]; |
|
99
|
|
|
while (!preg_match('/^]/', $line)) { |
|
100
|
|
|
$multiLineArray .= $line . "\n"; |
|
101
|
|
|
$line = $lines[++$i]; |
|
102
|
|
|
} |
|
103
|
|
|
$multiLineArray .= "]"; |
|
104
|
|
|
$set->defaultValue = $multiLineArray; |
|
105
|
|
|
} |
|
106
|
|
|
if (preg_match('/^set\(.+?, function/', $line, $m)) { |
|
107
|
|
|
$body = []; |
|
108
|
|
|
$line = $lines[++$i]; |
|
109
|
|
|
while (!preg_match('/^}\);$/', $line)) { |
|
110
|
|
|
$body[] = trim($line); |
|
111
|
|
|
$line = $lines[++$i]; |
|
112
|
|
|
} |
|
113
|
|
|
if (count($body) === 1 && preg_match('/throw new/', $body[0])) { |
|
114
|
|
|
$set->comment .= "\n:::info Required\nThrows exception if not set.\n:::\n"; |
|
115
|
|
|
} elseif (count($body) <= 4) { |
|
116
|
|
|
$set->defaultValue = implode("\n", $body); |
|
117
|
|
|
} else { |
|
118
|
|
|
$set->comment .= "\n:::info Autogenerated\nThe value of this configuration is autogenerated on access.\n:::\n"; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
$this->config[$set->name] = $set; |
|
122
|
|
|
break; |
|
123
|
|
|
} |
|
124
|
|
|
if ($match('^desc\([\'"](?<desc>.+?)[\'"]\);$')) { |
|
125
|
|
|
$desc = $m['desc']; |
|
126
|
|
|
break; |
|
127
|
|
|
} |
|
128
|
|
|
if ($match('^task\([\'"](?<task_name>[\w_:-]+?)[\'"],\s\[$')) { |
|
129
|
|
|
$task = new DocTask(); |
|
130
|
|
|
$task->name = $m['task_name']; |
|
131
|
|
|
$task->desc = $desc; |
|
132
|
|
|
$task->comment = trim($comment); |
|
133
|
|
|
$comment = ''; |
|
134
|
|
|
$task->group = []; |
|
135
|
|
|
$task->recipePath = $this->recipePath; |
|
136
|
|
|
$task->lineNumber = $i + 1; |
|
137
|
|
|
$this->tasks[$task->name] = $task; |
|
138
|
|
|
$state = 'group_task'; |
|
139
|
|
|
$currentTask = $task; |
|
140
|
|
|
break; |
|
141
|
|
|
} |
|
142
|
|
|
if ($match('^task\([\'"](?<task_name>[\w_:-]+?)[\'"],')) { |
|
143
|
|
|
$task = new DocTask(); |
|
144
|
|
|
$task->name = $m['task_name']; |
|
145
|
|
|
$task->desc = $desc; |
|
146
|
|
|
$task->comment = trim($comment); |
|
147
|
|
|
$comment = ''; |
|
148
|
|
|
$task->recipePath = $this->recipePath; |
|
149
|
|
|
$task->lineNumber = $i + 1; |
|
150
|
|
|
$this->tasks[$task->name] = $task; |
|
151
|
|
|
break; |
|
152
|
|
|
} |
|
153
|
|
|
if ($match('^<\?php')) { |
|
154
|
|
|
break; |
|
155
|
|
|
} |
|
156
|
|
|
if ($match('^namespace Deployer;$')) { |
|
157
|
|
|
$this->comment = $comment; |
|
158
|
|
|
break; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$desc = ''; |
|
162
|
|
|
$comment = ''; |
|
163
|
|
|
break; |
|
164
|
|
|
|
|
165
|
|
|
case 'comment': |
|
166
|
|
|
if ($match('\*/\s*$')) { |
|
167
|
|
|
$state = 'root'; |
|
168
|
|
|
break; |
|
169
|
|
|
} |
|
170
|
|
|
$comment .= trim_comment($line) . "\n"; |
|
171
|
|
|
break; |
|
172
|
|
|
|
|
173
|
|
|
case 'group_task': |
|
174
|
|
|
if ($match('^\s+\'(?<task_name>[\w_:-]+?)\',$')) { |
|
175
|
|
|
$currentTask->group[] = $m['task_name']; |
|
176
|
|
|
break; |
|
177
|
|
|
} |
|
178
|
|
|
$state = 'root'; |
|
179
|
|
|
break; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
return false; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: