|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Latte (https://latte.nette.org) |
| 5 | + * Copyright (c) 2008 David Grudl (https://davidgrudl.com) |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Nette\Bridges\CacheLatte\Nodes; |
| 11 | + |
| 12 | +use Latte; |
| 13 | +use Latte\Compiler\Nodes\AreaNode; |
| 14 | +use Latte\Compiler\Nodes\Php\Expression\ArrayNode; |
| 15 | +use Latte\Compiler\Nodes\StatementNode; |
| 16 | +use Latte\Compiler\Position; |
| 17 | +use Latte\Compiler\PrintContext; |
| 18 | +use Latte\Compiler\Tag; |
| 19 | +use Nette; |
| 20 | +use Nette\Caching\Cache; |
| 21 | + |
| 22 | + |
| 23 | +/** |
| 24 | + * {cache} ... {/cache} |
| 25 | + */ |
| 26 | +class CacheNode extends StatementNode |
| 27 | +{ |
| 28 | + public ArrayNode $args; |
| 29 | + public AreaNode $content; |
| 30 | + public ?Position $endLine; |
| 31 | + |
| 32 | + |
| 33 | + /** @return \Generator<int, ?array, array{AreaNode, ?Tag}, static> */ |
| 34 | + public static function create(Tag $tag): \Generator |
| 35 | + { |
| 36 | + $node = new static; |
| 37 | + $node->args = $tag->parser->parseArguments(); |
| 38 | + [$node->content, $endTag] = yield; |
| 39 | + $node->endLine = $endTag?->position; |
| 40 | + return $node; |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + public function print(PrintContext $context): string |
| 45 | + { |
| 46 | + return $context->format( |
| 47 | + <<<'XX' |
| 48 | + if (Nette\Bridges\CacheLatte\Nodes\CacheNode::createCache($this->global->cacheStorage, %dump, $this->global->cacheStack, %node?)) %line |
| 49 | + try { |
| 50 | + %node |
| 51 | + Nette\Bridges\CacheLatte\Nodes\CacheNode::endCache($this->global->cacheStack) %line; |
| 52 | + } catch (\Throwable $ʟ_e) { |
| 53 | + Nette\Bridges\CacheLatte\Nodes\CacheNode::rollback($this->global->cacheStack); throw $ʟ_e; |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + XX, |
| 58 | + Nette\Utils\Random::generate(), |
| 59 | + $this->args, |
| 60 | + $this->position, |
| 61 | + $this->content, |
| 62 | + $this->endLine, |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + public function &getIterator(): \Generator |
| 68 | + { |
| 69 | + yield $this->args; |
| 70 | + yield $this->content; |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + /********************* run-time helpers ****************d*g**/ |
| 75 | + |
| 76 | + |
| 77 | + public static function initRuntime(Latte\Runtime\Template $template): void |
| 78 | + { |
| 79 | + if (!empty($template->global->cacheStack)) { |
| 80 | + $file = (new \ReflectionClass($template))->getFileName(); |
| 81 | + if (@is_file($file)) { // @ - may trigger error |
| 82 | + end($template->global->cacheStack)->dependencies[Cache::Files][] = $file; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + /** |
| 89 | + * Starts the output cache. Returns Nette\Caching\OutputHelper object if buffering was started. |
| 90 | + * @return Nette\Caching\OutputHelper|\stdClass |
| 91 | + */ |
| 92 | + public static function createCache( |
| 93 | + Nette\Caching\Storage $cacheStorage, |
| 94 | + string $key, |
| 95 | + ?array &$parents, |
| 96 | + ?array $args = null, |
| 97 | + ) { |
| 98 | + if ($args) { |
| 99 | + if (array_key_exists('if', $args) && !$args['if']) { |
| 100 | + return $parents[] = new \stdClass; |
| 101 | + } |
| 102 | + |
| 103 | + $key = array_merge([$key], array_intersect_key($args, range(0, count($args)))); |
| 104 | + } |
| 105 | + |
| 106 | + if ($parents) { |
| 107 | + end($parents)->dependencies[Cache::Items][] = $key; |
| 108 | + } |
| 109 | + |
| 110 | + $cache = new Cache($cacheStorage, 'Nette.Templating.Cache'); |
| 111 | + if ($helper = $cache->capture($key)) { |
| 112 | + $parents[] = $helper; |
| 113 | + |
| 114 | + if (isset($args['dependencies'])) { |
| 115 | + $args += $args['dependencies'](); |
| 116 | + } |
| 117 | + |
| 118 | + $helper->dependencies[Cache::Tags] = $args['tags'] ?? null; |
| 119 | + $helper->dependencies[Cache::Expire] = $args['expiration'] ?? $args['expire'] ?? '+ 7 days'; |
| 120 | + } |
| 121 | + |
| 122 | + return $helper; |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + /** |
| 127 | + * Ends the output cache. |
| 128 | + * @param Nette\Caching\OutputHelper[] $parents |
| 129 | + */ |
| 130 | + public static function endCache(array &$parents): void |
| 131 | + { |
| 132 | + $helper = array_pop($parents); |
| 133 | + if ($helper instanceof Nette\Caching\OutputHelper) { |
| 134 | + $helper->end(); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | + /** |
| 140 | + * @param Nette\Caching\OutputHelper[] $parents |
| 141 | + */ |
| 142 | + public static function rollback(array &$parents): void |
| 143 | + { |
| 144 | + $helper = array_pop($parents); |
| 145 | + if ($helper instanceof Nette\Caching\OutputHelper) { |
| 146 | + $helper->rollback(); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments