You are here

protected function InstallableLibrary::request in Markdown 8.2

Requests a URL.

Parameters

string $url: The URL being requested.

Return value

\Drupal\Core\Cache\CacheableResponse A cacheable response.

2 calls to InstallableLibrary::request()
InstallableLibrary::requestJson in src/Annotation/InstallableLibrary.php
Retrieves JSON from a URL.
InstallableLibrary::requestXml in src/Annotation/InstallableLibrary.php
Retrieves XML from a URL.

File

src/Annotation/InstallableLibrary.php, line 444

Class

InstallableLibrary

Namespace

Drupal\markdown\Annotation

Code

protected function request($url) {
  $this->requestException = NULL;

  // Clean the URL.
  $extension = ltrim(pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION), '.');
  $cleanUrl = Html::cleanCssIdentifier(preg_replace('/' . preg_quote($extension, '/') . '$/', '', $url)) . ($extension ? ".{$extension}" : '');
  $cid = 'installable_library:' . $this->id . ':' . $cleanUrl;
  $cache = \Drupal::cache('markdown');

  // If there is a valid 24hr cached response in the database, use it.
  if (($cached = $cache
    ->get($cid)) && isset($cached->data)) {
    return $cached->data;
  }

  // Prepare the request.
  $content = NULL;
  $options = [];
  $directory = 'public://installable_plugins/library';
  file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);

  // If there's a cached file of the request, attempt to use it if its
  // modified time is still valid and acknowledged by the responding server.
  if (file_exists("{$directory}/{$cleanUrl}")) {
    $content = file_get_contents("{$directory}/{$cleanUrl}");
    $options['headers']['If-Modified-Since'] = date('r', filemtime("{$directory}/{$cleanUrl}"));
  }

  // Make the request.
  try {
    $response = static::httpClient()
      ->get($url, $options);
    $statusCode = $response
      ->getStatusCode();

    // New content.
    if ($statusCode >= 200 && $statusCode < 300) {
      $content = $response
        ->getBody()
        ->getContents();
      file_put_contents("{$directory}/{$cleanUrl}", $content);
    }
    elseif ($statusCode >= 400) {
      $request = new Request('GET', $url, isset($options['headers']) ? $options['headers'] : []);
      throw new RequestException($response
        ->getBody()
        ->getContents(), $request, $response);
    }

    // Create a cacheable response.
    $cacheableResponse = CacheableResponse::create($content, $statusCode, $response
      ->getHeaders());

    // Cache response in the database. The TTL value defaults to one day,
    // but allow it to be overrideable via settings.
    $ttl = Settings::get('installable_library_request_ttl', 86400);
    $cache
      ->set($cid, $cacheableResponse, REQUEST_TIME + $ttl);
  } catch (GuzzleException $exception) {
    \Drupal::logger('markdown')
      ->warning('%type: @message in %function (line %line of %file).<pre><code>@backtrace_string</code></pre>', Error::decodeException($exception));
    $this->requestException = $exception;
    $cacheableResponse = CacheableResponse::create($exception
      ->getMessage(), 500);
  }
  return $cacheableResponse;
}