浏览代码

Update Dirk

Add @isset(), @endisset, @empty() and @endempty
Visman 4 年之前
父节点
当前提交
0a8a57da98
共有 2 个文件被更改,包括 58 次插入13 次删除
  1. 5 0
      vendor/artoodetoo/dirk/README.md
  2. 53 13
      vendor/artoodetoo/dirk/src/Dirk.php

+ 5 - 0
vendor/artoodetoo/dirk/README.md

@@ -89,6 +89,11 @@ Conditionals
   * *@endif*
   * *@endif*
   * *@unless(condition)* - Starts an unless block
   * *@unless(condition)* - Starts an unless block
   * *@endunless*
   * *@endunless*
+  * *@isset(condition)* - Starts an isset block
+  * *@endisset*
+  * *@empty(condition)* - Starts an empty block
+  * *@endempty*
+
 
 
 Loops
 Loops
   * *@foreach($list as $key => $val)* - Starts a foreach block
   * *@foreach($list as $key => $val)* - Starts a foreach block

+ 53 - 13
vendor/artoodetoo/dirk/src/Dirk.php

@@ -166,12 +166,12 @@ class Dirk extends PhpEngine
     {
     {
         if (\preg_match('%^\(\s*(\!\s*)?(\$[\w>-]+\[(?:[\'"]\w+[\'"]|\d+)\])\s*\)$%', $expression, $matches)) {
         if (\preg_match('%^\(\s*(\!\s*)?(\$[\w>-]+\[(?:[\'"]\w+[\'"]|\d+)\])\s*\)$%', $expression, $matches)) {
             if (empty($matches[1])) {
             if (empty($matches[1])) {
-                return "<?php if(! empty{$expression}): ?>";
+                return "<?php if (! empty{$expression}): ?>";
             } else {
             } else {
-                return "<?php if(empty({$matches[2]})): ?>";
+                return "<?php if (empty({$matches[2]})): ?>";
             }
             }
         } else {
         } else {
-            return "<?php if{$expression}: ?>";
+            return "<?php if {$expression}: ?>";
         }
         }
     }
     }
 
 
@@ -183,7 +183,7 @@ class Dirk extends PhpEngine
      */
      */
     protected function compileElseif(string $expression): string
     protected function compileElseif(string $expression): string
     {
     {
-        return "<?php elseif{$expression}: ?>";
+        return "<?php elseif {$expression}: ?>";
     }
     }
 
 
     /**
     /**
@@ -206,6 +206,37 @@ class Dirk extends PhpEngine
         return "<?php endif; ?>";
         return "<?php endif; ?>";
     }
     }
 
 
+    /**
+     * Compile the isset statements
+     *
+     * @param  string  $expression
+     * @return string
+     */
+    protected function compileIsset(string $expression): string
+    {
+        return "<?php if (isset{$expression}): ?>";
+    }
+
+    /**
+     * Compile the end-isset statements
+     *
+     * @return string
+     */
+    protected function compileEndisset(): string
+    {
+        return "<?php endif; ?>";
+    }
+
+    /**
+     * Compile the end-empty statements
+     *
+     * @return string
+     */
+    protected function compileEndempty(): string
+    {
+        return "<?php endif; ?>";
+    }
+
     /**
     /**
      * Compile the unless statements
      * Compile the unless statements
      *
      *
@@ -214,7 +245,7 @@ class Dirk extends PhpEngine
      */
      */
     protected function compileUnless(string $expression): string
     protected function compileUnless(string $expression): string
     {
     {
-        return "<?php if(! $expression): ?>";
+        return "<?php if (! $expression): ?>";
     }
     }
 
 
     /**
     /**
@@ -235,7 +266,7 @@ class Dirk extends PhpEngine
      */
      */
     protected function compileFor(string $expression): string
     protected function compileFor(string $expression): string
     {
     {
-        return "<?php for{$expression}: ?>";
+        return "<?php for {$expression}: ?>";
     }
     }
 
 
     /**
     /**
@@ -256,7 +287,7 @@ class Dirk extends PhpEngine
      */
      */
     protected function compileForeach(string $expression): string
     protected function compileForeach(string $expression): string
     {
     {
-        return "<?php foreach{$expression}: ?>";
+        return "<?php foreach {$expression}: ?>";
     }
     }
 
 
     /**
     /**
@@ -281,21 +312,30 @@ class Dirk extends PhpEngine
         $this->emptyCounter++;
         $this->emptyCounter++;
 
 
         return "<?php \$__empty_{$this->emptyCounter} = true; "
         return "<?php \$__empty_{$this->emptyCounter} = true; "
-              . "foreach{$expression}: "
+              . "foreach {$expression}: "
               . "\$__empty_{$this->emptyCounter} = false;?>";
               . "\$__empty_{$this->emptyCounter} = false;?>";
     }
     }
 
 
     /**
     /**
      * Compile the end-forelse statements
      * Compile the end-forelse statements
+     * Compile the empty statements
      *
      *
+     * @param  string  $expression
      * @return string
      * @return string
      */
      */
-    protected function compileEmpty(): string
+    protected function compileEmpty(string $expression): string
     {
     {
-        $s = "<?php endforeach; if (\$__empty_{$this->emptyCounter}): ?>";
-        $this->emptyCounter--;
+        if (
+            isset($expression[0])
+            && '(' == $expression[0]
+        ) {
+            return "<?php if (empty{$expression}): ?>";
+        } else {
+            $s = "<?php endforeach; if (\$__empty_{$this->emptyCounter}): ?>";
+            $this->emptyCounter--;
 
 
-        return $s;
+            return $s;
+        }
     }
     }
 
 
     /**
     /**
@@ -316,7 +356,7 @@ class Dirk extends PhpEngine
      */
      */
     protected function compileWhile(string $expression): string
     protected function compileWhile(string $expression): string
     {
     {
-        return "<?php while{$expression}: ?>";
+        return "<?php while {$expression}: ?>";
     }
     }
 
 
     /**
     /**