— this contains BigPipe
* placeholders for the personalized parts of the page. Hence this sends the
* non-personalized parts of the page. Let's call it The Skeleton.
* 2. N chunks: a ';
/**
* The BigPipe placeholder replacements stop signal.
*
* @var string
*/
const STOP_SIGNAL = '';
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* The session.
*
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface
*/
protected $session;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* The HTTP kernel.
*
* @var \Symfony\Component\HttpKernel\HttpKernelInterface
*/
protected $httpKernel;
/**
* The event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Constructs a new BigPipe class.
*
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
* The session.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
* @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
* The HTTP kernel.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
*/
public function __construct(RendererInterface $renderer, SessionInterface $session, RequestStack $request_stack, HttpKernelInterface $http_kernel, EventDispatcherInterface $event_dispatcher, ConfigFactoryInterface $config_factory) {
$this->renderer = $renderer;
$this->session = $session;
$this->requestStack = $request_stack;
$this->httpKernel = $http_kernel;
$this->eventDispatcher = $event_dispatcher;
$this->configFactory = $config_factory;
}
/**
* Performs tasks before sending content (and rendering placeholders).
*/
protected function performPreSendTasks() {
// The content in the placeholders may depend on the session, and by the
// time the response is sent (see index.php), the session is already
// closed. Reopen it for the duration that we are rendering placeholders.
$this->session->start();
}
/**
* Performs tasks after sending content (and rendering placeholders).
*/
protected function performPostSendTasks() {
// Close the session again.
$this->session->save();
}
/**
* Sends a chunk.
*
* @param string|\Drupal\Core\Render\HtmlResponse $chunk
* The string or response to append. String if there's no cacheability
* metadata or attachments to merge.
*/
protected function sendChunk($chunk) {
assert(is_string($chunk) || $chunk instanceof HtmlResponse);
if ($chunk instanceof HtmlResponse) {
print $chunk->getContent();
}
else {
print $chunk;
}
flush();
}
/**
* Sends an HTML response in chunks using the BigPipe technique.
*
* @param \Drupal\big_pipe\Render\BigPipeResponse $response
* The BigPipe response to send.
*
* @internal
* This method should only be invoked by
* \Drupal\big_pipe\Render\BigPipeResponse, which is itself an internal
* class.
*/
public function sendContent(BigPipeResponse $response) {
$content = $response->getContent();
$attachments = $response->getAttachments();
// First, gather the BigPipe placeholders that must be replaced.
$placeholders = isset($attachments['big_pipe_placeholders']) ? $attachments['big_pipe_placeholders'] : [];
$nojs_placeholders = isset($attachments['big_pipe_nojs_placeholders']) ? $attachments['big_pipe_nojs_placeholders'] : [];
// BigPipe sends responses using "Transfer-Encoding: chunked". To avoid
// sending already-sent assets, it is necessary to track cumulative assets
// from all previously rendered/sent chunks.
// @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.41
$cumulative_assets = AttachedAssets::createFromRenderArray(['#attached' => $attachments]);
$cumulative_assets->setAlreadyLoadedLibraries($attachments['library']);
$this->performPreSendTasks();
// Find the closing