1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the rybakit/twig-deferred-extension package. |
5
|
|
|
* |
6
|
|
|
* (c) Eugene Leonovich <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Twig\DeferredExtension; |
15
|
|
|
|
16
|
|
|
use Twig\Extension\AbstractExtension; |
17
|
|
|
use Twig\Template; |
18
|
|
|
|
19
|
|
|
final class DeferredExtension extends AbstractExtension |
20
|
|
|
{ |
21
|
|
|
private $blocks = []; |
22
|
|
|
|
23
|
|
|
public function getTokenParsers() : array |
24
|
|
|
{ |
25
|
|
|
return [new DeferredTokenParser()]; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getNodeVisitors() : array |
29
|
|
|
{ |
30
|
|
|
return [new DeferredNodeVisitor()]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function defer(Template $template, string $blockName) : void |
34
|
|
|
{ |
35
|
|
|
$templateName = $template->getTemplateName(); |
36
|
|
|
$this->blocks[$templateName][] = $blockName; |
37
|
|
|
\ob_start(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function resolve(Template $template, array $context, array $blocks) : void |
41
|
|
|
{ |
42
|
|
|
$templateName = $template->getTemplateName(); |
43
|
|
|
if (empty($this->blocks[$templateName])) { |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
while ($blockName = \array_pop($this->blocks[$templateName])) { |
48
|
|
|
$buffer = \ob_get_clean(); |
49
|
|
|
|
50
|
|
|
$blocks[$blockName] = [$template, 'block_'.$blockName.'_deferred']; |
51
|
|
|
$template->displayBlock($blockName, $context, $blocks); |
52
|
|
|
|
53
|
|
|
echo $buffer; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($parent = $template->getParent($context)) { |
57
|
|
|
$this->resolve($parent, $context, $blocks); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.