Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Plugin / DMU / Converter / UserHooks.php
1 <?php
2
3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter;
4
5 use Drupal\Core\StringTranslation\TranslationInterface;
6 use Drupal\drupalmoduleupgrader\ConverterBase;
7 use Drupal\drupalmoduleupgrader\RewriterInterface;
8 use Drupal\drupalmoduleupgrader\TargetInterface;
9 use Pharborist\DocCommentNode;
10 use Pharborist\Types\NullNode;
11 use Psr\Log\LoggerInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * @Converter(
16  *  id = "user_hooks",
17  *  description = @Translation("Alters implementations of hook_user_insert(), hook_user_presave(), and hook_user_update()."),
18  *  hook = {
19  *    "hook_user_insert",
20  *    "hook_user_presave",
21  *    "hook_user_update"
22  *  }
23  * )
24  */
25 class UserHooks extends ConverterBase {
26
27   /**
28    * @var \Drupal\drupalmoduleupgrader\RewriterInterface
29    */
30   protected $rewriter;
31
32   public function __construct(array $configuration, $plugin_id, $plugin_definition, TranslationInterface $translator, LoggerInterface $log, RewriterInterface $rewriter) {
33     parent::__construct($configuration, $plugin_id, $plugin_definition, $translator, $log);
34     $this->rewriter = $rewriter;
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
41     return new static(
42       $configuration,
43       $plugin_id,
44       $plugin_definition,
45       $container->get('string_translation'),
46       $container->get('logger.factory')->get('drupalmoduleupgrader'),
47       $container->get('plugin.manager.drupalmoduleupgrader.rewriter')->createInstance('_rewriter:user')
48     );
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public function convert(TargetInterface $target) {
55     $indexer = $target->getIndexer('function');
56
57     $hooks = array_filter($this->pluginDefinition['hook'], [$indexer, 'has']);
58     foreach ($hooks as $hook) {
59       /** @var \Pharborist\Functions\FunctionDeclarationNode $function */
60       $function = $indexer->get($hook);
61       // The $edit parameter is defunct in Drupal 8, but we'll leave it in
62       // there as an empty array to prevent errors, and move it to the back
63       // of the line.
64       /** @var \Pharborist\Functions\ParameterNode $edit */
65       $edit = $function->getParameterList()->shift()->setReference(FALSE)->setValue(NullNode::create());
66       $function->appendParameter($edit);
67
68       // Slap a FIXME on the hook implementation, informing the developer that
69       // $edit and $category are dead.
70       $comment = $function->getDocComment();
71       $comment_text = $comment ? $comment->getCommentText() : '';
72       if ($comment_text) {
73         $comment_text .= "\n\n";
74       }
75       $comment_text .= <<<'END'
76 @FIXME
77 The $edit and $category parameters are gone in Drupal 8. They have been left
78 here in order to prevent 'undefined variable' errors, but they will never
79 actually be passed to this hook. You'll need to modify this function and
80 remove every reference to them.
81 END;
82       $function->setDocComment(DocCommentNode::create($comment_text));
83
84       $this->rewriteFunction($this->rewriter, $function->getParameterAtIndex(0), $target);
85       $target->save($function);
86     }
87   }
88
89 }