You are here

public function Grep::convert in Drupal 7 to 8/9 Module Upgrader 8

Performs required conversions.

Parameters

TargetInterface $target: The target module to convert.

Overrides ConverterInterface::convert

File

src/Plugin/DMU/Converter/Grep.php, line 54

Class

Grep
Plugin annotation @Converter( id = "grep", description = @Translation("Searches for and replaces commonly-used code that has changed in Drupal 8.") )

Namespace

Drupal\drupalmoduleupgrader\Plugin\DMU\Converter

Code

public function convert(TargetInterface $target) {
  foreach ($this->configuration['function_calls'] as $function => $replace_with) {
    $function_calls = $target
      ->getIndexer('function_call')
      ->get($function);
    foreach ($function_calls as $function_call) {
      $rewritten = str_ireplace($function, $replace_with, $function_call
        ->getText());
      $node = Parser::parseExpression($rewritten);
      $function_call
        ->replaceWith($node);
      $target
        ->save($node);
    }
  }

  // Flush other open syntax trees to ensure that other plugins don't clobber
  // our changes later.
  $target
    ->flush();
  foreach ($target
    ->getFinder() as $file) {

    // Load in the entire contents of the module. This is criminally inefficient
    // and wasteful of memory and should eventually be refactored into something
    // a little more...I dunno, sustainable.

    /** @var \Symfony\Component\Finder\SplFileInfo $file */
    $search = array_keys($this->targets);
    $replace = array_values($this->targets);
    file_put_contents($file
      ->getPathname(), str_replace($search, $replace, $file
      ->getContents()));
  }
}