Commit e69cd767 authored by webchick's avatar webchick
Browse files

Issue #2416763 by xjm, Wim Leers, effulgentsia: Convert Url::fromUri() base:// scheme to base:

parent 089a68f4
......@@ -499,9 +499,9 @@ function _format_date_callback(array $matches = NULL, $new_langcode = NULL) {
* non-controller or sub-domain URIs such as core/install.php. Note that
* \Drupal\Core\Url::fromUri() expects a valid URI including the scheme. URIs
* from the same sub-domain that are not handled by Drupal controllers should
* be prepended with base://. For example:
* use the base: scheme. For example:
* @code
* $installer_url = \Drupal\Core\Url::fromUri('base://core/install.php')->toString();
* $installer_url = \Drupal\Core\Url::fromUri('base:core/install.php')->toString();
* $external_url = \Drupal\Core\Url::fromUri('http://example.com', ['query' => ['foo' => 'bar']])->toString();
* $internal_url = \Drupal\Core\Url::fromRoute('system.admin')->toString();
* @endcode
......@@ -604,9 +604,9 @@ function drupal_http_header_attributes(array $attributes = array()) {
* core/install.php use \Drupal\Core\Url::fromUri(). Note that
* \Drupal\Core\Url::fromUri() expects a valid URI including the scheme. URIs
* from the same sub-domain that are not handled by Drupal controllers should
* be prepended with base://. For example:
* be prepended with base:. For example:
* @code
* $installer_url = \Drupal\Core\Url::fromUri('base://core/install.php')->toString();
* $installer_url = \Drupal\Core\Url::fromUri('base:core/install.php')->toString();
* $installer_link = \Drupal::l($text, $installer_url);
* $external_url = \Drupal\Core\Url::fromUri('http://example.com', ['query' => ['foo' => 'bar']])->toString();
* $external_link = \Drupal::l($text, $external_url);
......
......@@ -584,7 +584,7 @@ function install_run_task($task, &$install_state) {
// Otherwise, the batch will complete.
// Disable the default script for the URL and clone the object, as
// batch_process() will add additional options to the batch URL.
$url = Url::fromUri('base://install.php', ['query' => $install_state['parameters'], 'script' => '']);
$url = Url::fromUri('base:install.php', ['query' => $install_state['parameters'], 'script' => '']);
$response = batch_process($url, clone $url);
if ($response instanceof Response) {
// Save $_SESSION data from batch.
......
......@@ -476,7 +476,7 @@ public static function urlGenerator() {
* the \Drupal\Core\Url object. See \Drupal\Core\Url::fromRoute() for
* detailed documentation. For non-routed local URIs relative to
* the base path (like robots.txt) use Url::fromUri()->toString() with the
* base:// scheme.
* base: scheme.
*
* @param string $route_name
* The name of the route.
......
......@@ -36,7 +36,7 @@ public static function buildCancelLink(ConfirmFormInterface $form, Request $requ
$options = UrlHelper::parse($query->get('destination'));
// @todo Use Url::fromPath() once https://www.drupal.org/node/2351379 is
// resolved.
$url = Url::fromUri('base://' . $options['path'], $options);
$url = Url::fromUri('base:' . $options['path'], $options);
}
// Check for a route-based cancel link.
else {
......
......@@ -139,7 +139,7 @@ public function __construct($route_name, $route_parameters = array(), $options =
*
* This method is for URLs that have Drupal routes (that is, most pages
* generated by Drupal). For non-routed local URIs relative to the base
* path (like robots.txt) use Url::fromUri() with the base:// scheme.
* path (like robots.txt) use Url::fromUri() with the base: scheme.
*
* @param string $route_name
* The name of the route
......@@ -193,7 +193,7 @@ public static function fromRouteMatch(RouteMatchInterface $route_match) {
*
* This method is for generating URLs for URIs that:
* - do not have Drupal routes: both external URLs and unrouted local URIs
* like base://robots.txt
* like base:robots.txt
* - do have a Drupal route but have a custom scheme to simplify linking.
* Currently, there is only the entity: scheme (This allows URIs of the
* form entity:{entity_type}/{entity_id}. For example: entity:node/1
......@@ -204,7 +204,7 @@ public static function fromRouteMatch(RouteMatchInterface $route_match) {
*
* @param string $uri
* The URI of the resource including the scheme. For Drupal paths that are
* not handled by the routing system, use base:// for the scheme. For entity
* not handled by the routing system, use base: for the scheme. For entity
* URLs you may use entity:{entity_type}/{entity_id} URIs.
* @param array $options
* (optional) An associative array of additional URL options, with the
......@@ -233,15 +233,18 @@ public static function fromRouteMatch(RouteMatchInterface $route_match) {
*/
public static function fromUri($uri, $options = array()) {
if (!($scheme = parse_url($uri, PHP_URL_SCHEME))) {
throw new \InvalidArgumentException(String::format('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.', ['@uri' => $uri]));
throw new \InvalidArgumentException(String::format('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.', ['@uri' => $uri]));
}
// Special case entity: URIs. Map these to the canonical entity route.
if ($scheme === 'entity') {
return static::fromEntityUri($uri);
}
$url = new static($uri, array(), $options);
if ($scheme !== 'base') {
$url->external = TRUE;
$url->setOption('external', TRUE);
}
$url->setUnrouted();
return $url;
......@@ -313,11 +316,6 @@ protected function setUnrouted() {
// Set empty route name and parameters.
$this->routeName = NULL;
$this->routeParameters = array();
// @todo Add a method for the check below in
// https://www.drupal.org/node/2346859.
if ($this->external = strpos($this->uri, 'base://') !== 0) {
$this->options['external'] = TRUE;
}
}
/**
......
......@@ -61,14 +61,14 @@ public function assemble($uri, array $options = []) {
// Note that UrlHelper::isExternal will return FALSE if the $uri has a
// disallowed protocol. This is later made safe since we always add at
// least a leading slash.
if (strpos($uri, 'base://') === 0) {
if (parse_url($uri, PHP_URL_SCHEME) === 'base') {
return $this->buildLocalUrl($uri, $options);
}
elseif (UrlHelper::isExternal($uri)) {
// UrlHelper::isExternal() only returns true for safe protocols.
return $this->buildExternalUrl($uri, $options);
}
throw new \InvalidArgumentException(String::format('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.', ['@uri' => $uri]));
throw new \InvalidArgumentException(String::format('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.', ['@uri' => $uri]));
}
/**
......@@ -108,8 +108,10 @@ protected function buildLocalUrl($uri, array $options = []) {
$this->addOptionDefaults($options);
$request = $this->requestStack->getCurrentRequest();
// Remove the base:// scheme.
$uri = substr($uri, 7);
// Remove the base: scheme.
// @todo Consider using a class constant for this in
// https://www.drupal.org/node/2417459
$uri = substr($uri, 5);
// Allow (outbound) path processing, if needed. A valid use case is the path
// alias overview form:
......
......@@ -18,11 +18,11 @@ interface UnroutedUrlAssemblerInterface {
* domain-local URIs and external URLs.
*
* @param string $uri
* A local URI or an external URL being linked to, such as "base://foo"
* A local URI or an external URL being linked to, such as "base:foo"
* or "http://example.com/foo".
* - If you provide a full URL, it will be considered an external URL as
* long as it has an allowed protocol.
* - If you provide only a local URI (e.g. "base://foo"), it will be
* - If you provide only a local URI (e.g. "base:foo"), it will be
* considered a path local to Drupal, but not handled by the routing
* system. The base path (the subdirectory where the front controller
* is found) will be added to the path. Additional query arguments for
......
......@@ -255,11 +255,11 @@ public function eventDetails($event_id) {
),
array(
array('data' => $this->t('Location'), 'header' => TRUE),
$this->l($dblog->location, $dblog->location ? Url::fromUri('base://' . $dblog->location) : Url::fromRoute('<none>')),
$this->l($dblog->location, $dblog->location ? Url::fromUri('base:' . $dblog->location) : Url::fromRoute('<none>')),
),
array(
array('data' => $this->t('Referrer'), 'header' => TRUE),
$this->l($dblog->referer, $dblog->referer ? Url::fromUri('base://' . $dblog->referer) : Url::fromRoute('<none>')),
$this->l($dblog->referer, $dblog->referer ? Url::fromUri('base:' . $dblog->referer) : Url::fromRoute('<none>')),
),
array(
array('data' => $this->t('Message'), 'header' => TRUE),
......
......@@ -63,7 +63,7 @@ public static function getNextDestination(array $destinations) {
// Redirect to any given path within the same domain.
// @todo Use Url::fromPath() once https://www.drupal.org/node/2351379 is
// resolved.
$next_destination = Url::fromUri('base://' . $options['path']);
$next_destination = Url::fromUri('base:' . $options['path']);
}
return $next_destination;
}
......
......@@ -25,7 +25,7 @@ public function testTypeHandling() {
$data_with_valid_type = array(
'_links' => array(
'type' => array(
'href' => Url::fromUri('base://rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
),
),
);
......@@ -37,10 +37,10 @@ public function testTypeHandling() {
'_links' => array(
'type' => array(
array(
'href' => Url::fromUri('base://rest/types/foo', array('absolute' => TRUE))->toString(),
'href' => Url::fromUri('base:rest/types/foo', array('absolute' => TRUE))->toString(),
),
array(
'href' => Url::fromUri('base://rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
),
),
),
......@@ -52,7 +52,7 @@ public function testTypeHandling() {
$data_with_invalid_type = array(
'_links' => array(
'type' => array(
'href' => Url::fromUri('base://rest/types/foo', array('absolute' => TRUE))->toString(),
'href' => Url::fromUri('base:rest/types/foo', array('absolute' => TRUE))->toString(),
),
),
);
......@@ -85,7 +85,7 @@ public function testMarkFieldForDeletion() {
$no_field_data = array(
'_links' => array(
'type' => array(
'href' => Url::fromUri('base://rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
),
),
);
......@@ -95,7 +95,7 @@ public function testMarkFieldForDeletion() {
$empty_field_data = array(
'_links' => array(
'type' => array(
'href' => Url::fromUri('base://rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
),
),
'field_test_text' => array(),
......@@ -113,7 +113,7 @@ public function testBasicFieldDenormalization() {
$data = array(
'_links' => array(
'type' => array(
'href' => Url::fromUri('base://rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
),
),
'uuid' => array(
......@@ -183,7 +183,7 @@ public function testPatchDenormailzation() {
$data = array(
'_links' => array(
'type' => array(
'href' => Url::fromUri('base://rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
),
),
'field_test_text' => array(
......
......@@ -61,8 +61,8 @@ public function testNormalize() {
$entity->getTranslation('en')->set('field_test_entity_reference', array(0 => $translation_values['field_test_entity_reference']));
$entity->save();
$type_uri = Url::fromUri('base://rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString();
$relation_uri = Url::fromUri('base://rest/relation/entity_test/entity_test/field_test_entity_reference', array('absolute' => TRUE))->toString();
$type_uri = Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString();
$relation_uri = Url::fromUri('base:rest/relation/entity_test/entity_test/field_test_entity_reference', array('absolute' => TRUE))->toString();
$expected_array = array(
'_links' => array(
......
......@@ -225,7 +225,7 @@ public function buildUrl($path, $clean_urls = NULL) {
// built.
if ($clean_urls === FALSE && file_uri_scheme($uri) == 'public' && !file_exists($uri)) {
$directory_path = file_stream_wrapper_get_instance_by_uri($uri)->getDirectoryPath();
return Url::fromUri('base://' . $directory_path . '/' . file_uri_target($uri), array('absolute' => TRUE, 'query' => $token_query))->toString();
return Url::fromUri('base:' . $directory_path . '/' . file_uri_target($uri), array('absolute' => TRUE, 'query' => $token_query))->toString();
}
$file_url = file_create_url($uri);
......
......@@ -87,7 +87,7 @@ function testImageFormatterTheme() {
'#theme' => 'image_formatter',
'#image_style' => 'test',
'#item' => $entity->image_test,
'#url' => Url::fromUri('base://' . $path),
'#url' => Url::fromUri('base:' . $path),
);
// Test using theme_image_formatter() with a NULL value for the alt option.
......
......@@ -116,7 +116,7 @@ function testMenu() {
// Verify delete link exists and reset link does not exist.
$this->drupalGet('admin/structure/menu/manage/' . $this->menu->id());
$this->assertLinkByHref('admin/structure/menu/item/' . $this->items[0]->id() . '/delete');
$this->assertNoLinkByHref(Url::fromUri('base://admin/structure/menu/link/' . $this->items[0]->getPluginId() . '/reset')->toString());
$this->assertNoLinkByHref(Url::fromUri('base:admin/structure/menu/link/' . $this->items[0]->getPluginId() . '/reset')->toString());
// Check delete and reset access.
$this->drupalGet('admin/structure/menu/item/' . $this->items[0]->id() . '/delete');
$this->assertResponse(200);
......
......@@ -86,11 +86,11 @@ public function adminOverview(Request $request) {
$destination = drupal_get_destination();
foreach ($this->aliasStorage->getAliasesForAdminListing($header, $keys) as $data) {
$row = array();
$row['data']['alias'] = $this->l(Unicode::truncate($data->alias, 50, FALSE, TRUE), Url::fromUri('base://' . $data->source, array(
$row['data']['alias'] = $this->l(Unicode::truncate($data->alias, 50, FALSE, TRUE), Url::fromUri('base:' . $data->source, array(
'path_processing' => TRUE,
'attributes' => array('title' => $data->alias),
)));
$row['data']['source'] = $this->l(Unicode::truncate($data->source, 50, FALSE, TRUE), Url::fromUri('base://' . $data->source, array(
$row['data']['source'] = $this->l(Unicode::truncate($data->source, 50, FALSE, TRUE), Url::fromUri('base:' . $data->source, array(
'alias' => TRUE,
'attributes' => array('title' => $data->source),
)));
......
......@@ -54,7 +54,7 @@ public function __construct(CacheBackendInterface $cache, EntityManagerInterface
* {@inheritdoc}
*/
public function getRelationUri($entity_type, $bundle, $field_name) {
return $this->urlAssembler->assemble("base://rest/relation/$entity_type/$bundle/$field_name", array('absolute' => TRUE));
return $this->urlAssembler->assemble("base:rest/relation/$entity_type/$bundle/$field_name", array('absolute' => TRUE));
}
/**
......
......@@ -53,7 +53,7 @@ public function __construct(CacheBackendInterface $cache, UnroutedUrlAssemblerIn
*/
public function getTypeUri($entity_type, $bundle) {
// @todo Make the base path configurable.
return $this->urlAssembler->assemble("base://rest/type/$entity_type/$bundle", array('absolute' => TRUE));
return $this->urlAssembler->assemble("base:rest/type/$entity_type/$bundle", array('absolute' => TRUE));
}
/**
......
......@@ -70,7 +70,7 @@ public function testNodes() {
$data = array(
'_links' => array(
'type' => array(
'href' => Url::fromUri('base://rest/type/node/resttest', array('absolute' => TRUE))->toString(),
'href' => Url::fromUri('base:rest/type/node/resttest', array('absolute' => TRUE))->toString(),
),
),
'title' => array(
......
......@@ -63,12 +63,12 @@ function testUuidEntityResolver() {
$entity->set('field_test_entity_reference', array(array('target_id' => 1)));
$entity->save();
$field_uri = Url::fromUri('base://rest/relation/entity_test_mulrev/entity_test_mulrev/field_test_entity_reference', array('absolute' => TRUE))->toString();
$field_uri = Url::fromUri('base:rest/relation/entity_test_mulrev/entity_test_mulrev/field_test_entity_reference', array('absolute' => TRUE))->toString();
$data = array(
'_links' => array(
'type' => array(
'href' => Url::fromUri('base://rest/type/entity_test_mulrev/entity_test_mulrev', array('absolute' => TRUE))->toString(),
'href' => Url::fromUri('base:rest/type/entity_test_mulrev/entity_test_mulrev', array('absolute' => TRUE))->toString(),
),
$field_uri => array(
array(
......
......@@ -77,7 +77,7 @@ public function testInternalBrowser() {
// @see drupal_valid_test_ua()
// Not using File API; a potential error must trigger a PHP warning.
unlink($this->siteDirectory . '/.htkey');
$this->drupalGet(Url::fromUri('base://core/install.php', array('external' => TRUE, 'absolute' => TRUE))->toString());
$this->drupalGet(Url::fromUri('base:core/install.php', array('external' => TRUE, 'absolute' => TRUE))->toString());
$this->assertResponse(403, 'Cannot access install.php.');
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment