Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Plugin / DMU / Converter / Functions / CToolsGetPlugins.php
1 <?php
2
3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter\Functions;
4
5 use Drupal\drupalmoduleupgrader\TargetInterface;
6 use Pharborist\Functions\FunctionCallNode;
7 use Pharborist\Objects\ClassMethodCallNode;
8 use Pharborist\Types\StringNode;
9
10 /**
11  * @Converter(
12  *  id = "ctools_get_plugins",
13  *  description = @Translation("Rewrites calls to ctools_get_plugins().")
14  * )
15  */
16 class CToolsGetPlugins extends FunctionCallModifier {
17
18   /**
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.
22    *
23    * @param \Pharborist\Functions\FunctionCallNode $call
24    *  The function call to test.
25    * @param \Drupal\drupalmoduleupgrader\TargetInterface $target
26    *  The target module.
27    *
28    * @return boolean
29    */
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);
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public function rewrite(FunctionCallNode $call, TargetInterface $target) {
39     if (! $this->canRewrite($call, $target)) {
40       return NULL;
41     }
42
43     $arguments = $call->getArguments();
44     $plugin_owner = $arguments[0]->toValue();
45     $plugin_type = $arguments[1]->toValue();
46
47     $services = $target->getServices();
48     $service_id = 'plugin.manager.' . $plugin_owner . '.' . $plugin_type;
49     $services->set($service_id, [
50       'class' => 'Drupal\Core\Plugin\DefaultPluginManager',
51       'arguments' => [
52         'Plugin/' . $plugin_owner . '/' . $plugin_type,
53         '@container.namespaces',
54         '@module_handler',
55         'Drupal\Component\Plugin\PluginBase',
56         'Drupal\Component\Annotation\Plugin',
57       ],
58     ]);
59     $this->writeInfo($target, 'services', [ 'services' => $services->toArray() ]);
60
61     return ClassMethodCallNode::create('\Drupal', 'service')
62       ->appendArgument($service_id)
63       ->appendMethodCall('getDefinitions');
64   }
65
66 }