URL.php in Drupal 7 to 8/9 Module Upgrader 8
File
src/Plugin/DMU/Converter/Functions/URL.php
View source
<?php
namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter\Functions;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\drupalmoduleupgrader\TargetInterface;
use Pharborist\Functions\FunctionCallNode;
use Pharborist\Objects\ClassMethodCallNode;
use Pharborist\Types\StringNode;
use Psr\Log\LoggerInterface;
class URL extends FunctionCallModifier implements ContainerFactoryPluginInterface {
protected $routeProvider;
public function __construct(array $configuration, $plugin_id, $plugin_definition, TranslationInterface $translator, LoggerInterface $log, RouteProviderInterface $route_provider) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $translator, $log);
$this->routeProvider = $route_provider;
}
protected function routeExists($path) {
$scheme = parse_url($path, PHP_URL_SCHEME);
if (isset($scheme)) {
return FALSE;
}
else {
$routes = $this->routeProvider
->getRoutesByPattern('/' . $path);
return sizeof($routes) > 0;
}
}
public function rewrite(FunctionCallNode $call, TargetInterface $target) {
$arguments = $call
->getArguments();
if ($arguments[0] instanceof StringNode) {
$path = $arguments[0]
->toValue();
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));
}
}
}
}
Classes
Name |
Description |
URL |
Plugin annotation
@Converter(
id = "url",
description = @Translation("Rewrites calls to url()."),
fixme = @Translation("url() expects a route name or an external URI."),
dependencies = { "router.route_provider" }
) |