瀏覽代碼

Better error message.

Justin Hileman 3 年之前
父節點
當前提交
454b4c63e9
共有 2 個文件被更改,包括 30 次插入1 次删除
  1. 1 1
      src/Mustache/Parser.php
  2. 29 0
      src/Mustache/Tokenizer.php

+ 1 - 1
src/Mustache/Parser.php

@@ -334,7 +334,7 @@ class Mustache_Parser
         $msg = sprintf(
             'Invalid dynamic name: %s in %s tag',
             $token[Mustache_Tokenizer::NAME],
-            $token[Mustache_Tokenizer::TYPE]
+            Mustache_Tokenizer::getTagName($token[Mustache_Tokenizer::TYPE])
         );
 
         throw new Mustache_Exception_SyntaxException($msg, $token);

+ 29 - 0
src/Mustache/Tokenizer.php

@@ -53,6 +53,22 @@ class Mustache_Tokenizer
         self::T_BLOCK_VAR    => true,
     );
 
+    private static $tagNames = array(
+        self::T_SECTION      => 'section',
+        self::T_INVERTED     => 'inverted section',
+        self::T_END_SECTION  => 'section end',
+        self::T_COMMENT      => 'comment',
+        self::T_PARTIAL      => 'partial',
+        self::T_PARENT       => 'parent',
+        self::T_DELIM_CHANGE => 'set delimiter',
+        self::T_ESCAPED      => 'variable',
+        self::T_UNESCAPED    => 'unescaped variable',
+        self::T_UNESCAPED_2  => 'unescaped variable',
+        self::T_PRAGMA       => 'pragma',
+        self::T_BLOCK_VAR    => 'block variable',
+        self::T_BLOCK_ARG    => 'block variable',
+    );
+
     // Token properties
     const TYPE    = 'type';
     const NAME    = 'name';
@@ -358,6 +374,7 @@ class Mustache_Tokenizer
         return $end + $this->ctagLen - 1;
     }
 
+
     private function throwUnclosedTagException()
     {
         $name = trim($this->buffer);
@@ -376,4 +393,16 @@ class Mustache_Tokenizer
             self::INDEX => $this->seenTag - $this->otagLen,
         ));
     }
+
+    /**
+     * Get the human readable name for a tag type.
+     *
+     * @param string $tagType One of the tokenizer T_* constants
+     *
+     * @return string
+     */
+    static function getTagName($tagType)
+    {
+        return isset(self::$tagNames[$tagType]) ? self::$tagNames[$tagType] : 'unknown';
+    }
 }