Skip to content

Commit

Permalink
moved to namespaced classes by default
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Feb 15, 2019
1 parent db751d6 commit 3632a46
Show file tree
Hide file tree
Showing 276 changed files with 2,595 additions and 2,595 deletions.
166 changes: 83 additions & 83 deletions doc/advanced.rst

Large diffs are not rendered by default.

90 changes: 45 additions & 45 deletions doc/advanced_legacy.rst
Expand Up @@ -115,7 +115,7 @@ Globals
A global variable is like any other template variable, except that it's
available in all templates and macros::

$twig = new Twig_Environment($loader);
$twig = new \Twig\Environment($loader);
$twig->addGlobal('text', new Text());

You can then use the ``text`` variable anywhere in a template:
Expand Down Expand Up @@ -176,9 +176,9 @@ expected output:
{# should displays Gjvt #}
Adding a filter is as simple as calling the ``addFilter()`` method on the
``Twig_Environment`` instance::
``\Twig\Environment`` instance::

$twig = new Twig_Environment($loader);
$twig = new \Twig\Environment($loader);
$twig->addFilter('rot13', new Twig_Filter_Function('str_rot13'));

The second argument of ``addFilter()`` is an instance of ``Twig_Filter``.
Expand Down Expand Up @@ -232,7 +232,7 @@ if you want access to the current environment instance in your filter, set the
Twig will then pass the current environment as the first argument to the
filter call::

function twig_compute_rot13(Twig_Environment $env, $string)
function twig_compute_rot13(\Twig\Environment $env, $string)
{
// get the current charset for instance
$charset = $env->getCharset();
Expand Down Expand Up @@ -311,15 +311,15 @@ compilation, the generated PHP code is roughly equivalent to:
<?php echo constant('DATE_W3C') ?>

Adding a function is similar to adding a filter. This can be done by calling the
``addFunction()`` method on the ``Twig_Environment`` instance::
``addFunction()`` method on the ``\Twig\Environment`` instance::

$twig = new Twig_Environment($loader);
$twig = new \Twig\Environment($loader);
$twig->addFunction('functionName', new Twig_Function_Function('someFunction'));

You can also expose extension methods as functions in your templates::

// $this is an object that implements Twig_ExtensionInterface.
$twig = new Twig_Environment($loader);
// $this is an object that implements \Twig\Extension\ExtensionInterface.
$twig = new \Twig\Environment($loader);
$twig->addFunction('otherFunction', new Twig_Function_Method($this, 'someMethod'));

Functions also support ``needs_environment`` and ``is_safe`` parameters.
Expand Down Expand Up @@ -395,26 +395,26 @@ Registering a new tag
~~~~~~~~~~~~~~~~~~~~~

Adding a tag is as simple as calling the ``addTokenParser`` method on the
``Twig_Environment`` instance::
``\Twig\Environment`` instance::

$twig = new Twig_Environment($loader);
$twig = new \Twig\Environment($loader);
$twig->addTokenParser(new Project_Set_TokenParser());

Defining a Token Parser
~~~~~~~~~~~~~~~~~~~~~~~

Now, let's see the actual code of this class::

class Project_Set_TokenParser extends Twig_TokenParser
class Project_Set_TokenParser extends \Twig\TokenParser\AbstractTokenParser
{
public function parse(Twig_Token $token)
public function parse(\Twig\Token $token)
{
$lineno = $token->getLine();
$name = $this->parser->getStream()->expect(Twig_Token::NAME_TYPE)->getValue();
$this->parser->getStream()->expect(Twig_Token::OPERATOR_TYPE, '=');
$name = $this->parser->getStream()->expect(\Twig\Token::NAME_TYPE)->getValue();
$this->parser->getStream()->expect(\Twig\Token::OPERATOR_TYPE, '=');
$value = $this->parser->getExpressionParser()->parseExpression();

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

return new Project_Set_Node($name, $value, $lineno, $this->getTag());
}
Expand All @@ -428,7 +428,7 @@ Now, let's see the actual code of this class::
The ``getTag()`` method must return the tag we want to parse, here ``set``.

The ``parse()`` method is invoked whenever the parser encounters a ``set``
tag. It should return a ``Twig_Node`` instance that represents the node (the
tag. It should return a ``\Twig\Node\Node`` instance that represents the node (the
``Project_Set_Node`` calls creating is explained in the next section).

The parsing process is simplified thanks to a bunch of methods you can call
Expand Down Expand Up @@ -461,14 +461,14 @@ Defining a Node

The ``Project_Set_Node`` class itself is rather simple::

class Project_Set_Node extends Twig_Node
class Project_Set_Node extends \Twig\Node\Node
{
public function __construct($name, Twig_Node_Expression $value, $lineno, $tag = null)
public function __construct($name, \Twig\Node\Expression\AbstractExpression $value, $lineno, $tag = null)
{
parent::__construct(['value' => $value], ['name' => $name], $lineno, $tag);
}

public function compile(Twig_Compiler $compiler)
public function compile(\Twig\Compiler $compiler)
{
$compiler
->addDebugInfo($this)
Expand All @@ -492,15 +492,15 @@ developer generate beautiful and readable PHP code:
* ``string()``: Writes a quoted string.

* ``repr()``: Writes a PHP representation of a given value (see
``Twig_Node_For`` for a usage example).
``\Twig\Node\ForNode`` for a usage example).

* ``addDebugInfo()``: Adds the line of the original template file related to
the current node as a comment.

* ``indent()``: Indents the generated code (see ``Twig_Node_Block`` for a
* ``indent()``: Indents the generated code (see ``\Twig\Node\BlockNode`` for a
usage example).

* ``outdent()``: Outdents the generated code (see ``Twig_Node_Block`` for a
* ``outdent()``: Outdents the generated code (see ``\Twig\Node\BlockNode`` for a
usage example).

.. _creating_extensions:
Expand Down Expand Up @@ -540,7 +540,7 @@ An extension is a class that implements the following interface::
*
* This is where you can load some file that contains filter functions for instance.
*/
function initRuntime(Twig_Environment $environment);
function initRuntime(\Twig\Environment $environment);

/**
* Returns the token parser instances to add to the existing list.
Expand All @@ -552,28 +552,28 @@ An extension is a class that implements the following interface::
/**
* Returns the node visitor instances to add to the existing list.
*
* @return Twig_NodeVisitorInterface[]
* @return \Twig\NodeVisitor\NodeVisitorInterface[]
*/
function getNodeVisitors();

/**
* Returns a list of filters to add to the existing list.
*
* @return Twig_SimpleFilter[]
* @return \Twig\TwigFilter[]
*/
function getFilters();

/**
* Returns a list of tests to add to the existing list.
*
* @return Twig_SimpleTest[]
* @return \Twig\TwigTest[]
*/
function getTests();

/**
* Returns a list of functions to add to the existing list.
*
* @return Twig_SimpleFunction[]
* @return \Twig\TwigFunction[]
*/
function getFunctions();

Expand All @@ -600,16 +600,16 @@ An extension is a class that implements the following interface::
}

To keep your extension class clean and lean, it can inherit from the built-in
``Twig_Extension`` class instead of implementing the whole interface. That
``\Twig\Extension\AbstractExtension`` class instead of implementing the whole interface. That
way, you just need to implement the ``getName()`` method as the
``Twig_Extension`` provides empty implementations for all other methods.
``\Twig\Extension\AbstractExtension`` provides empty implementations for all other methods.

The ``getName()`` method must return a unique identifier for your extension.

Now, with this information in mind, let's create the most basic extension
possible::

class Project_Twig_Extension extends Twig_Extension
class Project_Twig_Extension extends \Twig\Extension\AbstractExtension
{
public function getName()
{
Expand All @@ -628,7 +628,7 @@ extensions must be registered explicitly to be available in your templates.
You can register an extension by using the ``addExtension()`` method on your
main ``Environment`` object::

$twig = new Twig_Environment($loader);
$twig = new \Twig\Environment($loader);
$twig->addExtension(new Project_Twig_Extension());

Of course, you need to first load the extension file by either using
Expand All @@ -644,7 +644,7 @@ Globals
Global variables can be registered in an extension via the ``getGlobals()``
method::

class Project_Twig_Extension extends Twig_Extension
class Project_Twig_Extension extends \Twig\Extension\AbstractExtension
{
public function getGlobals()
{
Expand All @@ -662,7 +662,7 @@ Functions
Functions can be registered in an extension via the ``getFunctions()``
method::

class Project_Twig_Extension extends Twig_Extension
class Project_Twig_Extension extends \Twig\Extension\AbstractExtension
{
public function getFunctions()
{
Expand All @@ -681,7 +681,7 @@ To add a filter to an extension, you need to override the ``getFilters()``
method. This method must return an array of filters to add to the Twig
environment::

class Project_Twig_Extension extends Twig_Extension
class Project_Twig_Extension extends \Twig\Extension\AbstractExtension
{
public function getFilters()
{
Expand All @@ -705,7 +705,7 @@ $twig->addFilter('rot13', new Twig_Filter_Function('Project_Twig_Extension::rot1
You can also use ``Twig_Filter_Method`` instead of ``Twig_Filter_Function``
when defining a filter to use a method::

class Project_Twig_Extension extends Twig_Extension
class Project_Twig_Extension extends \Twig\Extension\AbstractExtension
{
public function getFilters()
{
Expand Down Expand Up @@ -737,7 +737,7 @@ If some default core filters do not suit your needs, you can easily override
them by creating your own extension. Just use the same names as the one you
want to override::

class MyCoreExtension extends Twig_Extension
class MyCoreExtension extends \Twig\Extension\AbstractExtension
{
public function getFilters()
{
Expand All @@ -762,7 +762,7 @@ Here, we override the ``date`` filter with a custom one. Using this extension
is as simple as registering the ``MyCoreExtension`` extension by calling the
``addExtension()`` method on the environment instance::

$twig = new Twig_Environment($loader);
$twig = new \Twig\Environment($loader);
$twig->addExtension(new MyCoreExtension());

Tags
Expand All @@ -772,7 +772,7 @@ Adding a tag in an extension can be done by overriding the
``getTokenParsers()`` method. This method must return an array of tags to add
to the Twig environment::

class Project_Twig_Extension extends Twig_Extension
class Project_Twig_Extension extends \Twig\Extension\AbstractExtension
{
public function getTokenParsers()
{
Expand All @@ -792,17 +792,17 @@ Operators
The ``getOperators()`` methods allows to add new operators. Here is how to add
``!``, ``||``, and ``&&`` operators::

class Project_Twig_Extension extends Twig_Extension
class Project_Twig_Extension extends \Twig\Extension\AbstractExtension
{
public function getOperators()
{
return [
[
'!' => ['precedence' => 50, 'class' => 'Twig_Node_Expression_Unary_Not'],
'!' => ['precedence' => 50, 'class' => '\Twig\Node\Expression\Unary\NotUnary'],
),
[
'||' => ['precedence' => 10, 'class' => 'Twig_Node_Expression_Binary_Or', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT],
'&&' => ['precedence' => 15, 'class' => 'Twig_Node_Expression_Binary_And', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT],
'||' => ['precedence' => 10, 'class' => '\Twig\Node\Expression\Binary\OrBinary', 'associativity' => \Twig\ExpressionParser::OPERATOR_LEFT],
'&&' => ['precedence' => 15, 'class' => '\Twig\Node\Expression\Binary\AndBinary', 'associativity' => \Twig\ExpressionParser::OPERATOR_LEFT],
],
];
}
Expand All @@ -815,7 +815,7 @@ Tests

The ``getTests()`` methods allows to add new test functions::

class Project_Twig_Extension extends Twig_Extension
class Project_Twig_Extension extends \Twig\Extension\AbstractExtension
{
public function getTests()
{
Expand Down Expand Up @@ -853,7 +853,7 @@ following file structure in your test directory::

The ``IntegrationTest.php`` file should look like this::

class Project_Tests_IntegrationTest extends Twig_Test_IntegrationTestCase
class Project_Tests_IntegrationTest extends \Twig\Test\IntegrationTestCase
{
public function getExtensions()
{
Expand All @@ -876,7 +876,7 @@ Node Tests
~~~~~~~~~~

Testing the node visitors can be complex, so extend your test cases from
``Twig_Test_NodeTestCase``. Examples can be found in the Twig repository
``\Twig\Test\NodeTestCase``. Examples can be found in the Twig repository
`tests/Twig/Node`_ directory.

.. _`spl_autoload_register()`: https://secure.php.net/spl_autoload_register
Expand Down

0 comments on commit 3632a46

Please sign in to comment.