You are here

public function JsonBlueprintDenormalizer::buildExecutionSequence in Subrequests 3.x

Same name and namespace in other branches
  1. 8.2 src/Normalizer/JsonBlueprintDenormalizer.php \Drupal\subrequests\Normalizer\JsonBlueprintDenormalizer::buildExecutionSequence()

Builds the execution sequence.

Builds an array where each position contains the IDs of the requests to be executed. All the IDs in the same position in the sequence can be executed in parallel.

Parameters

\Drupal\subrequests\Subrequest[] $parsed: The parsed requests.

Return value

SubrequestsTree The sequence of IDs grouped by execution order.

1 call to JsonBlueprintDenormalizer::buildExecutionSequence()
JsonBlueprintDenormalizer::denormalize in src/Normalizer/JsonBlueprintDenormalizer.php

File

src/Normalizer/JsonBlueprintDenormalizer.php, line 215

Class

JsonBlueprintDenormalizer
Denormalizer that builds the blueprint based on the incoming blueprint.

Namespace

Drupal\subrequests\Normalizer

Code

public function buildExecutionSequence(array $parsed) {
  $sequence = new SubrequestsTree();
  $rooted_reqs = array_filter($parsed, function (Subrequest $item) {
    return $item->waitFor === [
      '<ROOT>',
    ];
  });
  $sequence
    ->stack($rooted_reqs);
  $subreqs_with_unresolved_deps = array_values(array_filter($parsed, function (Subrequest $item) {
    return $item->waitFor !== [
      '<ROOT>',
    ];
  }));
  $dependency_is_resolved = function (Subrequest $item) use ($sequence) {
    return empty(array_diff($item->waitFor, $sequence
      ->allIds()));
  };
  while (count($subreqs_with_unresolved_deps)) {
    $no_deps = array_filter($subreqs_with_unresolved_deps, $dependency_is_resolved);
    if (empty($no_deps)) {
      throw new BadRequestHttpException('Waiting for unresolvable request. Abort.');
    }
    $sequence
      ->stack($no_deps);
    $subreqs_with_unresolved_deps = array_diff($subreqs_with_unresolved_deps, $no_deps);
  }
  return $sequence;
}