Latte\PhpWriter::quotingPass PHP Method

quotingPass() public method

Quotes symbols to strings.
public quotingPass ( MacroTokens $tokens ) : MacroTokens
$tokens MacroTokens
return MacroTokens
    public function quotingPass(MacroTokens $tokens)
    {
        $res = new MacroTokens();
        while ($tokens->nextToken()) {
            $res->append($tokens->isCurrent($tokens::T_SYMBOL) && (!$tokens->isPrev() || $tokens->isPrev(',', '(', '[', '=>', ':', '?', '.', '<', '>', '<=', '>=', '===', '!==', '==', '!=', '<>', '&&', '||', '=', 'and', 'or', 'xor', '??')) && (!$tokens->isNext() || $tokens->isNext(',', ';', ')', ']', '=>', ':', '?', '.', '<', '>', '<=', '>=', '===', '!==', '==', '!=', '<>', '&&', '||', 'and', 'or', 'xor', '??')) && !preg_match('#^[A-Z_][A-Z0-9_]{2,}$#', $tokens->currentValue()) ? "'" . $tokens->currentValue() . "'" : $tokens->currentToken());
        }
        return $res;
    }

Usage Example

コード例 #1
0
ファイル: CoreMacros.php プロジェクト: nette/latte
 /**
  * {var ...}
  * {default ...}
  */
 public function macroVar(MacroNode $node, PhpWriter $writer)
 {
     if ($node->modifiers) {
         throw new CompileException('Modifiers are not allowed in ' . $node->getNotation());
     }
     if ($node->args === '' && $node->parentNode && $node->parentNode->name === 'switch') {
         return '} else {';
     }
     $var = TRUE;
     $tokens = $writer->preprocess();
     $res = new Latte\MacroTokens();
     while ($tokens->nextToken()) {
         if ($var && $tokens->isCurrent($tokens::T_SYMBOL, $tokens::T_VARIABLE)) {
             if ($node->name === 'default') {
                 $res->append("'" . ltrim($tokens->currentValue(), '$') . "'");
             } else {
                 $res->append('$' . ltrim($tokens->currentValue(), '$'));
             }
             $var = NULL;
         } elseif ($tokens->isCurrent('=', '=>') && $tokens->depth === 0) {
             $res->append($node->name === 'default' ? '=>' : '=');
             $var = FALSE;
         } elseif ($tokens->isCurrent(',') && $tokens->depth === 0) {
             if ($var === NULL) {
                 $res->append($node->name === 'default' ? '=>NULL' : '=NULL');
             }
             $res->append($node->name === 'default' ? ',' : ';');
             $var = TRUE;
         } elseif ($var === NULL && $node->name === 'default' && !$tokens->isCurrent($tokens::T_WHITESPACE)) {
             throw new CompileException("Unexpected '{$tokens->currentValue()}' in {default {$node->args}}");
         } else {
             $res->append($tokens->currentToken());
         }
     }
     if ($var === NULL) {
         $res->append($node->name === 'default' ? '=>NULL' : '=NULL');
     }
     $out = $writer->quotingPass($res)->joinAll();
     return $node->name === 'default' ? "extract([{$out}], EXTR_SKIP)" : "{$out};";
 }