mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-30 20:16:45 +00:00
Add a deprecation when a Node uses echo/print
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
# 3.9.0 (2023-XX-XX)
|
||||
# 3.9.0 (2024-XX-XX)
|
||||
|
||||
* Add a new "yield" mode for output generation
|
||||
The "use_yield" Environment option controls the strategy: use "false" for "echo", "true" for "yield"
|
||||
The "use_yield" Environment option controls the output strategy: use "false" for "echo", "true" for "yield"
|
||||
"yield" will be the only strategy supported in the next major version
|
||||
* Add return type for Symfony 7 compatibility
|
||||
* Fix premature loop exit in Security Policy lookup of allowed methods/properties
|
||||
* Deprecate all internal extension functions in favor of methods on the extension classes
|
||||
|
||||
@@ -30,7 +30,7 @@ class Kernel extends BaseKernel
|
||||
'router' => ['utf8' => true],
|
||||
'http_method_override' => false,
|
||||
];
|
||||
if (Kernel::MAJOR_VERSION >= 6 && Kernel::MINOR_VERSION >= 2) {
|
||||
if (6 === Kernel::MAJOR_VERSION) {
|
||||
$config['handle_all_throwables'] = true;
|
||||
$config['php_errors']['log'] = true;
|
||||
}
|
||||
|
||||
@@ -27,10 +27,12 @@ class Compiler
|
||||
private $sourceOffset;
|
||||
private $sourceLine;
|
||||
private $varNameSalt = 0;
|
||||
private $checkForOutput;
|
||||
|
||||
public function __construct(Environment $env)
|
||||
{
|
||||
$this->env = $env;
|
||||
$this->checkForOutput = $env->isDebug();
|
||||
}
|
||||
|
||||
public function getEnvironment(): Environment
|
||||
@@ -85,6 +87,16 @@ class Compiler
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function checkForOutput(bool $checkForOutput)
|
||||
{
|
||||
$this->checkForOutput = $checkForOutput ? $this->env->isDebug() : false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a raw string to the compiled code.
|
||||
*
|
||||
@@ -92,6 +104,9 @@ class Compiler
|
||||
*/
|
||||
public function raw(string $string)
|
||||
{
|
||||
if ($this->checkForOutput) {
|
||||
$this->checkStringForOutput(trim($string));
|
||||
}
|
||||
$this->source .= $string;
|
||||
|
||||
return $this;
|
||||
@@ -105,6 +120,10 @@ class Compiler
|
||||
public function write(...$strings)
|
||||
{
|
||||
foreach ($strings as $string) {
|
||||
if ($this->checkForOutput) {
|
||||
$this->checkStringForOutput(trim($string));
|
||||
}
|
||||
|
||||
$this->source .= str_repeat(' ', $this->indentation * 4).$string;
|
||||
}
|
||||
|
||||
@@ -220,4 +239,13 @@ class Compiler
|
||||
{
|
||||
return sprintf('__internal_compile_%d', $this->varNameSalt++);
|
||||
}
|
||||
|
||||
private function checkStringForOutput(string $string): void
|
||||
{
|
||||
if (str_starts_with($string, 'echo')) {
|
||||
trigger_deprecation('twig/twig', '3.9.0', 'Using "echo" in a "Node::compile()" method is deprecated; use a "TextNode" or "PrintNode" instead or use "yield" when "use_yield" is "true" on the environment (triggered by "%s").', $string);
|
||||
} elseif (str_starts_with($string, 'print')) {
|
||||
trigger_deprecation('twig/twig', '3.9.0', 'Using "print" in a "Node::compile()" method is deprecated; use a "TextNode" or "PrintNode" instead or use "yield" when "use_yield" is "true" on the environment (triggered by "%s").', $string);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,9 @@ final class InlinePrint extends AbstractExpression
|
||||
;
|
||||
} else {
|
||||
$compiler
|
||||
->checkForOutput(false)
|
||||
->raw('print(')
|
||||
->checkForOutput(true)
|
||||
->subcompile($this->getNode('node'))
|
||||
->raw(')')
|
||||
;
|
||||
|
||||
@@ -34,7 +34,11 @@ class PrintNode extends Node implements NodeOutputInterface
|
||||
if ($compiler->getEnvironment()->useYield()) {
|
||||
$compiler->write('yield ');
|
||||
} else {
|
||||
$compiler->write('echo ');
|
||||
$compiler
|
||||
->checkForOutput(false)
|
||||
->write('echo ')
|
||||
->checkForOutput(true)
|
||||
;
|
||||
}
|
||||
|
||||
$compiler
|
||||
|
||||
@@ -33,7 +33,11 @@ class TextNode extends Node implements NodeOutputInterface
|
||||
if ($compiler->getEnvironment()->useYield()) {
|
||||
$compiler->write('yield ');
|
||||
} else {
|
||||
$compiler->write('echo ');
|
||||
$compiler
|
||||
->checkForOutput(false)
|
||||
->write('echo ')
|
||||
->checkForOutput(true)
|
||||
;
|
||||
}
|
||||
|
||||
$compiler
|
||||
|
||||
Reference in New Issue
Block a user