fixed Twig_Token::getTypeAsString (closes #53)
authorfabien <fabien@93ef8e89-cb99-4229-a87c-7fa0fa45744b>
Thu, 14 Jan 2010 12:35:14 +0000 (12:35 +0000)
committerfabien <fabien@93ef8e89-cb99-4229-a87c-7fa0fa45744b>
Thu, 14 Jan 2010 12:35:14 +0000 (12:35 +0000)
git-svn-id: http://svn.twig-project.org/trunk@226 93ef8e89-cb99-4229-a87c-7fa0fa45744b

lib/Twig/Environment.php
lib/Twig/Extension/Core.php
lib/Twig/Node/Expression/Name.php
lib/Twig/Token.php

index c691d97..5576df8 100644 (file)
@@ -16,6 +16,7 @@ class Twig_Environment
   protected $charset;
   protected $loader;
   protected $trimBlocks;
+  protected $strictMode;
   protected $debug;
   protected $autoReload;
   protected $cache;
@@ -56,6 +57,11 @@ class Twig_Environment
    *  * auto_reload: Whether to reload the template is the original source changed.
    *    If you don't provide the auto_reload option, it will be
    *    determined automatically base on the debug value.
+   *
+   *  * strict_mode: Whether to enable the strict mode or not. If enabled,
+   *    everything must be explicit. For instance, all variables must be
+   *    declared at the top of the template, you must pass explicitely the variables
+   *    when including another template, and so on. If not, a StrictError is thrown.
    */
   public function __construct(Twig_LoaderInterface $loader = null, $options = array())
   {
@@ -64,6 +70,7 @@ class Twig_Environment
       $this->setLoader($loader);
     }
 
+    $this->strictMode         = isset($options['strict_mode']) ? (bool) $options['strict_mode'] : false;
     $this->debug              = isset($options['debug']) ? (bool) $options['debug'] : false;
     $this->trimBlocks         = isset($options['trim_blocks']) ? (bool) $options['trim_blocks'] : false;
     $this->charset            = isset($options['charset']) ? $options['charset'] : 'UTF-8';
@@ -84,6 +91,21 @@ class Twig_Environment
     $this->baseTemplateClass = $class;
   }
 
+  public function enableStrictMode()
+  {
+    $this->strictMode = true;
+  }
+
+  public function disableStrictMode()
+  {
+    $this->strictMode = false;
+  }
+
+  public function isStrictMode()
+  {
+    return $this->strictMode;
+  }
+
   public function enableDebug()
   {
     $this->debug = true;
index 3ae0905..d6dbd4c 100644 (file)
@@ -30,6 +30,7 @@ class Twig_Extension_Core extends Twig_Extension
       new Twig_TokenParser_Import(),
       new Twig_TokenParser_Set(),
       new Twig_TokenParser_Debug(),
+      new Twig_TokenParser_Use(),
     );
   }
 
@@ -40,7 +41,10 @@ class Twig_Extension_Core extends Twig_Extension
    */
   public function getNodeVisitors()
   {
-    return array(new Twig_NodeVisitor_Filter());
+    return array(
+      new Twig_NodeVisitor_Filter(),
+      new Twig_NodeVisitor_StrictMode()
+    );
   }
 
   /**
index 439a4b3..fae4c9b 100644 (file)
@@ -12,6 +12,7 @@
 class Twig_Node_Expression_Name extends Twig_Node_Expression
 {
   protected $name;
+  protected $declared = false;
 
   public function __construct($name, $lineno)
   {
@@ -26,11 +27,23 @@ class Twig_Node_Expression_Name extends Twig_Node_Expression
 
   public function compile($compiler)
   {
-    $compiler->raw(sprintf('(isset($context[\'%s\']) ? $context[\'%s\'] : null)', $this->name, $this->name));
+    if ($this->declared)
+    {
+      $compiler->raw(sprintf('$context[\'%s\']', $this->name));
+    }
+    else
+    {
+      $compiler->raw(sprintf('(isset($context[\'%s\']) ? $context[\'%s\'] : null)', $this->name, $this->name));
+    }
   }
 
   public function getName()
   {
     return $this->name;
   }
+
+  public function setDeclared()
+  {
+    $this->declared = true;
+  }
 }
index 4c1c907..cca6828 100644 (file)
@@ -84,26 +84,38 @@ class Twig_Token
   {
     switch ($type)
     {
-      case 0:
-        return 'TEXT_TYPE';
-      case -1:
-        return 'EOF_TYPE';
-      case 1:
-        return 'BLOCK_START_TYPE';
-      case 2:
-        return 'VAR_START_TYPE';
-      case 3:
-        return 'BLOCK_END_TYPE';
-      case 4:
-        return 'VAR_END_TYPE';
-      case 5:
-        return 'NAME_TYPE';
-      case 6:
-        return 'NUMBER_TYPE';
-      case 7:
-        return 'STRING_TYPE';
-      case 8:
-        return 'OPERATOR_TYPE';
+      case self::EOF_TYPE:
+        $name = 'EOF_TYPE';
+        break;
+      case self::TEXT_TYPE:
+        $name = 'TEXT_TYPE';
+        break;
+      case self::BLOCK_START_TYPE:
+        $name = 'BLOCK_START_TYPE';
+        break;
+      case self::VAR_START_TYPE:
+        $name = 'VAR_START_TYPE';
+        break;
+      case selg::BLOCK_END_TYPE:
+        $name = 'BLOCK_END_TYPE';
+        break;
+      case self::VAR_END_TYPE:
+        $name = 'VAR_END_TYPE';
+        break;
+      case self::NAME_TYPE:
+        $name = 'NAME_TYPE';
+        break;
+      case self::NUMBER_TYPE:
+        $name = 'NUMBER_TYPE';
+        break;
+      case self::STRING_TYPE:
+        $name = 'STRING_TYPE';
+        break;
+      case self::OPERATOR_TYPE:
+        $name = 'OPERATOR_TYPE';
+        break;
+      default:
+        throw new InvalidArgumentException(sprintf('Token of type %s does not exist.', $type));
     }
 
     return $short ? $name : 'Twig_Token::'.$name;