/** * {@inheritdoc} */ public function convert(TargetInterface $target) { // If the hook implementation contains logic, we cannot convert it and // that's that. So we'll leave a FIXME and bail out. /** @var \Pharborist\Functions\FunctionDeclarationNode $hook */ $hook = $target->getIndexer('function')->get('hook_menu'); if ($hook->is(new ContainsLogicFilter())) { $hook->setDocComment(DocCommentNode::create($this->pluginDefinition['fixme'])); $target->save($hook); return; } $hook_menu = new HookMenu($target, $this->routeConverters); foreach ($hook_menu->getSourceRoutes() as $path => $route) { /** @var \Drupal\drupalmoduleupgrader\Routing\Drupal7\RouteWrapper $route */ if ($route->containsKey('page callback')) { $plugin_id = $this->routeConverters->hasDefinition($route['page callback']) ? $route['page callback'] : 'default'; /** @var \Drupal\drupalmoduleupgrader\Routing\RouteConverterInterface $converter */ $this->routeConverters->createInstance($plugin_id)->buildRoute($target, $route); } } $routing = []; foreach ($hook_menu->getDestinationRoutes() as $name => $route) { $routing[$name] = ['path' => $route->getPath()->__toString(), 'defaults' => $route->getDefaults(), 'requirements' => $route->getRequirements()]; } $this->writeInfo($target, 'routing', $routing); }
/** * {@inheritdoc} */ public function convert(TargetInterface $target) { $indexer = $target->getIndexer('function'); // @FIXME This is not working (returns empty result set)...don't know why. $alter_hooks = $indexer->getQuery()->condition(db_or()->condition('id', $target->id() . '_form_alter')->condition('id', db_like($target->id() . '_form_%_alter'), 'LIKE'))->execute(); foreach ($alter_hooks as $alter_hook) { /** @var \Pharborist\Functions\FunctionDeclarationNode $function */ $function = $indexer->get($alter_hook->id); $parameters = $function->getParameters(); if (sizeof($parameters) > 1) { $parameters[1]->setTypeHint('\\Drupal\\Core\\Form\\FormStateInterface'); $target->save($function); } } }
/** * {@inheritdoc} */ public function convert(TargetInterface $target) { /** @var \Pharborist\Functions\FunctionDeclarationNode $function */ $function = $target->getIndexer('function')->get('hook_node_prepare'); // foo_node_prepare() --> foo_node_prepare_form(). $function->setName($function->getName() . '_form'); // The first parameter is a node, so rewrite the function accordingly. $this->rewriters->createInstance('_entity:node')->rewrite($function->getParameterAtIndex(0)); // Create the $operation parameter. $function->appendParameter(ParameterNode::create('operation')); // Create the $form_state parameter. $form_state = ParameterNode::create('form_state')->setTypeHint('\\Drupal\\Core\\Form\\FormStateInterface'); $function->appendParameter($form_state); $target->save($function); }
/** * {@inheritdoc} */ public function convert(TargetInterface $target) { // If the hook implementation contains logic, we cannot convert it and // that's that. So we'll leave a FIXME and bail out. /** @var \Pharborist\Functions\FunctionDeclarationNode $hook */ $hook = $target->getIndexer('function')->get('hook_menu'); if ($hook->is(new ContainsLogicFilter())) { $hook->setDocComment(DocCommentNode::create($this->pluginDefinition['fixme'])); $target->save($hook); return; } // Links are split out by group because there are separate config files // for each link type. $links = ['menu' => new LinkIndex(), 'task' => new LinkIndex(), 'action' => new LinkIndex(), 'contextual' => new LinkIndex()]; $hook_menu = new HookMenu($target, $this->routeConverters); foreach ($hook_menu->getSourceRoutes()->getAllLinks() as $path => $source) { /** @var LinkBinding $binding */ $binding = $this->linkBinding->create($source, $hook_menu->getDestinationRoute($path)); // Skip if the converter wasn't able to find a destination. $destination = $binding->getDestination(); if (empty($destination)) { continue; } if ($binding instanceof MenuLinkBinding) { $links['menu']->addBinding($binding); } elseif ($binding instanceof LocalTaskLinkBinding) { $links['task']->addBinding($binding); } elseif ($binding instanceof LocalActionLinkBinding) { $links['action']->addBinding($binding); } elseif ($source->isContextualLink()) { $links['contextual']->addBinding($binding); } } $links = array_map(function (LinkIndex $index) { return $index->build(); }, $links); foreach ($links['contextual'] as $link) { $link['group'] = $target->id(); } foreach ($links as $group => $data) { if ($data) { $this->writeInfo($target, 'links.' . $group, $data); } } }
/** * {@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())); } }
/** * {@inheritdoc} */ public function convert(TargetInterface $target) { $hook = $target->getIndexer('function')->get($this->pluginDefinition['hook'])->before(DocCommentNode::create($this->pluginDefinition['fixme'])); $target->save($hook); }
/** * {@inheritdoc} */ public function convert(TargetInterface $target) { $hook = $target->getIndexer('function')->get($this->pluginDefinition['hook'])->setName($target->id() . '_entity_bundle_create'); $target->save($hook); }
/** * Slaps a translated FIXME notice above a block-related hook. * * @param TargetInterface $target * The target module. * @param string $hook * The hook to put the FIXME on. It's up to the calling code to ensure * that the hook actually exists. */ private function addFixMe(TargetInterface $target, $hook) { $variables = ['!hook' => $hook]; $function = $target->getIndexer('function')->get('hook_' . $hook)->setDocComment($this->buildFixMe(NULL, $variables, self::DOC_COMMENT)); $target->save($function); }
/** * Parametrically rewrites a function. * * @param \Drupal\drupalmoduleupgrader\RewriterInterface $rewriter * A fully configured parametric rewriter. * @param \Pharborist\Functions\ParameterNode $parameter * The parameter upon which to base the rewrite. * @param TargetInterface $target * The target module. * @param boolean $recursive * If TRUE, rewriting will recurse into called functions which are passed * the rewritten parameter as an argument. */ protected function rewriteFunction(RewriterInterface $rewriter, ParameterNode $parameter, TargetInterface $target, $recursive = TRUE) { $rewriter->rewrite($parameter); $target->save($parameter); // Find function calls within the rewritten function which are called // with the rewritten parameter. $indexer = $target->getIndexer('function'); $next = $parameter->getFunction()->find(new FunctionCallArgumentFilter($parameter->getName()))->filter(function (FunctionCallNode $call) use($indexer) { return $indexer->has($call->getName()->getText()); }); /** @var \Pharborist\Functions\FunctionCallNode $call */ foreach ($next as $call) { /** @var \Pharborist\Functions\FunctionDeclarationNode $function */ $function = $indexer->get($call->getName()->getText()); foreach ($call->getArguments() as $index => $argument) { if ($argument instanceof VariableNode && $argument->getName() == $parameter->getName()) { $this->rewriteFunction($rewriter, $function->getParameterAtIndex($index), $target, $recursive); break; } } } }