View source
<?php
namespace Drupal\Core\Utility;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\GeneratedUrl;
use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class UnroutedUrlAssembler implements UnroutedUrlAssemblerInterface {
protected $requestStack;
protected $pathProcessor;
public function __construct(RequestStack $request_stack, OutboundPathProcessorInterface $path_processor, array $filter_protocols = [
'http',
'https',
]) {
UrlHelper::setAllowedProtocols($filter_protocols);
$this->requestStack = $request_stack;
$this->pathProcessor = $path_processor;
}
public function assemble($uri, array $options = [], $collect_bubbleable_metadata = FALSE) {
if (parse_url($uri, PHP_URL_SCHEME) === 'base') {
return $this
->buildLocalUrl($uri, $options, $collect_bubbleable_metadata);
}
elseif (UrlHelper::isExternal($uri)) {
return $this
->buildExternalUrl($uri, $options, $collect_bubbleable_metadata);
}
throw new \InvalidArgumentException("The URI '{$uri}' is invalid. You must use a valid URI scheme. Use base: for a path, e.g., to a Drupal file that needs the base path. Do not use this for internal paths controlled by Drupal.");
}
protected function buildExternalUrl($uri, array $options = [], $collect_bubbleable_metadata = FALSE) {
$this
->addOptionDefaults($options);
$parsed = UrlHelper::parse($uri);
$uri = $parsed['path'];
$parsed += [
'query' => [],
];
$options += [
'query' => [],
];
$options['query'] = NestedArray::mergeDeepArray([
$parsed['query'],
$options['query'],
], TRUE);
if ($parsed['fragment'] && !$options['fragment']) {
$options['fragment'] = '#' . $parsed['fragment'];
}
if (isset($options['https'])) {
if ($options['https'] === TRUE) {
$uri = str_replace('http://', 'https://', $uri);
}
elseif ($options['https'] === FALSE) {
$uri = str_replace('https://', 'http://', $uri);
}
}
if ($options['query']) {
$uri .= '?' . UrlHelper::buildQuery($options['query']);
}
$url = $uri . $options['fragment'];
return $collect_bubbleable_metadata ? (new GeneratedUrl())
->setGeneratedUrl($url) : $url;
}
protected function buildLocalUrl($uri, array $options = [], $collect_bubbleable_metadata = FALSE) {
$generated_url = $collect_bubbleable_metadata ? new GeneratedUrl() : NULL;
$this
->addOptionDefaults($options);
$request = $this->requestStack
->getCurrentRequest();
$uri = substr($uri, 5);
if (!empty($options['path_processing'])) {
$uri = $this->pathProcessor
->processOutbound($uri, $options, NULL, $generated_url);
}
$uri = ltrim($uri, '/');
$current_base_path = $request
->getBasePath() . '/';
if ($options['absolute']) {
$current_base_url = $request
->getSchemeAndHttpHost() . $current_base_path;
if (isset($options['https'])) {
if (!empty($options['https'])) {
$base = str_replace('http://', 'https://', $current_base_url);
$options['absolute'] = TRUE;
}
else {
$base = str_replace('https://', 'http://', $current_base_url);
$options['absolute'] = TRUE;
}
}
else {
$base = $current_base_url;
}
if ($collect_bubbleable_metadata) {
$generated_url
->addCacheContexts([
'url.site',
]);
}
}
else {
$base = $current_base_path;
}
$prefix = empty($uri) ? rtrim($options['prefix'], '/') : $options['prefix'];
$uri = str_replace('%2F', '/', rawurlencode($prefix . $uri));
$query = $options['query'] ? '?' . UrlHelper::buildQuery($options['query']) : '';
$url = $base . $options['script'] . $uri . $query . $options['fragment'];
return $collect_bubbleable_metadata ? $generated_url
->setGeneratedUrl($url) : $url;
}
protected function addOptionDefaults(array &$options) {
$request = $this->requestStack
->getCurrentRequest();
$current_base_path = $request
->getBasePath() . '/';
$current_script_path = '';
$base_path_with_script = $request
->getBaseUrl();
if (!empty($base_path_with_script)) {
$script_name = $request
->getScriptName();
if (strpos($base_path_with_script, $script_name) !== FALSE) {
$current_script_path = ltrim(substr($script_name, strlen($current_base_path)), '/') . '/';
}
}
$options += [
'fragment' => '',
'query' => [],
'absolute' => FALSE,
'prefix' => '',
'script' => $current_script_path,
];
if (isset($options['fragment']) && $options['fragment'] !== '') {
$options['fragment'] = '#' . $options['fragment'];
}
}
}