-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEsiStrategy.php
62 lines (58 loc) · 1.94 KB
/
EsiStrategy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace Drupal\esi_placeholders\Render\Placeholder;
use Drupal\big_pipe\Render\Placeholder\BigPipeStrategy;
use Drupal\Core\Render\Markup;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\HttpCache\Esi;
class EsiStrategy extends BigPipeStrategy
{
/**
* @var RequestStack
*/
protected $requestStack;
/**
* @var Esi
*/
protected $esi;
/**
* EsiStrategy constructor.
* @param RequestStack $request_stack
* @param Esi $esi
*/
public function __construct(RequestStack $request_stack, Esi $esi)
{
$this->requestStack = $request_stack;
$this->esi = $esi;
}
/**
* @param array $placeholders
* @return array
*/
public function processPlaceholders(array $placeholders)
{
$request = $this->requestStack->getCurrentRequest();
$overridenPlaceHolder = [];
foreach ($placeholders as $placeholder => $placeholder_elements) {
// ESI works at the HTML tag level. But placeholders can also be
// HTML tag attribute values!
// @see \Drupal\big_pipe\Render\Placeholder\BigPipeStrategy::doProcessPlaceholders()
if (!static::placeholderIsAttributeSafe($placeholder)) {
continue;
}
if (isset($placeholder_elements['#lazy_builder']) && $this->esi->hasSurrogateCapability($request)) {
$overridenPlaceHolder[$placeholder] = [
'#markup' =>
Markup::create(
$this->esi->renderIncludeTag(
'/esi/block/?'.
$this->generateBigPipePlaceholderId($placeholder,$placeholder_elements),
null,
false
)
)
];
}
}
return $overridenPlaceHolder;
}
}