function LocaleFileManager::checkRemoteFileStatus

Check if remote file exists and when it was last updated.

Parameters

string $uri: URI of remote file.

Return value

\Drupal\locale\File\RemoteFileInfo RemoteFileInfo value object.

File

core/modules/locale/src/File/LocaleFileManager.php, line 122

Class

LocaleFileManager
Provide Locale File helper methods.

Namespace

Drupal\locale\File

Code

public function checkRemoteFileStatus(string $uri) : RemoteFileInfo {
  $logger = $this->loggerFactory
    ->get('locale');
  $remoteFileInfo = new RemoteFileInfo();
  try {
    $actual_uri = NULL;
    $response = $this->clientFactory
      ->fromOptions([
      'allow_redirects' => [
        'on_redirect' => function (RequestInterface $request, ResponseInterface $response, UriInterface $request_uri) use (&$actual_uri) {
          $actual_uri = (string) $request_uri;
        },
      ],
    ])
      ->head($uri);
    // Return the effective URL if it differs from the requested.
    if ($actual_uri && $actual_uri !== $uri) {
      $remoteFileInfo->location = $actual_uri;
    }
    $remoteFileInfo->lastModified = $response->hasHeader('Last-Modified') ? strtotime($response->getHeaderLine('Last-Modified')) : 0;
    $remoteFileInfo->status = RemoteFileStatus::Success;
    return $remoteFileInfo;
  } catch (RequestException $e) {
    // Handle 4xx and 5xx http responses.
    $response = $e->getResponse();
    if ($response) {
      if ($response->getStatusCode() == 404) {
        // File not found occurs when a translation file is not yet available
        // at the translation server. But also if a custom module or custom
        // theme does not define the location of a translation file. By
        // default the file is checked at the translation server, but it will
        // not be found there.
        $logger->notice('Translation file not found: @uri.', [
          '@uri' => $uri,
        ]);
        $remoteFileInfo->status = RemoteFileStatus::Missing;
        return $remoteFileInfo;
      }
      $logger->notice('HTTP request to @url failed with error: @error.', [
        '@url' => $uri,
        '@error' => $response->getStatusCode() . ' ' . $response->getReasonPhrase(),
      ]);
    }
  } catch (ConnectException $e) {
    $logger->notice('HTTP request to @url failed with error: @error.', [
      '@url' => $uri,
      '@error' => $e->getMessage(),
    ]);
  }
  $remoteFileInfo->status = RemoteFileStatus::Error;
  return $remoteFileInfo;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.