processPlaceholders example

public function onRespond(ResponseEvent $event) {
    $response = $event->getResponse();
    if (!$response instanceof HtmlResponse) {
      return;
    }

    $attachments = $response->getAttachments();
    if (empty($attachments['placeholders'])) {
      return;
    }

    $attachments['placeholders'] = $this->placeholderStrategy->processPlaceholders($attachments['placeholders']);

    $response->setAttachments($attachments);
  }

  /** * {@inheritdoc} */
  public static function getSubscribedEvents(): array {
    // Run shortly before HtmlResponseSubscriber.     $events[KernelEvents::RESPONSE][] = ['onRespond', 5];
    return $events;
  }


    // Assert that there is at least one strategy.     assert(!empty($this->placeholderStrategies), 'At least one placeholder strategy must be present; by default the fallback strategy \Drupal\Core\Render\Placeholder\SingleFlushStrategy is always present.');

    $new_placeholders = [];

    // Give each placeholder strategy a chance to replace all not-yet replaced     // placeholders. The order of placeholder strategies is well defined     // and this uses a variation of the "chain of responsibility" design pattern.     foreach ($this->placeholderStrategies as $strategy) {
      $processed_placeholders = $strategy->processPlaceholders($placeholders);
      assert(array_intersect_key($processed_placeholders$placeholders) === $processed_placeholders, 'Processed placeholders must be a subset of all placeholders.');
      $placeholders = array_diff_key($placeholders$processed_placeholders);
      $new_placeholders += $processed_placeholders;

      if (empty($placeholders)) {
        break;
      }
    }

    return $new_placeholders;
  }

}
$session_configuration->hasSession(Argument::type(Request::class))
      ->willReturn($request_has_session);

    $route = $this->prophesize(Route::class);
    $route->getOption('_no_big_pipe')
      ->willReturn($route_match_has_no_big_pipe_option);
    $route_match = $this->prophesize(RouteMatchInterface::class);
    $route_match->getRouteObject()
      ->willReturn($route);

    $big_pipe_strategy = new BigPipeStrategy($session_configuration->reveal()$request_stack->reveal()$route_match->reveal());
    $processed_placeholders = $big_pipe_strategy->processPlaceholders($placeholders);

    if ($request->isMethodCacheable() && !$route_match_has_no_big_pipe_option && $request_has_session) {
      $this->assertSameSize($expected_big_pipe_placeholders$processed_placeholders, 'BigPipe is able to deliver all placeholders.');
      foreach (array_keys($placeholders) as $placeholder) {
        $this->assertSame($expected_big_pipe_placeholders[$placeholder]$processed_placeholders[$placeholder], "Verifying how BigPipeStrategy handles the placeholder '$placeholder'");
      }
    }
    else {
      $this->assertCount(0, $processed_placeholders);
    }
  }

  

  public function testProcessPlaceholders($strategies$placeholders$result) {
    $chained_placeholder_strategy = new ChainedPlaceholderStrategy();

    foreach ($strategies as $strategy) {
      $chained_placeholder_strategy->addPlaceholderStrategy($strategy);
    }

    $this->assertEquals($result$chained_placeholder_strategy->processPlaceholders($placeholders));
  }

  /** * Provides a list of render strategies, placeholders and results. * * @return array */
  public static function providerProcessPlaceholders() {
    $prophet = new Prophet();
    $data = [];

    
Home | Imprint | This part of the site doesn't use cookies.