X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fdrupalmoduleupgrader%2Fsrc%2FPlugin%2FDMU%2FConverter%2FGrep.php;fp=web%2Fmodules%2Fcontrib%2Fdrupalmoduleupgrader%2Fsrc%2FPlugin%2FDMU%2FConverter%2FGrep.php;h=7fbf063d6064fc917c19ddfe1605917652a761b0;hp=0000000000000000000000000000000000000000;hb=8acec36f19c470dfcda1ae2336826a782f41874c;hpb=e0411c4e83ba0d079034db83c3f7f55be24a0e35 diff --git a/web/modules/contrib/drupalmoduleupgrader/src/Plugin/DMU/Converter/Grep.php b/web/modules/contrib/drupalmoduleupgrader/src/Plugin/DMU/Converter/Grep.php new file mode 100644 index 000000000..7fbf063d6 --- /dev/null +++ b/web/modules/contrib/drupalmoduleupgrader/src/Plugin/DMU/Converter/Grep.php @@ -0,0 +1,80 @@ + $replacement) { + $this->targets['global $' . $variable . ';'] = '$' . $variable . ' = ' . $replacement . ';'; + $this->targets['$GLOBALS[\'' . $variable . '\']'] = $replacement; + $this->targets['$GLOBALS["' . $variable . '"]'] = $replacement; + } + foreach ($configuration['constants'] as $constant => $replacement) { + $this->targets[$constant] = $replacement; + } + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $container->get('config.factory')->get('drupalmoduleupgrader.grep')->get('definitions'), + $plugin_id, + $plugin_definition, + $container->get('string_translation'), + $container->get('logger.factory')->get('drupalmoduleupgrader') + ); + } + + /** + * {@inheritdoc} + */ + 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())); + } + } + +}