X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fdrupalmoduleupgrader%2Fsrc%2FPlugin%2FDMU%2FConverter%2FFunctions%2FURL.php;fp=web%2Fmodules%2Fcontrib%2Fdrupalmoduleupgrader%2Fsrc%2FPlugin%2FDMU%2FConverter%2FFunctions%2FURL.php;h=b1966884c185e7525e372894e5dce513d7cc1c1e;hp=0000000000000000000000000000000000000000;hb=8acec36f19c470dfcda1ae2336826a782f41874c;hpb=e0411c4e83ba0d079034db83c3f7f55be24a0e35 diff --git a/web/modules/contrib/drupalmoduleupgrader/src/Plugin/DMU/Converter/Functions/URL.php b/web/modules/contrib/drupalmoduleupgrader/src/Plugin/DMU/Converter/Functions/URL.php new file mode 100644 index 000000000..b1966884c --- /dev/null +++ b/web/modules/contrib/drupalmoduleupgrader/src/Plugin/DMU/Converter/Functions/URL.php @@ -0,0 +1,79 @@ +routeProvider = $route_provider; + } + + /** + * Looks up routes by path, and returns TRUE if at least one was found. + * + * @param string $path + * The path to search for, not including the leading slash. Can be an + * external URL. + * + * @return boolean + * TRUE if the path matches a route, FALSE otherwise. External URLs will + * always return FALSE. + */ + protected function routeExists($path) { + // If there's a scheme in the URL, consider this an external URL and don't even + // try to rewrite it. + $scheme = parse_url($path, PHP_URL_SCHEME); + if (isset($scheme)) { + return FALSE; + } + else { + $routes = $this->routeProvider->getRoutesByPattern('/' . $path); + return (sizeof($routes) > 0); + } + } + + /** + * {@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)); + } + } + } + +}