Skip to content

Commit

Permalink
feature #3943 Deprecate passing a Template instance in Environment::r…
Browse files Browse the repository at this point in the history
…esolveTemplate() and Template::loadTemplate() (fabpot)

This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Deprecate passing a Template instance in Environment::resolveTemplate() and Template::loadTemplate()

Commits
-------

dbe9456 Fix incompatibility with old PHP versions
a62581a Fix typo
61031d6 Deprecate passing a Template instance in Environment::resolveTemplate() and Template::loadTemplate()
  • Loading branch information
fabpot committed Apr 14, 2024
2 parents f6d620c + dbe9456 commit c8ec14c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -1,6 +1,7 @@
# 3.9.0 (2024-XX-XX)

* Deprecate AbstractNodeVisitor
* Deprecate passing Template to Environment::resolveTemplate(), Environment::load(), and Template::loadTemplate()
* Add a new "yield" mode for output generation;
Node implementations that use "echo" or "print" should use "yield" instead;
all Node implementations should be flagged with `#[YieldReady]` once they've been made ready for "yield";
Expand Down
8 changes: 8 additions & 0 deletions doc/deprecated.rst
Expand Up @@ -23,3 +23,11 @@ Node Visitors

* The ``Twig\NodeVisitor\AbstractNodeVisitor`` class is deprecated, implement the
``Twig\NodeVisitor\NodeVisitorInterface`` interface instead.

Templates
---------

* Passing ``Twig\\Template`` instances to Twig public API is deprecated (like

Check failure on line 30 in doc/deprecated.rst

View workflow job for this annotation

GitHub Actions / DOCtor-RST

Please use 1 backslash when highlighting a namespace with double backticks: ``Twig\\Template``
in ``Environment::resolveTemplate()``, ``Environment::load()``, and
``Template::loadTemplate()``); pass instances of ``Twig\\TemplateWrapper``

Check failure on line 32 in doc/deprecated.rst

View workflow job for this annotation

GitHub Actions / DOCtor-RST

Please use 1 backslash when highlighting a namespace with double backticks: ``Twig\\TemplateWrapper``
instead.
13 changes: 10 additions & 3 deletions src/Environment.php
Expand Up @@ -327,6 +327,11 @@ public function load($name): TemplateWrapper
if ($name instanceof TemplateWrapper) {
return $name;
}
if ($name instanceof Template) {
trigger_deprecation('twig/twig', '3.9', 'Passing a "%s" instance to "%s" is deprecated.', self::class, __METHOD__);

return $name;
}

return new TemplateWrapper($this, $this->loadTemplate($this->getTemplateClass($name), $name));
}
Expand Down Expand Up @@ -440,10 +445,10 @@ public function isTemplateFresh(string $name, int $time): bool
/**
* Tries to load a template consecutively from an array.
*
* Similar to load() but it also accepts instances of \Twig\Template and
* \Twig\TemplateWrapper, and an array of templates where each is tried to be loaded.
* Similar to load() but it also accepts instances of \Twig\TemplateWrapper
* and an array of templates where each is tried to be loaded.
*
* @param string|TemplateWrapper|array $names A template or an array of templates to try consecutively
* @param string|TemplateWrapper|array<string|TemplateWrapper> $names A template or an array of templates to try consecutively
*
* @throws LoaderError When none of the templates can be found
* @throws SyntaxError When an error occurred during compilation
Expand All @@ -457,6 +462,8 @@ public function resolveTemplate($names): TemplateWrapper
$count = \count($names);
foreach ($names as $name) {
if ($name instanceof Template) {
trigger_deprecation('twig/twig', '3.9', 'Passing a "%s" instance to "%s" is deprecated.', Template::class, __METHOD__);

return new TemplateWrapper($this, $name);
}
if ($name instanceof TemplateWrapper) {
Expand Down
10 changes: 9 additions & 1 deletion src/Template.php
Expand Up @@ -249,6 +249,8 @@ public function getBlockNames(array $context, array $blocks = [])
}

/**
* @param string|TemplateWrapper|array<string|TemplateWrapper> $template
*
* @return self|TemplateWrapper
*/
protected function loadTemplate($template, $templateName = null, $line = null, $index = null)
Expand All @@ -258,7 +260,13 @@ protected function loadTemplate($template, $templateName = null, $line = null, $
return $this->env->resolveTemplate($template);
}

if ($template instanceof self || $template instanceof TemplateWrapper) {
if ($template instanceof TemplateWrapper) {
return $template;
}

if ($template instanceof self) {
trigger_deprecation('twig/twig', '3.9', 'Passing a "%s" instance to "%s" is deprecated.', self::class, __METHOD__);

return $template;
}

Expand Down

0 comments on commit c8ec14c

Please sign in to comment.