You are here

public function JsonSubrequestDenormalizer::denormalize in Subrequests 3.x

Same name and namespace in other branches
  1. 8.2 src/Normalizer/JsonSubrequestDenormalizer.php \Drupal\subrequests\Normalizer\JsonSubrequestDenormalizer::denormalize()
  2. 8 src/Normalizer/JsonSubrequestDenormalizer.php \Drupal\subrequests\Normalizer\JsonSubrequestDenormalizer::denormalize()

Denormalizes data back into an object of the given class.

Parameters

mixed $data: data to restore.

string $class: the expected class to instantiate.

string $format: format the given data was extracted from.

array $context: options available to the denormalizer.

Return value

object

File

src/Normalizer/JsonSubrequestDenormalizer.php, line 29

Class

JsonSubrequestDenormalizer
Creates a request object for each Subrequest.

Namespace

Drupal\subrequests\Normalizer

Code

public function denormalize($data, $class, $format = NULL, array $context = []) {

  /** @var \Drupal\subrequests\Subrequest $data */
  $path = parse_url($data->uri, PHP_URL_PATH);
  $query = parse_url($data->uri, PHP_URL_QUERY) ?: [];
  if (isset($query) && !is_array($query)) {
    $_query = [];
    parse_str($query, $_query);
    $query = $_query;
  }

  /** @var \Symfony\Component\HttpFoundation\Request $master_request */
  $master_request = $context['master_request'];
  $request = Request::create($path, static::getMethodFromAction($data->action), empty($data->body) ? $query : $data->body, $master_request->cookies ? $master_request->cookies
    ->all() : [], $master_request->files ? $master_request->files
    ->all() : [], [], empty($data->body) ? '' : Json::encode($data->body));

  // Maintain the same session as in the master request.
  $session = $master_request
    ->getSession();
  $request
    ->setSession($session);

  // Replace the headers by the ones in the subrequest.
  foreach ($data->headers as $name => $value) {
    $request->headers
      ->set($name, $value);
  }
  $this::fixBasicAuth($request);

  // Add the content ID to the sub-request.
  $content_id = empty($data->requestId) ? md5(serialize($data)) : $data->requestId;
  $request->headers
    ->set('Content-ID', '<' . $content_id . '>');
  return $request;
}