ConverterBase.php
9.18 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
namespace Drupal\drupalmoduleupgrader;
use Drupal\Component\Serialization\Yaml;
use Drupal\drupalmoduleupgrader\Utility\Filter\ContainsLogicFilter;
use Drupal\drupalmoduleupgrader\Utility\Filter\FunctionCallArgumentFilter;
use Pharborist\DocCommentNode;
use Pharborist\Filter;
use Pharborist\Functions\FunctionCallNode;
use Pharborist\Functions\FunctionDeclarationNode;
use Pharborist\Functions\ParameterNode;
use Pharborist\LineCommentBlockNode;
use Pharborist\Objects\ClassNode;
use Pharborist\Parser;
use Pharborist\Variables\VariableNode;
use Pharborist\WhitespaceNode;
use Symfony\Component\Filesystem\Filesystem;
/**
* Base class for converters.
*/
abstract class ConverterBase extends PluginBase implements ConverterInterface {
// Used by buildFixMe() to determine the comment style of the generated
// FIXME notice.
const LINE_COMMENT = '//';
const DOC_COMMENT = '/**/';
/**
* {@inheritdoc}
*/
public function isExecutable(TargetInterface $target) {
// If the plugin applies to particular hook(s), only return TRUE if the
// target module implements any of the hooks. Otherwise, return TRUE
// unconditionally.
if (isset($this->pluginDefinition['hook'])) {
return (boolean) array_filter((array) $this->pluginDefinition['hook'], [ $target->getIndexer('function'), 'has' ]);
}
else {
return TRUE;
}
}
/**
* Executes the target module's implementation of the specified hook, and
* returns the result.
*
* @return mixed
*
* @throws \LogicException if the target module doesn't implement the
* specified hook, or if the implementation contains logic.
*
* @deprecated
*/
protected function executeHook(TargetInterface $target, $hook) {
$indexer = $target->getIndexer('function');
if ($indexer->has($hook)) {
// Configure the ContainsLogicFilter so that certain "safe" functions
// will pass it.
$has_logic = new ContainsLogicFilter();
$has_logic->whitelist('t');
$has_logic->whitelist('drupal_get_path');
$function = $indexer->get($hook);
if ($function->is($has_logic)) {
throw new \LogicException('{target}_{hook} cannot be executed because it contains logic.');
}
else {
$function_name = $function->getName()->getText();
if (! function_exists($function_name)) {
eval($function->getText());
}
return call_user_func($function_name);
}
}
else {
throw new \LogicException('{target} does not implement hook_{hook}.');
}
}
/**
* Creates an empty implementation of a hook.
*
* @param TargetInterface $target
* The target module.
* @param string $hook
* The hook to implement, without the hook_ prefix.
*
* @return \Pharborist\Functions\FunctionDeclarationNode
* The hook implementation, appended to the main module file.
*/
protected function implement(TargetInterface $target, $hook) {
$function = FunctionDeclarationNode::create($target->id() . '_' . $hook);
$function->setDocComment(DocCommentNode::create('Implements hook_' . $hook . '().'));
$module_file = $target->getPath('.module');
$target->open($module_file)->append($function);
WhitespaceNode::create("\n")->insertBefore($function);
WhitespaceNode::create("\n")->insertAfter($function);
return $function;
}
/**
* Writes a file to the target module's directory.
*
* @param TargetInterface $target
* The target module.
* @param string $path
* The path of the file to write, relative to the module root.
* @param string $data
* The file contents.
*
* @return string
* The path of the file, including the target's base path.
*/
public function write(TargetInterface $target, $path, $data) {
static $fs;
if (empty($fs)) {
$fs = new Filesystem();
}
$destination_path = $target->getPath($path);
$fs->dumpFile($destination_path, (string) $data);
return $destination_path;
}
/**
* Writes a class to the target module's PSR-4 root.
*
* @param TargetInterface $target
* The target module.
* @param ClassNode $class
* The class to write. The path will be determined from the class'
* fully qualified name.
*
* @return string
* The generated path to the class.
*/
public function writeClass(TargetInterface $target, ClassNode $class) {
$class_path = ltrim($class->getName()->getAbsolutePath(), '\\');
$path = str_replace([ 'Drupal\\' . $target->id(), '\\', ], [ 'src', '/' ], $class_path) . '.php';
return $this->write($target, $path, $class->parents()->get(0));
}
/**
* Writes out arbitrary data in YAML format.
*
* @param TargetInterface $target
* The target module.
* @param string $group
* The name of the YAML file. It will be prefixed with the module's machine
* name and suffixed with .yml. For example, a group value of 'routing'
* will write MODULE.routing.yml.
* @param array $data
* The data to write.
*
* @todo This should be writeYAML, not writeInfo.
*/
protected function writeInfo(TargetInterface $target, $group, array $data) {
$destination = $target->getPath('.' . $group . '.yml');
file_put_contents($destination, Yaml::encode($data));
}
/**
* Writes a service definition to the target module's services.yml file.
*
* @param TargetInterface $target
* The target module.
* @param string $service_id
* The service ID. If an existing one with the same ID already exists,
* it will be overwritten.
* @param array $service_definition
*/
protected function writeService(TargetInterface $target, $service_id, array $service_definition) {
$services = $target->getServices();
$services->set($service_id, $service_definition);
$this->writeInfo($target, 'services', [ 'services' => $services->toArray() ]);
}
/**
* Parses a generated class into a syntax tree.
*
* @param string|array $class
* The class to parse, either as a string of PHP code or a renderable array.
*
* @return \Pharborist\Objects\ClassNode
*/
protected function parse($class) {
if (is_array($class)) {
$class = \Drupal::service('renderer')->renderPlain($class);
}
return Parser::parseSnippet($class)->find(Filter::isInstanceOf('Pharborist\Objects\ClassNode'))[0];
}
/**
* Builds a FIXME notice using either the text in the plugin definition,
* or passed-in text.
*
* @param string|NULL $text
* The FIXME notice's text, with variable placeholders and no translation.
* @param array $variables
* Optional variables to use in translation. If empty, the FIXME will not
* be translated.
* @param string|NULL $style
* The comment style. Returns a LineCommentBlockNode if this is set to
* self::LINE_COMMENT, a DocCommentNode if self::DOC_COMMENT, or the FIXME
* as a string if set to anything else.
*
* @return mixed
*/
protected function buildFixMe($text = NULL, array $variables = [], $style = self::LINE_COMMENT) {
$fixMe = "@FIXME\n" . ($text ?: $this->pluginDefinition['fixme']);
if (isset($this->pluginDefinition['documentation'])) {
$fixMe .= "\n";
foreach ($this->pluginDefinition['documentation'] as $doc) {
$fixMe .= "\n@see ";
$fixMe .= (isset($doc['url']) ? $doc['url'] : (string) $doc);
}
}
if ($variables) {
$fixMe = $this->t($fixMe, $variables);
}
switch ($style) {
case self::LINE_COMMENT:
return LineCommentBlockNode::create($fixMe);
case self::DOC_COMMENT:
return DocCommentNode::create($fixMe);
default:
return $fixMe;
}
}
/**
* Parametrically rewrites a function.
*
* @param \Drupal\drupalmoduleupgrader\RewriterInterface $rewriter
* A fully configured parametric rewriter.
* @param \Pharborist\Functions\ParameterNode $parameter
* The parameter upon which to base the rewrite.
* @param TargetInterface $target
* The target module.
* @param boolean $recursive
* If TRUE, rewriting will recurse into called functions which are passed
* the rewritten parameter as an argument.
*/
protected function rewriteFunction(RewriterInterface $rewriter, ParameterNode $parameter, TargetInterface $target, $recursive = TRUE) {
$rewriter->rewrite($parameter);
$target->save($parameter);
// Find function calls within the rewritten function which are called
// with the rewritten parameter.
$indexer = $target->getIndexer('function');
$next = $parameter
->getFunction()
->find(new FunctionCallArgumentFilter($parameter->getName()))
->filter(function(FunctionCallNode $call) use ($indexer) {
return $indexer->has($call->getName()->getText());
});
/** @var \Pharborist\Functions\FunctionCallNode $call */
foreach ($next as $call) {
/** @var \Pharborist\Functions\FunctionDeclarationNode $function */
$function = $indexer->get($call->getName()->getText());
foreach ($call->getArguments() as $index => $argument) {
if ($argument instanceof VariableNode && $argument->getName() == $parameter->getName()) {
$this->rewriteFunction($rewriter, $function->getParameterAtIndex($index), $target, $recursive);
break;
}
}
}
}
}