added support for twig C extension
authorFabien Potencier <fabien.potencier@gmail.com>
Sat, 10 Sep 2011 09:35:39 +0000 (11:35 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Mon, 21 Nov 2011 08:36:32 +0000 (09:36 +0100)
CHANGELOG
doc/intro.rst
lib/Twig/Node/Expression/GetAttr.php
test/Twig/Tests/Node/Expression/GetAttrTest.php
test/Twig/Tests/Node/TestCase.php

index 421fa70..158cfdc 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,5 +1,6 @@
 * 1.4.0-RC2
 
+ * added the Twig C extension (replaces the implementation of Twig_Template::getAttribute() by a C function)
  * added negative timestamp support to the date filter
 
 * 1.4.0-RC1 (2011-11-20)
index 9cec815..951ec91 100644 (file)
@@ -32,8 +32,8 @@ Installation
 You have multiple ways to install Twig. If you are unsure what to do, go with
 the tarball.
 
-From the tarball release
-~~~~~~~~~~~~~~~~~~~~~~~~
+Installing grom the tarball release
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 1. Download the most recent tarball from the `download page`_
 2. Unpack the tarball
@@ -43,8 +43,8 @@ Installing the development version
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 1. Install Subversion or Git
-2. For Subversion: ``svn co http://svn.twig-project.org/trunk/ twig``, for Git:
-   ``git clone git://github.com/fabpot/Twig.git``
+2. For Git: ``git clone git://github.com/fabpot/Twig.git``
+3. For Subversion: ``svn co http://svn.twig-project.org/trunk/ twig``
 
 Installing the PEAR package
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -53,6 +53,30 @@ Installing the PEAR package
 2. ``pear channel-discover pear.twig-project.org``
 3. ``pear install twig/Twig`` (or ``pear install twig/Twig-beta``)
 
+Installing the C extension
+~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. versionadded:: 1.4
+    The C extension was added in Twig 1.4.
+
+Twig comes with a C extension that enhances the performance of the Twig
+runtime engine. You can install it like any other PHP extension:
+
+.. code-block:: bash
+
+    $ cd ext/twig
+    $ phpize
+    $ ./configure
+    $ make
+    $ make install
+
+Finally, enable the extension in your ``php.ini`` configuration file:
+
+    extension=twig.so
+
+And from now on, Twig will automatically compiles your templates to take
+advantage of the C extension.
+
 Basic API Usage
 ---------------
 
index 8d7184b..e6f5ba2 100644 (file)
@@ -18,7 +18,11 @@ class Twig_Node_Expression_GetAttr extends Twig_Node_Expression
 
     public function compile(Twig_Compiler $compiler)
     {
-        $compiler->raw('$this->getAttribute(');
+        if (function_exists('twig_template_get_attributes')) {
+            $compiler->raw('twig_template_get_attributes($this, ');
+        } else {
+            $compiler->raw('$this->getAttribute(');
+        }
 
         if ($this->getAttribute('ignore_strict_check')) {
             $this->getNode('node')->setAttribute('ignore_strict_check', true);
index 264618d..3893748 100644 (file)
@@ -49,10 +49,10 @@ class Twig_Tests_Node_Expression_GetAttrTest extends Twig_Tests_Node_TestCase
         $attr = new Twig_Node_Expression_Constant('bar', 0);
         $args = new Twig_Node();
         $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::ANY_CALL, 0);
-        $tests[] = array($node, sprintf('$this->getAttribute(%s, "bar")', $this->getVariableGetter('foo')));
+        $tests[] = array($node, sprintf('%s%s, "bar")', $this->getAttributeGetter(), $this->getVariableGetter('foo')));
 
         $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::ARRAY_CALL, 0);
-        $tests[] = array($node, sprintf('$this->getAttribute(%s, "bar", array(), "array")', $this->getVariableGetter('foo')));
+        $tests[] = array($node, sprintf('%s%s, "bar", array(), "array")', $this->getAttributeGetter(), $this->getVariableGetter('foo')));
 
 
         $args = new Twig_Node(array(
@@ -60,7 +60,7 @@ class Twig_Tests_Node_Expression_GetAttrTest extends Twig_Tests_Node_TestCase
             new Twig_Node_Expression_Constant('bar', 0),
         ));
         $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::METHOD_CALL, 0);
-        $tests[] = array($node, sprintf('$this->getAttribute(%s, "bar", array(%s, "bar", ), "method")', $this->getVariableGetter('foo'), $this->getVariableGetter('foo')));
+        $tests[] = array($node, sprintf('%s%s, "bar", array(%s, "bar", ), "method")', $this->getAttributeGetter(), $this->getVariableGetter('foo'), $this->getVariableGetter('foo')));
 
         return $tests;
     }
index ec4eee4..f142529 100644 (file)
@@ -46,4 +46,13 @@ abstract class Twig_Tests_Node_TestCase extends PHPUnit_Framework_TestCase
 
         return sprintf('$this->getContext($context, "%s")', $name);
     }
+
+    protected function getAttributeGetter()
+    {
+        if (function_exists('twig_template_get_attributes')) {
+            return 'twig_template_get_attributes($this, ';
+        }
+
+        return '$this->getAttribute(';
+    }
 }