You are here

public function RequestTree::dereference in Subrequests 8

Resolves the JSON Pointer references.

@todo For now we are forcing the use of JSON Pointer as the only format to reference properties in existing responses. Allow pluggability, this step should probably be better placed in the subrequest normalizer.

Parameters

\Symfony\Component\HttpFoundation\Response[] $responses: Previous responses available.

File

src/Blueprint/RequestTree.php, line 148

Class

RequestTree
Contains the hierarchical information of the requests.

Namespace

Drupal\subrequests\Blueprint

Code

public function dereference(array $responses) {
  $this->requests = array_map(function (Request $request) use ($responses) {
    $id = $request->attributes
      ->get(static::SUBREQUEST_ID);
    $parent_id = $request->attributes
      ->get(static::SUBREQUEST_PARENT_ID);

    // Allow replacement tokens in:
    //   1. The body.
    //   2. The path.
    //   3. The query string values.
    $content = $request
      ->getContent();
    $changes = static::replaceAllOccurrences($responses, $content);
    $uri = $request
      ->getUri();
    $changes += static::replaceAllOccurrences($responses, $uri);
    foreach ($request->query as $key => $value) {
      $new_key = $key;
      $query_changes = static::replaceAllOccurrences($responses, $new_key);
      $query_changes += static::replaceAllOccurrences($responses, $value);
      if ($query_changes) {
        $request->query
          ->remove($key);
        $request->query
          ->set($new_key, $value);
      }
    }

    // If there is anything to update.
    if ($changes) {

      // We need to duplicate the request to force recomputing the internal
      // caches.
      $request = static::cloneRequest($request, $uri, $content, $id, $parent_id);
    }
    return $request;
  }, $this
    ->getRequests());
}