3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter\Functions;
5 use Drupal\drupalmoduleupgrader\TargetInterface;
6 use Pharborist\Functions\FunctionCallNode;
7 use Pharborist\Objects\ClassMethodCallNode;
8 use Pharborist\Types\StringNode;
12 * id = "ctools_get_plugins",
13 * description = @Translation("Rewrites calls to ctools_get_plugins().")
16 class CToolsGetPlugins extends FunctionCallModifier {
19 * Tests if the function call can be rewritten at all, which it will be
20 * only if both arguments are strings, and the first argument is the machine
21 * name of the target module.
23 * @param \Pharborist\Functions\FunctionCallNode $call
24 * The function call to test.
25 * @param \Drupal\drupalmoduleupgrader\TargetInterface $target
30 public function canRewrite(FunctionCallNode $call, TargetInterface $target) {
31 $arguments = $call->getArguments();
32 return ($arguments[0] instanceof StringNode && $arguments[0]->toValue() == $target->id() && $arguments[1] instanceof StringNode);
38 public function rewrite(FunctionCallNode $call, TargetInterface $target) {
39 if (! $this->canRewrite($call, $target)) {
43 $arguments = $call->getArguments();
44 $plugin_owner = $arguments[0]->toValue();
45 $plugin_type = $arguments[1]->toValue();
47 $services = $target->getServices();
48 $service_id = 'plugin.manager.' . $plugin_owner . '.' . $plugin_type;
49 $services->set($service_id, [
50 'class' => 'Drupal\Core\Plugin\DefaultPluginManager',
52 'Plugin/' . $plugin_owner . '/' . $plugin_type,
53 '@container.namespaces',
55 'Drupal\Component\Plugin\PluginBase',
56 'Drupal\Component\Annotation\Plugin',
59 $this->writeInfo($target, 'services', [ 'services' => $services->toArray() ]);
61 return ClassMethodCallNode::create('\Drupal', 'service')
62 ->appendArgument($service_id)
63 ->appendMethodCall('getDefinitions');