-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Template runtime and block composition #4917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fabpot
wants to merge
19
commits into
twigphp:3.x
Choose a base branch
from
fabpot:template-runtime-and-block-composition
base: 3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
1340a5c
Make extra integration tests compatible with Twig 4
fabpot 0f28b0f
Add template block composition
fabpot 7747406
Document template block composition
fabpot 25577e6
Preserve frozen lineage macro imports
fabpot 6072d21
Cover explicit template block resolution
fabpot 52aaf96
Store BlockChain blocks without duplicate metadata
fabpot be68c43
Clarify BlockChain owner identity
fabpot b37acf5
Fix frozen block chain isolation
fabpot 733e77a
Preserve block resolution object identities
fabpot 87cec07
Restore buffers after parent block errors
fabpot d03134f
Keep block chain macro imports live
fabpot 42dd021
Expand block chain regression coverage
fabpot bf18ed1
Simplify block chain runtime handling
fabpot c232483
Restore focused block chain macro coverage
fabpot 0ffea8b
Shorten block composition documentation
fabpot c0c1d4c
Clarify block composition documentation
fabpot f932014
Mention BlockChain ecosystem use cases
fabpot 5ba9acb
Make block chain import test PHPUnit 11 compatible
fabpot 52d50d6
Fix block streaming globals
fabpot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of Twig. | ||
| * | ||
| * (c) Fabien Potencier | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Twig; | ||
|
|
||
| use Twig\Error\RuntimeError; | ||
|
|
||
| /** | ||
| * Composes blocks from several templates without composing their bodies or macros. | ||
| */ | ||
| final class BlockChain | ||
| { | ||
| /** @var array<string, array{Template, string}> */ | ||
| private array $blocks; | ||
| private Template $template; | ||
|
|
||
| /** | ||
| * @param iterable<string|TemplateWrapper> $templates Templates ordered from highest to lowest precedence | ||
| */ | ||
| public function __construct( | ||
| private Environment $env, | ||
| iterable $templates, | ||
| array $context = [], | ||
| ) { | ||
| $resolution = new BlockResolutionContext($env, $context + $env->getGlobals()); | ||
| $blocks = []; | ||
|
|
||
| foreach ($templates as $template) { | ||
| if (\is_string($template)) { | ||
| $template = $env->load($template); | ||
| } | ||
| if (!$template instanceof TemplateWrapper) { | ||
| throw new \TypeError(\sprintf('Block chain templates must be strings or "%s" instances, "%s" given.', TemplateWrapper::class, get_debug_type($template))); | ||
| } | ||
|
|
||
| $current = $template->unwrap()->freezeLineage($resolution); | ||
| $this->template ??= $current; | ||
| do { | ||
| foreach ($current->getBlocks() as $name => $block) { | ||
| if (isset($blocks[$name])) { | ||
| continue; | ||
| } | ||
| if (!\is_array($block) || !isset($block[0], $block[1]) || !$block[0] instanceof Template || !\is_string($block[1])) { | ||
| throw new \LogicException('A block must be a method on a \Twig\Template instance.'); | ||
| } | ||
|
|
||
| $resolution->assertOwns($block[0]); | ||
| $blocks[$name] = $block; | ||
| } | ||
| } while (false !== $current = $resolution->getParent($current)); | ||
| } | ||
|
|
||
| if (!isset($this->template)) { | ||
| throw new \InvalidArgumentException('A block chain requires at least one template.'); | ||
| } | ||
|
|
||
| $this->blocks = $blocks; | ||
| } | ||
|
|
||
| public function hasBlock(string $name): bool | ||
| { | ||
| return isset($this->blocks[$name]); | ||
| } | ||
|
|
||
| /** | ||
| * @return string[] | ||
| */ | ||
| public function getBlockNames(): array | ||
| { | ||
| return array_keys($this->blocks); | ||
| } | ||
|
|
||
| /** | ||
| * @return iterable<scalar|\Stringable|null> | ||
| */ | ||
| public function streamBlock(string $name, array $context = []): iterable | ||
| { | ||
| yield from $this->getBlock($name)->yieldBlock($name, $context + $this->env->getGlobals(), $this->blocks); | ||
| } | ||
|
|
||
| public function renderBlock(string $name, array $context = []): string | ||
| { | ||
| return $this->getBlock($name)->renderBlock($name, $context + $this->env->getGlobals(), $this->blocks); | ||
| } | ||
|
|
||
| public function displayBlock(string $name, array $context = []): void | ||
| { | ||
| $this->getBlock($name)->displayBlock($name, $context + $this->env->getGlobals(), $this->blocks); | ||
| } | ||
|
|
||
| private function getBlock(string $name): Template | ||
| { | ||
| if (isset($this->blocks[$name])) { | ||
| return $this->blocks[$name][0]; | ||
| } | ||
|
|
||
| throw new RuntimeError(\sprintf('Block "%s" on template "%s" does not exist.', $name, $this->template->getTemplateName()), -1, $this->template->getSourceContext()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of Twig. | ||
| * | ||
| * (c) Fabien Potencier | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Twig; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| final class BlockResolutionContext | ||
| { | ||
| /** @var \SplObjectStorage<Template, Template|false> */ | ||
| private \SplObjectStorage $parents; | ||
|
|
||
| /** @var \SplObjectStorage<Template, Template> */ | ||
| private \SplObjectStorage $frozen; | ||
|
|
||
| /** @var \SplObjectStorage<Template, true> */ | ||
| private \SplObjectStorage $freezing; | ||
|
|
||
| public function __construct( | ||
| private Environment $env, | ||
| private array $context, | ||
| ) { | ||
| $this->parents = new \SplObjectStorage(); | ||
| $this->frozen = new \SplObjectStorage(); | ||
| $this->freezing = new \SplObjectStorage(); | ||
| } | ||
|
|
||
| public function getParent(Template $template): Template|false | ||
| { | ||
| if ($this->parents->offsetExists($template)) { | ||
| return $this->parents[$template]; | ||
| } | ||
|
|
||
| $parent = $template->getParent($this->context); | ||
| if ($parent instanceof TemplateWrapper) { | ||
| $parent = $parent->unwrap(); | ||
| } | ||
|
|
||
| return $this->parents[$template] = $parent; | ||
| } | ||
|
|
||
| public function assertOwns(Template $template): void | ||
| { | ||
| if (!$template->isOwnedBy($this->env)) { | ||
| throw new \LogicException('A block chain cannot contain templates from different Twig environments.'); | ||
| } | ||
| } | ||
|
|
||
| public function isFrozen(Template $template): bool | ||
| { | ||
| return $this->frozen->offsetExists($template); | ||
| } | ||
|
|
||
| public function getFrozen(Template $template): Template | ||
| { | ||
| return $this->frozen[$template]; | ||
| } | ||
|
|
||
| public function setFrozen(Template $template, Template $frozen): void | ||
| { | ||
| $this->frozen[$template] = $frozen; | ||
| } | ||
|
|
||
| public function beginFreeze(Template $template): void | ||
| { | ||
| if ($this->freezing->offsetExists($template)) { | ||
| throw new \LogicException(\sprintf('Circular template inheritance detected while building a block chain from "%s".', $template->getTemplateName())); | ||
| } | ||
|
|
||
| $this->freezing[$template] = true; | ||
| } | ||
|
|
||
| public function endFreeze(Template $template): void | ||
| { | ||
| $this->freezing->offsetUnset($template); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Warning — Chain construction loses template/line context and bypasses Twig's error contract.
Parent resolution during construction runs user expressions (
{% extends parent|filter %}, dynamic parent names) outside of any Twig error handling, so a non-Twig\Error\Errorexception escapesnew BlockChain(...)raw. Verified on this checkout: with'theme' => '{% extends parent|boom %}...'whereboomthrows,$twig->render('theme')yieldsTwig\Error\RuntimeError: An exception has been thrown during the rendering of a template ("kaboom") in "theme" at line 1., whilenew BlockChain($twig, ['theme'], ['parent' => 'parent'])propagates a bareDomainException: kaboomwith no template name or line. The same happens for a dynamic parent expression that evaluates to null (Template::load(): Argument #1 ... null givenTypeError escapes construction, where rendering reports it as a RuntimeError in "theme" at line 1). Callers that catchTwig\Error\Erroraround chain construction will not catch these, and the failing template is not identified.