7fbf063d6064fc917c19ddfe1605917652a761b0
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Plugin / DMU / Converter / Grep.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\TargetInterface;
8 use Pharborist\Parser;
9 use Psr\Log\LoggerInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * @Converter(
14  *  id = "grep",
15  *  description = @Translation("Searches for and replaces commonly-used code that has changed in Drupal 8.")
16  * )
17  */
18 class Grep extends ConverterBase {
19
20   /**
21    * @var string[]
22    */
23   private $targets = [];
24
25   public function __construct(array $configuration, $plugin_id, $plugin_definition, TranslationInterface $translator, LoggerInterface $log) {
26     parent::__construct($configuration, $plugin_id, $plugin_definition, $translator, $log);
27
28     foreach ($configuration['globals'] as $variable => $replacement) {
29       $this->targets['global $' . $variable . ';'] = '$' . $variable . ' = ' . $replacement . ';';
30       $this->targets['$GLOBALS[\'' . $variable . '\']'] = $replacement;
31       $this->targets['$GLOBALS["' . $variable . '"]'] = $replacement;
32     }
33     foreach ($configuration['constants'] as $constant => $replacement) {
34       $this->targets[$constant] = $replacement;
35     }
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
42     return new static(
43       $container->get('config.factory')->get('drupalmoduleupgrader.grep')->get('definitions'),
44       $plugin_id,
45       $plugin_definition,
46       $container->get('string_translation'),
47       $container->get('logger.factory')->get('drupalmoduleupgrader')
48     );
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public function convert(TargetInterface $target) {
55     foreach ($this->configuration['function_calls'] as $function => $replace_with) {
56       $function_calls = $target->getIndexer('function_call')->get($function);
57       foreach ($function_calls as $function_call) {
58         $rewritten = str_ireplace($function, $replace_with, $function_call->getText());
59         $node = Parser::parseExpression($rewritten);
60         $function_call->replaceWith($node);
61         $target->save($node);
62       }
63     }
64
65     // Flush other open syntax trees to ensure that other plugins don't clobber
66     // our changes later.
67     $target->flush();
68
69     foreach ($target->getFinder() as $file) {
70       // Load in the entire contents of the module. This is criminally inefficient
71       // and wasteful of memory and should eventually be refactored into something
72       // a little more...I dunno, sustainable.
73       /** @var \Symfony\Component\Finder\SplFileInfo $file */
74       $search = array_keys($this->targets);
75       $replace = array_values($this->targets);
76       file_put_contents($file->getPathname(), str_replace($search, $replace, $file->getContents()));
77     }
78   }
79
80 }