Skip to content

Commit

Permalink
Added "deprecated" tag
Browse files Browse the repository at this point in the history
  • Loading branch information
yceruto committed Jun 4, 2018
1 parent b9c6034 commit a0f1c66
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 0 deletions.
17 changes: 17 additions & 0 deletions doc/tags/deprecated.rst
@@ -0,0 +1,17 @@
``deprecated``
==============

.. versionadded:: 1.x
The ``deprecated`` tag was added in Twig 1.x.

Twig generates a deprecation notice (via a call to the ``trigger_error()``
PHP function) where the ``deprecated`` tag is used in a template:

.. code-block:: jinja
{# base.twig #}
{% deprecated 'The "base.twig" template is deprecated, use "layout.twig" instead.' %}
{% extends 'layout.twig' %}
Note that by default, the deprecation notices are silenced and never displayed nor logged.
See :ref:`deprecation-notices` to learn how to handle them.
1 change: 1 addition & 0 deletions doc/tags/index.rst
Expand Up @@ -23,3 +23,4 @@ Tags
use
verbatim
with
deprecated
1 change: 1 addition & 0 deletions lib/Twig/Extension/Core.php
Expand Up @@ -136,6 +136,7 @@ public function getTokenParsers()
new Twig_TokenParser_Do(),
new Twig_TokenParser_Embed(),
new Twig_TokenParser_With(),
new Twig_TokenParser_Deprecated(),
);
}

Expand Down
35 changes: 35 additions & 0 deletions lib/Twig/Node/Deprecated.php
@@ -0,0 +1,35 @@
<?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.
*/

/**
* Represents a deprecated node.
*
* @author Yonel Ceruto <yonelceruto@gmail.com>
*/
class Twig_Node_Deprecated extends Twig_Node
{
public function __construct(Twig_Node_Expression $expr, $lineno, $tag = null)
{
parent::__construct(array('expr' => $expr), array(), $lineno, $tag);
}

public function compile(Twig_Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write('@trigger_error(')
->subcompile($this->getNode('expr'))
->raw(", E_USER_DEPRECATED);\n")
;
}
}

class_alias('Twig_Node_Deprecated', 'Twig\Node\DeprecatedNode', false);
42 changes: 42 additions & 0 deletions lib/Twig/TokenParser/Deprecated.php
@@ -0,0 +1,42 @@
<?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.
*/

/**
* Deprecates a section of a template.
*
* <pre>
* {% deprecated 'The "base.html.twig" template is deprecated, use "layout.html.twig" instead.' %}
*
* {% extends 'layout.html.twig' %}
* </pre>
*
* @author Yonel Ceruto <yonelceruto@gmail.com>
*
* @final
*/
class Twig_TokenParser_Deprecated extends Twig_TokenParser
{
public function parse(Twig_Token $token)
{
$expr = $this->parser->getExpressionParser()->parseExpression();

$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);

return new Twig_Node_Deprecated($expr, $token->getLine(), $this->getTag());
}

public function getTag()
{
return 'deprecated';
}
}

class_alias('Twig_TokenParser_Deprecated', 'Twig\TokenParser\DeprecatedTokenParser', false);
11 changes: 11 additions & 0 deletions src/Node/DeprecatedNode.php
@@ -0,0 +1,11 @@
<?php

namespace Twig\Node;

class_exists('Twig_Node_Deprecated');

if (\false) {
class DeprecatedNode extends \Twig_Node_Deprecated
{
}
}
11 changes: 11 additions & 0 deletions src/TokenParser/DeprecatedTokenParser.php
@@ -0,0 +1,11 @@
<?php

namespace Twig\TokenParser;

class_exists('Twig_TokenParser_Deprecated');

if (\false) {
class DeprecatedTokenParser extends \Twig_TokenParser_Deprecated
{
}
}
32 changes: 32 additions & 0 deletions test/Twig/Tests/Node/DeprecatedTest.php
@@ -0,0 +1,32 @@
<?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.
*/

class Twig_Tests_Node_DeprecatedTest extends Twig_Test_NodeTestCase
{
public function testConstructor()
{
$expr = new Twig_Node_Expression_Constant('foo', 1);
$node = new Twig_Node_Deprecated($expr, 1);

$this->assertEquals($expr, $node->getNode('expr'));
}

public function getTests()
{
$tests = array();

$expr = new Twig_Node_Expression_Constant('foo', 1);
$node = new Twig_Node_Deprecated($expr, 1);
$tests[] = array($node, "// line 1\n@trigger_error(\"foo\", E_USER_DEPRECATED);");

return $tests;
}
}

0 comments on commit a0f1c66

Please sign in to comment.