You are here

public function GatsbyPreview::triggerRefresh in Gatsby Live Preview & Incremental Builds 2.0.x

Same name and namespace in other branches
  1. 8 src/GatsbyPreview.php \Drupal\gatsby\GatsbyPreview::triggerRefresh()

Triggers Gatsby refresh endpoint.

Parameters

string $preview_callback_url: The Gatsby URL to refresh.

object $json: Optional JSON object to post to the server.

string $path: The path used to trigger the refresh endpoint.

1 call to GatsbyPreview::triggerRefresh()
GatsbyPreview::gatsbyUpdate in src/GatsbyPreview.php
Triggers the refreshing of Gatsby preview and incremental builds.

File

src/GatsbyPreview.php, line 186

Class

GatsbyPreview
Defines a class for generating Gatsby based previews.

Namespace

Drupal\gatsby

Code

public function triggerRefresh($preview_callback_url, $json = FALSE, $path = "") {

  // If the URL has a comma it means multiple end points need to be called.
  if (stripos($preview_callback_url, ',')) {
    $urls = array_map('trim', explode(',', $preview_callback_url));
    foreach ($urls as $url) {
      $this
        ->triggerRefresh($url, $json, $path);
    }
    return;
  }
  $data = [
    'timeout' => 1,
  ];

  // Optonally log the JSON data.
  $json_log = NULL;
  if (!empty($json)) {
    $data['json'] = $json;

    // Check for enabled setting and set up a logger debug message with JSON
    // object.
    $json_log = $this->configFactory
      ->get('gatsby.settings')
      ->get('log_json') ? $this->logger
      ->debug(json_encode($json)) : NULL;
  }
  try {
    $this->httpClient
      ->post($preview_callback_url . $path, $data);

    // Return logger with JSON object data.
    if (isset($json_log)) {
      return $json_log;
    }
  } catch (ConnectException $e) {

    // This is maintained for the legacy callback URL only.
    // Do nothing as no response is returned from the preview server.
  } catch (\Exception $e) {
    $this->logger
      ->error($e
      ->getMessage());
  }
}