-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathModuleCodeFile.php
More file actions
251 lines (207 loc) · 8.15 KB
/
ModuleCodeFile.php
File metadata and controls
251 lines (207 loc) · 8.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
namespace DrupalCodeBuilder\Generator;
use DrupalCodeBuilder\File\DrupalExtension;
/**
* Generator class for module code files.
*/
class ModuleCodeFile extends PHPFile {
/**
* Whether this file is merged with existing code.
*
* @todo Move this up the class hierarchy to PHPFIle when it's used there and
* in all child classes.
*
* @var bool
*/
protected $merged = FALSE;
/**
* {@inheritdoc}
*/
public function getMergeTag() {
return $this->component_data['filename'];
}
/**
* {@inheritdoc}
*/
public function detectExistence(DrupalExtension $extension) {
$this->exists = $extension->hasFile($this->getFilename());
$this->extension = $extension;
}
/**
* Build the code files.
*/
public function getFileInfo() {
// Create a build list tag from the filename.
$filename_pieces = explode('.', $this->component_data['filename']);
if ($filename_pieces[0] == '%module') {
// Take off the module name from the front.
array_shift($filename_pieces);
}
if (in_array(end($filename_pieces), ['php', 'inc'])) {
// Take off a file extenstion.
array_pop($filename_pieces);
}
// Implode whatever's left.
$file_key_tag = implode('.', $filename_pieces);
return [
'path' => '', // Means base folder.
'filename' => $this->component_data['filename'],
'body' => $this->fileContents(),
'build_list_tags' => ['code', $file_key_tag],
'merged' => $this->merged,
];
}
/**
* Return the main body of the file code.
*
* @return
* An array of code lines. Keys are immaterial but should avoid clashing.
*/
function phpCodeBody() {
// Keep each function separate for now, so they can be ordered.
$function_code = [];
// Array of the names of generated functions.
$generated_function_names = [];
$generated_function_names_without_suffixes = [];
foreach ($this->containedComponents['function'] as $key => $child_item) {
// Subtle (and brittle) point: call getContents first so that any value
// replacement can take place. E.g. HookUpdateN setting the schema number.
$function_lines = $child_item->getContents();
$function_name = $child_item->component_data->function_name->value;
$function_name = str_replace('%module', $this->component_data['root_component_name'], $function_name);
$generated_function_names[$function_name] = TRUE;
if (preg_match('@\d+$@', $function_name)) {
$generated_function_names_without_suffixes[$function_name] = preg_replace('@\d+$@', '', $function_name);
}
// Blank line after the function.
$function_lines[] = '';
$function_code[$function_name] = $function_lines;
}
// Merge any existing functions.
$existing_function_order = [];
$existing_function_names_without_suffixes = [];
if ($this->exists) {
$ast = $this->extension->getFileAST($this->getFilename());
$existing_function_nodes = $this->extension->getASTFunctions($ast);
if ($existing_function_nodes) {
$this->merged = TRUE;
}
// The first function in the AST might have an additional comment block for
// the @file tag. Remove this, since we will generate it ourselves.
foreach ($ast as $ast_node) {
if ($ast_node->getType() != 'Stmt_Function') {
continue;
}
$first_function = $ast_node;
$comments = $first_function->getAttribute('comments');
// Allow for crappy code with missing docblock.
if ($comments) {
$docblock = end($comments);
$first_function->setAttribute('comments', [$docblock]);
}
// Only act on the first function.
break;
}
// Add functions from the existing file, unless we are generating them
// too, in which case we assume that our version is better.
// Keep track of their order.
$index = 0;
foreach ($existing_function_nodes as $function_node) {
$existing_function_name = (string) $function_node->name;
$existing_function_order[$existing_function_name] = $index;
$index++;
if (preg_match('@\d+$@', $existing_function_name)) {
$existing_function_names_without_suffixes[$existing_function_name] = preg_replace('@\d+$@', '', $existing_function_name);
}
// Skip if the function has already been generated: a generated function
// overwrites an existing one.
if (isset($generated_function_names[$existing_function_name])) {
continue;
}
if ($comments = $function_node->getAttribute('comments')) {
$first_line = $comments[0]->getStartLine();
}
else {
$first_line = $function_node->getStartLine();
}
$end_line = $function_node->getEndLine() + 1;
$function_code[$existing_function_name] = $this->extension->getFileLines($this->getFilename(), $first_line, $end_line);
}
}
// Assemble the code.
$code_body = [];
// Generated functions go first, unless they match existing functions to
// within a numeric suffix.
foreach (array_keys($generated_function_names) as $function_name) {
// This matches an existing function to within a suffix: leave it for now
// because we insert it among existing functions.
if (
isset($generated_function_names_without_suffixes[$function_name]) &&
in_array($generated_function_names_without_suffixes[$function_name], $existing_function_names_without_suffixes)
) {
continue;
}
$code_body[$function_name] = $function_code[$function_name];
}
foreach (array_keys($existing_function_order) as $function_name) {
// Add the function.
$code_body[$function_name] = $function_code[$function_name];
// Now see if a generated function that we held over needs to go next.
if (isset($existing_function_names_without_suffixes[$function_name])) {
// Make a function name with the suffix incremented by 1 and see if it
// exists in the generated functions.
$numeric_suffix = substr($function_name, strlen($existing_function_names_without_suffixes[$function_name]));
$potential_generated_function_name = $existing_function_names_without_suffixes[$function_name] . ($numeric_suffix + 1);
if (isset($generated_function_names[$potential_generated_function_name])) {
$code_body[$potential_generated_function_name] = $function_code[$potential_generated_function_name];
}
}
}
if (empty($code_body)) {
// If there are no functions, then this is a .module file that's been
// requested so the module is correctly formed. It is customary to add a
// comment to the file for DX.
$code_body['empty'] = "// Drupal needs this blank file.";
$code_body[] = '';
}
else {
// Merge the arrays of code lines for each function.
$code_body = array_merge(...array_values($code_body));
}
// Replace any fully-qualified classes with short class names, and keep a
// list of the replacements to make import statements with.
$imported_classes = [];
$this->extractFullyQualifiedClasses($code_body, $imported_classes);
// Merge any existing import statements.
if ($this->exists) {
$existing_import_nodes = $this->extension->getASTImports($ast);
foreach ($existing_import_nodes as $import_node) {
$existing_import = $import_node->uses[0]->name->toString();
$imported_classes[] = $existing_import;
}
}
$return = array_merge(
$this->imports($imported_classes),
$code_body
);
return $return;
}
/**
* {@inheritdoc}
*/
function fileDocblockSummary() {
$filename_pieces = explode('.', $this->component_data['filename']);
return match (end($filename_pieces)) {
'module' => 'Contains hook implementations for the %readable module.',
'install' => 'Contains install and update hooks for the %readable module.',
default => parent::fileDocblockSummary(),
};
}
/**
* {@inheritdoc}
*/
function code_footer() {
$footer = \DrupalCodeBuilder\Factory::getEnvironment()->getSetting('footer', NULL);
return $footer;
}
}