/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { if ($this->tryRewrite($call, $target)) { $arguments = $call->getArguments(); return ClassMethodCallNode::create('\\Drupal', 'configFactory')->appendMethodCall('getEditable')->appendArgument($target->id() . '.settings')->appendMethodCall('set')->appendArgument(clone $arguments[0])->appendArgument(clone $arguments[1])->appendMethodCall('save'); } }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $arguments = $call->getArguments(); $get = ClassMethodCallNode::create('\\Drupal', 'cache'); if (sizeof($arguments) == 2) { $get->appendArgument(clone $arguments[1]); } return $get->appendMethodCall('get')->appendArgument(clone $arguments[0]); }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $arguments = $call->getArguments()->toArray(); $invoke = ClassMethodCallNode::create('\\Drupal', 'moduleHandler')->appendMethodCall('invoke')->appendArgument(array_shift($arguments)->remove())->appendArgument(array_shift($arguments)->remove()); if ($arguments) { $invoke->appendArgument(ArrayNode::create($arguments)); } return $invoke; }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $arguments = $call->getArguments()->toArray(); array_shift($arguments); if (sizeof($arguments) == 3) { array_pop($arguments); } return ClassMethodCallNode::create('\\Drupal', 'service')->appendArgument('user.tempstore')->appendMethodCall('set')->appendArgument($arguments[0])->appendArgument($arguments[1]); }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $arguments = $call->getArguments(); if ($arguments[0] instanceof StringNode) { $handler_type = $arguments[0]->toValue(); if ($handler_type == 'validate' || $handler_type == 'submit') { return ClassMethodCallNode::create('\\Drupal', 'formBuilder')->appendMethodCall('execute' . ucFirst($handler_type) . 'Handlers')->appendArgument(clone $arguments[1])->appendArgument(clone $arguments[2]); } } }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $arguments = $call->getArguments(); if (isset($arguments[1]) && $arguments[1] instanceof VariableNode) { $rewritten = ObjectMethodCallNode::create(clone $arguments[1], 'hasPermission'); } else { $rewritten = ClassMethodCallNode::create('\\Drupal', 'currentUser')->appendMethodCall('hasPermission'); } return $rewritten->appendArgument(clone $arguments[0]); }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $manager = ClassMethodCallNode::create('\\Drupal', 'entityManager'); $arguments = $call->getArguments(); if ($arguments->isEmpty()) { return $manager->appendMethodCall('getDefinitions'); } elseif (sizeof($arguments) == 1) { return $manager->appendMethodCall('getDefinition')->appendArgument(clone $arguments[0]); } }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $replacement = ClassMethodCallNode::create('\\Drupal', 'service')->appendArgument('plugin.manager.field.formatter'); $arguments = $call->getArguments(); if ($arguments->isEmpty()) { return $replacement->appendMethodCall('getDefinitions'); } elseif (sizeof($arguments) == 1) { return $replacement->appendMethodCall('getDefinition')->appendArgument(clone $arguments[0]); } }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $rewritten = ClassMethodCallNode::create('\\Drupal', 'database'); $arguments = $call->getArguments(); if (sizeof($arguments) == 3) { $key = $arguments[2] instanceof StringNode ? ArrayNode::create([clone $arguments[2]]) : clone $arguments[2]; return $rewritten->appendMethodCall('merge')->appendArgument(clone $arguments[0])->appendMethodCall('fields')->appendArgument(clone $arguments[1])->appendMethodCall('key')->appendArgument($key)->appendMethodCall('execute'); } else { return $rewritten->appendMethodCall('insert')->appendArgument(clone $arguments[0])->appendMethodCall('fields')->appendArgument(clone $arguments[1])->appendMethodCall('execute'); } }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $arguments = $call->getArguments(); // If there were three arguments, the call is affecting the internal // comment_load() cache. Unfortunately, it's pretty much impossible to // reliably determine whether or not they wanted to reset the cache, // so let's just leave a FIXME. if (sizeof($arguments) == 2) { $this->buildFixMe('To reset the comment cache, use EntityStorageInterface::resetCache().')->insertBefore($call); } return ClassMethodCallNode::create('\\Drupal', 'entityManager')->appendMethodCall('getStorage')->appendArgument('comment')->appendMethodCall('load')->appendArgument(clone $arguments[0]); }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $arguments = $call->getArguments(); if ($arguments[1] instanceof StringNode) { // Create a call to url() and let the parent class rewrite it like normal, // so we don't have to duplicate that code. $url = Parser::parseSnippet('url(' . $arguments[1] . ');')->firstChild(); $url_rewritten = parent::rewrite($url, $target); if ($url_rewritten) { return ClassMethodCallNode::create('\\Drupal', 'l')->appendArgument($arguments[0])->appendArgument($url_rewritten); } } }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $arguments = $call->getArguments(); $cache = ClassMethodCallNode::create('\\Drupal', 'cache'); if (sizeof($arguments) > 2) { $cache->appendArgument(clone $arguments[2]); } $set = $cache->appendMethodCall('set')->appendArgument(clone $arguments[0])->appendArgument(clone $arguments[1]); // Include the expiration time, if given. if (sizeof($arguments) == 4) { $set->appendArgument(clone $arguments[3]); } return $set; }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $arguments = $call->getArguments(); if ($arguments[0] instanceof StringNode) { $path = $arguments[0]->toValue(); // If the URL has a scheme (e.g., http://), it's external. if (parse_url($path, PHP_URL_SCHEME)) { return ClassMethodCallNode::create('\\Drupal\\Core\\Url', 'fromUri')->appendArgument(clone $arguments[0]); } elseif ($this->routeExists($path)) { $route = $this->routeProvider->getRoutesByPattern('/' . $path)->getIterator()->key(); return ClassMethodCallNode::create('\\Drupal\\Core\\Url', 'fromRoute')->appendArgument(StringNode::fromValue($route)); } } }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { if (!$this->canRewrite($call, $target)) { return NULL; } $arguments = $call->getArguments(); $plugin_owner = $arguments[0]->toValue(); $plugin_type = $arguments[1]->toValue(); $services = $target->getServices(); $service_id = 'plugin.manager.' . $plugin_owner . '.' . $plugin_type; $services->set($service_id, ['class' => 'Drupal\\Core\\Plugin\\DefaultPluginManager', 'arguments' => ['Plugin/' . $plugin_owner . '/' . $plugin_type, '@container.namespaces', '@module_handler', 'Drupal\\Component\\Plugin\\PluginBase', 'Drupal\\Component\\Annotation\\Plugin']]); $this->writeInfo($target, 'services', ['services' => $services->toArray()]); return ClassMethodCallNode::create('\\Drupal', 'service')->appendArgument($service_id)->appendMethodCall('getDefinitions'); }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $arguments = $call->getArguments(); // If there were three arguments, the call is affecting the internal // node_load() cache. Unfortunately, it's pretty much impossible to // reliably determine whether or not they wanted to reset the cache, // so let's just leave a FIXME. if (sizeof($arguments) == 3) { $this->buildFixMe('To reset the node cache, use EntityStorageInterface::resetCache().')->insertBefore($call); } $rewritten = ClassMethodCallNode::create('\\Drupal', 'entityManager')->appendMethodCall('getStorage')->appendArgument('node'); // If there's more than one argument, a revision ID was passed, which // means we call loadRevision($nid). Otherwise, call load($nid). if (sizeof($arguments) > 1) { return $rewritten->appendMethodCall('loadRevision')->appendArgument(clone $arguments[1]); } else { return $rewritten->appendMethodCall('load')->appendArgument(clone $arguments[0]); } }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $arguments = $call->getArguments(); // If there were three arguments, the call is affecting the internal // entity cache. Unfortunately, it's pretty much impossible to reliably // determine whether or not they wanted to reset the cache, so let's just // leave a FIXME. if (sizeof($arguments) == 3) { $variables = ['!entity_type' => $this->pluginDefinition['entity_type']]; $this->buildFixMe('To reset the !entity_type cache, use EntityStorageInterface::resetCache().', $variables)->insertBefore($call); } $rewritten = ClassMethodCallNode::create('\\Drupal', 'entityManager')->appendMethodCall('getStorage')->appendArgument($this->pluginDefinition['entity_type']); // If there's more than one argument, conditions were passed (not a // recommended practice, but modules might have done it anyway), in which // case we need to use loadByProperties(). Otherwise, loadMultiple(). if (sizeof($arguments) > 1) { return $rewritten->appendMethodCall('loadByProperties')->appendArgument(clone $arguments[1]); } else { return $rewritten->appendMethodCall('loadMultiple')->appendArgument(clone $arguments[0]); } }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $arguments = $call->getArguments(); // We'll call a specific method on the logger object, depending on the // severity passed in the original function call (if any). If there are // at least four arguments, a severity was passed. We check $arguments[3] // to ensure it's a valid severity constant, and if it's not, we default // to the notice() severity. // // @TODO Leave a FIXME for an invalid severity, since changing it to a // notice alters the intent of the original code. // if (sizeof($arguments) > 3 && $arguments[3] instanceof ConstantNode && in_array($arguments[3]->getConstantName()->getText(), static::$severityConstants)) { $method = strtolower(subStr($arguments[3], 9)); } else { $method = 'notice'; } // If there is a third argument, and it's an array, a context array // was passed. $context = sizeof($arguments) > 2 && $arguments[2] instanceof ArrayNode ? clone $arguments[2] : ArrayNode::create([]); return ClassMethodCallNode::create('\\Drupal', 'logger')->appendArgument(clone $arguments[0])->appendMethodCall($method)->appendArgument(clone $arguments[1])->appendArgument($context); }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { if ($this->tryRewrite($call, $target)) { $arguments = $call->getArguments(); $key = $arguments[0]->toValue(); if ($arguments[1] instanceof ScalarNode) { // @TODO Couldn't convert() derive the schema from $this->defaults? // That'd be preferable to having yet another state property ($schema) // on this class. $this->defaults[$key] = $arguments[1]->toValue(); $this->schema[$key]['type'] = getType($this->defaults[$key]); } else { $comment = <<<END Could not extract the default value because it is either indeterminate, or not scalar. You'll need to provide a default value in config/install/@module.settings.yml and config/schema/@module.schema.yml. END; $variables = ['@module' => $target->id()]; $this->buildFixMe($comment, $variables)->prependTo($call->getStatement()); } return ClassMethodCallNode::create('\\Drupal', 'config')->appendArgument($target->id() . '.settings')->appendMethodCall('get')->appendArgument(clone $arguments[0]); } }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $arguments = $call->getArguments(); // If there were three arguments, the call is affecting the internal // entity cache. Unfortunately, it's pretty much impossible to reliably // determine whether or not they wanted to reset the cache, so let's just // leave a FIXME. if (sizeof($arguments) == 3) { $this->buildFixMe('To reset the entity cache, use EntityStorageInterface::resetCache().')->insertBefore($call); } $rewritten = ClassMethodCallNode::create('\\Drupal', 'entityManager')->appendMethodCall('getStorage')->appendArgument(clone $arguments[0]); // If there's a third argument, conditions were passed. Not a recommended // practice, but certain modules might have done it anyway. If we detect // conditions, use loadByProperties(). if (sizeof($arguments) > 2) { return $rewritten->appendMethodCall('loadByProperties')->appendArgument(clone $arguments[2]); } else { $rewritten->appendMethodCall('load'); if (sizeof($arguments) > 1 && $arguments[1] instanceof ArrayNode) { $rewritten->appendArgument(clone $arguments[1]); } return $rewritten; } }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { return ClassMethodCallNode::create('\\Drupal', 'service')->appendArgument('user.tempstore')->appendMethodCall('get')->appendArgument(clone $call->getArgumentList()->getItem(1)); }
/** * {@inheritdoc} */ public function buildRoute(TargetInterface $target, Drupal7Route $route) { $definition = $this->buildRouteDefinition($target, $route); $map = $this->buildParameterMap($target, $route); $map->applyRoute($definition->unwrap()); $indexer = $target->getIndexer('function'); foreach ($map->toArray() as $function_name => $parameters) { if ($parameters && $indexer->has($function_name)) { /** @var \Pharborist\Functions\FunctionDeclarationNode $function */ $function = $indexer->get($function_name); foreach ($parameters as $parameter_name => $info) { $parameter = $function->getParameterByName($parameter_name)->setName($info['name'], TRUE); if (isset($info['type'])) { $plugin_id = '_rewriter:' . $info['type']; if ($this->rewriters->hasDefinition($plugin_id)) { $this->rewriters->createInstance($plugin_id)->rewrite($parameter); } } } } } $class_indexer = $target->getIndexer('class'); if ($class_indexer->has('DefaultController')) { $controller = $class_indexer->get('DefaultController'); } else { $controller = $this->getController($target, $route); $class_indexer->addFile($this->writeClass($target, $controller)); } if ($indexer->has($route['title callback'])) { if (!$controller->hasMethod($route['title callback'])) { $indexer->get($route['title callback'])->cloneAsMethodOf($controller); } } if ($indexer->has($route['access callback'])) { $func = $indexer->get($route['access callback']); $returns = $func->find(Filter::isInstanceOf('\\Pharborist\\ReturnStatementNode')); foreach ($returns as $ret) { $call = ClassMethodCallNode::create('\\Drupal\\Core\\Access\\AccessResult', 'allowedIf')->appendArgument($ret->getExpression()); $ret->replaceWith(ReturnStatementNode::create($call)); } // The access callback always receives an $account parameter. if ($func->hasParameter('account')) { $func->getParameter('account')->setTypeHint('Drupal\\Core\\Session\\AccountInterface'); } else { $account = ParameterNode::create('account')->setTypeHint('Drupal\\Core\\Session\\AccountInterface'); $func->appendParameter($account); } if (!$controller->hasMethod($route['access callback'])) { $func->cloneAsMethodOf($controller); } } if ($indexer->has($route['page callback'])) { if (!$controller->hasMethod($route['page callback'])) { $indexer->get($route['page callback'])->cloneAsMethodOf($controller); } } $this->writeClass($target, $controller); }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { return ClassMethodCallNode::create('\\Drupal', 'service')->appendArgument('title_resolver')->appendMethodCall('getTitle')->appendArgument(ClassMethodCallNode::create('\\Drupal', 'request'))->appendArgument(ClassMethodCallNode::create('\\Drupal', 'routeMatch')->appendMethodCall('getRouteObject')); }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $arguments = $call->getArguments()->toArray(); return ClassMethodCallNode::create('\\Drupal', 'service')->appendArgument(StringNode::fromValue('theme.registry'))->appendMethodCall($arguments && $arguments[0] instanceof FalseNode ? 'getRuntime' : 'get'); }
public function testClassMethodCall() { $method_call = ClassMethodCallNode::create('TestClass', 'someMethod'); $this->assertInstanceOf('\\Pharborist\\Objects\\ClassMethodCallNode', $method_call); $this->assertEquals('TestClass', $method_call->getClassName()->getText()); $this->assertEquals('someMethod', $method_call->getMethodName()->getText()); $this->assertCount(0, $method_call->getArguments()); $var = Parser::parseExpression('$a'); $method_call->appendArgument($var); $arg = $method_call->getArguments()[0]; $this->assertInstanceOf('\\Pharborist\\Variables\\VariableNode', $arg); $this->assertEquals('$a', $arg->getText()); /** @var ParentNode $expression */ $expression = Parser::parseExpression($method_call->getText()); $expected = $expression->getTree(); $actual = $method_call->getTree(); $this->assertEquals($expected, $actual); }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $arguments = $call->getArguments(); return ClassMethodCallNode::create('\\Drupal', 'entityManager')->appendMethodCall('getStorage')->appendArgument(clone $arguments[0])->appendMethodCall('create')->appendArgument(clone $arguments[1]); }