You are here

public static function TomeStaticHelper::setBaseUrl in Tome 8

Sets the base URL for a given request.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request.

string $base_url: The base URL.

Return value

array An array meant to be passed to ::restoreBaseUrl

4 calls to TomeStaticHelper::setBaseUrl()
StaticGeneratorForm::exportPath in modules/tome_static/src/Form/StaticGeneratorForm.php
Exports a path using Tome.
StaticGeneratorForm::submitForm in modules/tome_static/src/Form/StaticGeneratorForm.php
Form submission handler.
TomeStaticQueueWorker::processItem in modules/tome_static/modules/tome_static_cron/src/Plugin/QueueWorker/TomeStaticQueueWorker.php
Works on a single queue item.
tome_static_cron_cron in modules/tome_static/modules/tome_static_cron/tome_static_cron.module
Implements hook_cron().

File

modules/tome_static/src/TomeStaticHelper.php, line 25

Class

TomeStaticHelper
Provides helpers for the Tome Static module.

Namespace

Drupal\tome_static

Code

public static function setBaseUrl(Request $request, $base_url) {
  $original_params = [
    'server' => $request->server
      ->all(),
    'headers' => $request->headers
      ->all(),
  ];
  $server = $request->server
    ->all();

  // Original credit to the Drush team for this logic.
  $base_url = parse_url($base_url);
  $base_url += [
    'scheme' => NULL,
    'path' => '',
    'host' => NULL,
    'port' => NULL,
  ];
  $server['HTTP_HOST'] = $base_url['host'];
  if ($base_url['scheme'] === 'https') {
    $server['HTTPS'] = 'on';
  }
  if ($base_url['port']) {
    $server['HTTP_HOST'] .= ':' . $base_url['port'];
  }
  $server['SERVER_PORT'] = $base_url['port'];
  $server['REMOTE_ADDR'] = '127.0.0.1';
  $request->server
    ->replace($server);
  $request->headers
    ->set('HOST', $base_url['host']);
  return $original_params;
}