Visman 4 år sedan
förälder
incheckning
064af21cc2

+ 5 - 5
composer.lock

@@ -12,16 +12,16 @@
             "source": {
                 "type": "git",
                 "url": "https://github.com/MioVisman/dirk.git",
-                "reference": "05cbba461274158699668c9e8075fb2d8fab5967"
+                "reference": "82c1ba51883c32c62e7eda121efcab5109acbbcc"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/MioVisman/dirk/zipball/05cbba461274158699668c9e8075fb2d8fab5967",
-                "reference": "05cbba461274158699668c9e8075fb2d8fab5967",
+                "url": "https://api.github.com/repos/MioVisman/dirk/zipball/82c1ba51883c32c62e7eda121efcab5109acbbcc",
+                "reference": "82c1ba51883c32c62e7eda121efcab5109acbbcc",
                 "shasum": ""
             },
             "require": {
-                "php": ">=5.4.0"
+                "php": ">=7.3.0"
             },
             "require-dev": {
                 "phpunit/phpunit": "4.0.*"
@@ -57,7 +57,7 @@
             "support": {
                 "source": "https://github.com/MioVisman/dirk/tree/visman"
             },
-            "time": "2019-11-28T13:14:33+00:00"
+            "time": "2020-10-16T13:32:17+00:00"
         },
         {
             "name": "miovisman/normemail",

+ 2 - 2
vendor/artoodetoo/dirk/composer.json

@@ -10,7 +10,7 @@
         }
     ],
     "require": {
-        "php": ">=5.4.0"
+        "php": ">=7.3.0"
     },
     "require-dev": {
         "phpunit/phpunit": "4.0.*"
@@ -25,4 +25,4 @@
             "R2\\Templating\\Tests": "tests"
         }
     }
-}
+}

+ 105 - 84
vendor/artoodetoo/dirk/src/Dirk.php

@@ -1,5 +1,7 @@
 <?php
 
+declare(strict_types=1);
+
 namespace R2\Templating;
 
 use R2\Templating\PhpEngine;
@@ -12,42 +14,48 @@ class Dirk extends PhpEngine
 
     public function __construct(array $config = [])
     {
-        $config = array_replace_recursive(
+        $config = \array_replace_recursive(
             [
                 'ext'   => '.blade.php',
                 'cache' => '.',
-                'echo'  => 'htmlspecialchars(%s, ENT_QUOTES, \'UTF-8\')',
+                'echo'  => '\\htmlspecialchars((string) %s, \\ENT_QUOTES, \'UTF-8\')',
             ],
             $config
         );
-        $this->cache      = isset($config['cache']) ? $config['cache'] : '.';
-        $this->echoFormat = isset($config['echo'])  ? $config['echo']  : '%s';
+        $this->cache      = $config['cache'] ?? '.';
+        $this->echoFormat = $config['echo']  ?? '%s';
         parent::__construct($config);
     }
 
-    protected $compilers = array(
+    protected $compilers = [
         'Statements',
         'Comments',
-        'Echos'
-    );
+        'Echos',
+    ];
 
     /**
      * Prepare file to include
      * @param  string $name
      * @return string
      */
-    protected function prepare($name)
+    protected function prepare(string $name): string
     {
-        $name = str_replace('.', '/', $name);
-        $tpl = $this->views . '/' . $name . $this->ext;
-        $php = $this->cache . '/' . md5($name) . '.php';
-        if (!file_exists($php) || filemtime($tpl) > filemtime($php)) {
-            $text = file_get_contents($tpl);
+        $name = \str_replace('.', '/', $name);
+        $tpl  = $this->views . '/' . $name . $this->ext;
+        $php  = $this->cache . '/' . \md5($name) . '.php';
+        if (
+            ! \file_exists($php)
+            || \filemtime($tpl) > \filemtime($php)
+        ) {
+            $text = \file_get_contents($tpl);
+
             foreach ($this->compilers as $type) {
                 $text = $this->{'compile' . $type}($text);
             }
-            file_put_contents($php, $text);
+
+            \file_put_contents($php, $text);
         }
+
         return $php;
     }
 
@@ -55,17 +63,18 @@ class Dirk extends PhpEngine
      * Compile Statements that start with "@"
      *
      * @param  string  $value
-     * @return mixed
+     * @return string
      */
-    protected function compileStatements($value)
+    protected function compileStatements(string $value): string
     {
-        return preg_replace_callback(
-            '/\B@(\w+)([ \t]*)(\( ( (?>[^()]+) | (?3) )* \))?/x',
+        return \preg_replace_callback(
+            '%[ \t]*+\B@(\w+)(?: [ \t]*( \( ( (?>[^()]+) | (?2) )* \) ) )?%x',
             function($match) {
-                if (method_exists($this, $method = 'compile' . ucfirst($match[1]))) {
-                    $match[0] = $this->$method(isset($match[3]) ? $match[3] : '');
+                if (\method_exists($this, $method = 'compile' . \ucfirst($match[1]))) {
+                    return $this->$method($match[2] ?? '');
+                } else {
+                    return $match[0];
                 }
-                return isset($match[3]) ? $match[0] : $match[0] . $match[2];
             },
             $value
         );
@@ -77,11 +86,9 @@ class Dirk extends PhpEngine
      * @param  string  $value
      * @return string
      */
-    protected function compileComments($value)
+    protected function compileComments(string $value): string
     {
-        $pattern = '/\{\{--((.|\s)*?)--\}\}/';
-
-        return preg_replace($pattern, '<?php /*$1*/ ?>', $value);
+        return \preg_replace('%\{\{--(.*?)--\}\}%s', '<?php /*$1*/ ?>', $value);
     }
 
     /**
@@ -90,44 +97,51 @@ class Dirk extends PhpEngine
      * @param  string  $value
      * @return string
      */
-    protected function compileEchos($value)
+    protected function compileEchos(string $value): string
     {
         // compile escaped echoes
-        $value = preg_replace_callback(
-            '/\{\{\{\s*(.+?)\s*\}\}\}(\r?\n)?/s',
+        $value = \preg_replace_callback(
+            '%\{\{\{\s*(.+?)\s*\}\}\}(\r?\n)?%s',
             function($matches) {
                 $whitespace = empty($matches[2]) ? '' : $matches[2] . $matches[2];
-                return '<?= htmlspecialchars('
-                    .$this->compileEchoDefaults($matches[1])
-                    .', ENT_QUOTES, \'UTF-8\') ?>'
-                    .$whitespace;
+
+                return '<?= \\htmlspecialchars('
+                    . $this->compileEchoDefaults($matches[1])
+                    . ', \\ENT_QUOTES, \'UTF-8\') ?>'
+                    . $whitespace;
             },
             $value
         );
 
         // compile not escaped echoes
-        $value = preg_replace_callback(
-            '/\{\!!\s*(.+?)\s*!!\}(\r?\n)?/s',
+        $value = \preg_replace_callback(
+            '%\{\!!\s*(.+?)\s*!!\}(\r?\n)?%s',
             function($matches) {
                 $whitespace = empty($matches[2]) ? '' : $matches[2] . $matches[2];
-                return '<?= '.$this->compileEchoDefaults($matches[1]).' ?>'.$whitespace;
+
+                return '<?= '
+                    . $this->compileEchoDefaults($matches[1])
+                    . ' ?>'
+                    . $whitespace;
             },
             $value
         );
 
         // compile regular echoes
-        $value = preg_replace_callback(
-            '/(@)?\{\{\s*(.+?)\s*\}\}(\r?\n)?/s',
+        $value = \preg_replace_callback(
+            '%(@)?\{\{\s*(.+?)\s*\}\}(\r?\n)?%s',
             function($matches) {
                 $whitespace = empty($matches[3]) ? '' : $matches[3] . $matches[3];
+
                 return $matches[1]
                     ? substr($matches[0], 1)
                     : '<?= '
-                      .sprintf($this->echoFormat, $this->compileEchoDefaults($matches[2]))
-                      .' ?>'.$whitespace;
+                        . sprintf($this->echoFormat, $this->compileEchoDefaults($matches[2]))
+                        . ' ?>' . $whitespace;
             },
             $value
         );
+
         return $value;
     }
 
@@ -137,9 +151,9 @@ class Dirk extends PhpEngine
      * @param  string  $value
      * @return string
      */
-    public function compileEchoDefaults($value)
+    public function compileEchoDefaults(string $value): string
     {
-        return preg_replace('/^(?=\$)(.+?)(?:\s+or\s+)(.+?)$/s', 'isset($1) ? $1 : $2', $value);
+        return \preg_replace('%^(?=\$)(.+?)(?:\s+or\s+)(.+?)$%s', '($1 ?? $2)', $value);
     }
 
     /**
@@ -148,9 +162,17 @@ class Dirk extends PhpEngine
      * @param  string  $expression
      * @return string
      */
-    protected function compileIf($expression)
+    protected function compileIf(string $expression): string
     {
-        return "<?php if{$expression}: ?>";
+        if (\preg_match('%^\(\s*(\!\s*)?(\$[\w>-]+\[(?:[\'"]\w+[\'"]|\d+)\])\s*\)$%', $expression, $matches)) {
+            if (empty($matches[1])) {
+                return "<?php if(! empty{$expression}): ?>";
+            } else {
+                return "<?php if(empty({$matches[2]})): ?>";
+            }
+        } else {
+            return "<?php if{$expression}: ?>";
+        }
     }
 
     /**
@@ -159,7 +181,7 @@ class Dirk extends PhpEngine
      * @param  string  $expression
      * @return string
      */
-    protected function compileElseif($expression)
+    protected function compileElseif(string $expression): string
     {
         return "<?php elseif{$expression}: ?>";
     }
@@ -167,10 +189,9 @@ class Dirk extends PhpEngine
     /**
      * Compile the else statements
      *
-     * @param  string  $expression
      * @return string
      */
-    protected function compileElse($expression)
+    protected function compileElse(): string
     {
         return "<?php else: ?>";
     }
@@ -178,10 +199,9 @@ class Dirk extends PhpEngine
     /**
      * Compile the end-if statements
      *
-     * @param  string  $expression
      * @return string
      */
-    protected function compileEndif($expression)
+    protected function compileEndif(): string
     {
         return "<?php endif; ?>";
     }
@@ -192,18 +212,17 @@ class Dirk extends PhpEngine
      * @param  string  $expression
      * @return string
      */
-    protected function compileUnless($expression)
+    protected function compileUnless(string $expression): string
     {
-        return "<?php if(!$expression): ?>";
+        return "<?php if(! $expression): ?>";
     }
 
     /**
      * Compile the end unless statements
      *
-     * @param  string  $expression
      * @return string
      */
-    protected function compileEndunless($expression)
+    protected function compileEndunless(): string
     {
         return "<?php endif; ?>";
     }
@@ -214,7 +233,7 @@ class Dirk extends PhpEngine
      * @param  string  $expression
      * @return string
      */
-    protected function compileFor($expression)
+    protected function compileFor(string $expression): string
     {
         return "<?php for{$expression}: ?>";
     }
@@ -222,10 +241,9 @@ class Dirk extends PhpEngine
     /**
      * Compile the end-for statements
      *
-     * @param  string  $expression
      * @return string
      */
-    protected function compileEndfor($expression)
+    protected function compileEndfor(): string
     {
         return "<?php endfor; ?>";
     }
@@ -236,7 +254,7 @@ class Dirk extends PhpEngine
      * @param  string  $expression
      * @return string
      */
-    protected function compileForeach($expression)
+    protected function compileForeach(string $expression): string
     {
         return "<?php foreach{$expression}: ?>";
     }
@@ -244,10 +262,9 @@ class Dirk extends PhpEngine
     /**
      * Compile the end-for-each statements
      *
-     * @param  string  $expression
      * @return string
      */
-    protected function compileEndforeach($expression)
+    protected function compileEndforeach(): string
     {
         return "<?php endforeach; ?>";
     }
@@ -259,12 +276,13 @@ class Dirk extends PhpEngine
      * @param  string  $expression
      * @return string
      */
-    protected function compileForelse($expression)
+    protected function compileForelse(string $expression): string
     {
         $this->emptyCounter++;
+
         return "<?php \$__empty_{$this->emptyCounter} = true; "
-              ."foreach{$expression}: "
-              ."\$__empty_{$this->emptyCounter} = false;?>";
+              . "foreach{$expression}: "
+              . "\$__empty_{$this->emptyCounter} = false;?>";
     }
 
     /**
@@ -272,10 +290,11 @@ class Dirk extends PhpEngine
      *
      * @return string
      */
-    protected function compileEmpty()
+    protected function compileEmpty(): string
     {
         $s = "<?php endforeach; if (\$__empty_{$this->emptyCounter}): ?>";
         $this->emptyCounter--;
+
         return $s;
     }
 
@@ -284,7 +303,7 @@ class Dirk extends PhpEngine
      *
      * @return string
      */
-    protected function compileEndforelse()
+    protected function compileEndforelse(): string
     {
         return "<?php endif; ?>";
     }
@@ -295,7 +314,7 @@ class Dirk extends PhpEngine
      * @param  string  $expression
      * @return string
      */
-    protected function compileWhile($expression)
+    protected function compileWhile(string $expression): string
     {
         return "<?php while{$expression}: ?>";
     }
@@ -303,10 +322,9 @@ class Dirk extends PhpEngine
     /**
      * Compile the end-while statements
      *
-     * @param  string  $expression
      * @return string
      */
-    protected function compileEndwhile($expression)
+    protected function compileEndwhile(): string
     {
         return "<?php endwhile; ?>";
     }
@@ -317,11 +335,15 @@ class Dirk extends PhpEngine
      * @param  string  $expression
      * @return string
      */
-    protected function compileExtends($expression)
+    protected function compileExtends(string $expression): string
     {
-        if (isset($expression[0]) && $expression[0] == '(') {
-            $expression = substr($expression, 1, -1);
+        if (
+            isset($expression[0])
+            && '(' == $expression[0]
+        ) {
+            $expression = \substr($expression, 1, -1);
         }
+
         return "<?php \$this->extend({$expression}) ?>";
     }
 
@@ -331,11 +353,15 @@ class Dirk extends PhpEngine
      * @param  string  $expression
      * @return string
      */
-    protected function compileInclude($expression)
+    protected function compileInclude(string $expression): string
     {
-        if (isset($expression[0]) && $expression[0] == '(') {
-            $expression = substr($expression, 1, -1);
+        if (
+            isset($expression[0])
+            && '(' == $expression[0]
+        ) {
+            $expression = \substr($expression, 1, -1);
         }
+
         return "<?php include \$this->prepare({$expression}) ?>";
     }
 
@@ -345,7 +371,7 @@ class Dirk extends PhpEngine
      * @param  string  $expression
      * @return string
      */
-    protected function compileYield($expression)
+    protected function compileYield(string $expression): string
     {
         return "<?= \$this->block{$expression} ?>";
     }
@@ -356,7 +382,7 @@ class Dirk extends PhpEngine
      * @param  string  $expression
      * @return string
      */
-    protected function compileSection($expression)
+    protected function compileSection(string $expression): string
     {
         return "<?php \$this->beginBlock{$expression} ?>";
     }
@@ -364,10 +390,9 @@ class Dirk extends PhpEngine
     /**
      * Compile the end-section statements
      *
-     * @param  string  $expression
      * @return string
      */
-    protected function compileEndsection($expression)
+    protected function compileEndsection(): string
     {
         return "<?php \$this->endBlock() ?>";
     }
@@ -375,10 +400,9 @@ class Dirk extends PhpEngine
     /**
      * Compile the show statements
      *
-     * @param  string  $expression
      * @return string
      */
-    protected function compileShow($expression)
+    protected function compileShow(): string
     {
         return "<?= \$this->block(\$this->endBlock()) ?>";
     }
@@ -386,10 +410,9 @@ class Dirk extends PhpEngine
     /**
      * Compile the append statements
      *
-     * @param  string  $expression
      * @return string
      */
-    protected function compileAppend($expression)
+    protected function compileAppend(): string
     {
         return "<?php \$this->endBlock() ?>";
     }
@@ -397,10 +420,9 @@ class Dirk extends PhpEngine
     /**
      * Compile the stop statements
      *
-     * @param  string  $expression
      * @return string
      */
-    protected function compileStop($expression)
+    protected function compileStop(): string
     {
         return "<?php \$this->endBlock() ?>";
     }
@@ -408,10 +430,9 @@ class Dirk extends PhpEngine
     /**
      * Compile the overwrite statements
      *
-     * @param  string  $expression
      * @return string
      */
-    protected function compileOverwrite($expression)
+    protected function compileOverwrite(): string
     {
         return "<?php \$this->endBlock(true) ?>";
     }

+ 50 - 33
vendor/artoodetoo/dirk/src/PhpEngine.php

@@ -1,5 +1,7 @@
 <?php
 
+declare(strict_types=1);
+
 namespace R2\Templating;
 
 class PhpEngine
@@ -17,12 +19,13 @@ class PhpEngine
      */
     public function __construct(array $config = [], array $composers = [])
     {
-        $this->views     = isset($config['views'])     ? $config['views']     : '.';
-        $this->ext       = isset($config['ext'])       ? $config['ext']       : '.php';
-        $this->separator = isset($config['separator']) ? $config['separator'] : '/';
-        $this->blocks = [];
+        $this->views      = $config['views']     ?? '.';
+        $this->ext        = $config['ext']       ?? '.php';
+        $this->separator  = $config['separator'] ?? '/';
+        $this->blocks     = [];
         $this->blockStack = [];
-        $this->composers = [];
+        $this->composers  = [];
+
         foreach ($composers as $name => $composer) {
             $this->composer($name, $composer);
         }
@@ -33,14 +36,16 @@ class PhpEngine
      * @param mixed $name     template name or array of names
      * @param mixed $composer data in the same meaning as for fetch() call, or callable returning such data
      */
-    public function composer($name, $composer)
+    public function composer($name, $composer): void
     {
-        if (is_array($name)) {
+        if (\is_array($name)) {
             foreach ($name as $n) {
                 $this->composer($n, $composer);
             }
         } else {
-            $p = '~^'.str_replace('\*', '[^'.$this->separator.']+', preg_quote($name, $this->separator.'~')).'$~';
+            $p = '~^'
+                . \str_replace('\*', '[^' . $this->separator . ']+', \preg_quote($name, $this->separator . '~'))
+                . '$~';
             $this->composers[$p][] = $composer;
         }
     }
@@ -50,12 +55,13 @@ class PhpEngine
      * @param  string $name
      * @return string
      */
-    protected function prepare($name)
+    protected function prepare(string $name): string
     {
-        if ($this->separator !== '/') {
-            $name = str_replace($this->separator, '/', $name);
+        if ('/' !== $this->separator) {
+            $name = \str_replace($this->separator, '/', $name);
         }
-        return $this->views.'/'.$name.$this->ext;
+
+        return $this->views . '/' . $name . $this->ext;
     }
 
 
@@ -64,7 +70,7 @@ class PhpEngine
      * @param string $name
      * @param array  $data
      */
-    public function render($name, array $data = [])
+    public function render(string $name, array $data = []): string
     {
         echo $this->fetch($name, $data);
     }
@@ -75,42 +81,48 @@ class PhpEngine
      * @param  array  $data
      * @return string
      */
-    public function fetch($name, array $data = [])
+    public function fetch(string $name, array $data = []): string
     {
         $this->templates[] = $name;
-        if (!empty($data)) {
-            extract($data);
+
+        if (! empty($data)) {
+            \extract($data);
         }
-        while ($_name = array_shift($this->templates)) {
+
+        while ($_name = \array_shift($this->templates)) {
             $this->beginBlock('content');
+
             foreach ($this->composers as $_cname => $_cdata) {
-                if (preg_match($_cname, $_name)) {
+                if (\preg_match($_cname, $_name)) {
                     foreach ($_cdata as $_citem) {
-                        extract((is_callable($_citem) ? $_citem($this) : $_citem) ?: []);
+                        \extract((\is_callable($_citem) ? $_citem($this) : $_citem) ?: []);
                     }
                 }
             }
+
             require($this->prepare($_name));
+
             $this->endBlock(true);
         }
+
         return $this->block('content');
     }
 
     /**
      * Is template file exists?
      * @param  string  $name
-     * @return Boolean
+     * @return bool
      */
-    public function exists($name)
+    public function exists(string $name): bool
     {
-        return file_exists($this->prepare($name));
+        return \file_exists($this->prepare($name));
     }
 
     /**
      * Define parent
      * @param string $name
      */
-    protected function extend($name)
+    protected function extend(string $name): void
     {
         $this->templates[] = $name;
     }
@@ -121,9 +133,9 @@ class PhpEngine
      * @param  string $default
      * @return string
      */
-    protected function block($name, $default = '')
+    protected function block(string $name, string $default = ''): string
     {
-        return array_key_exists($name, $this->blocks)
+        return \array_key_exists($name, $this->blocks)
             ? $this->blocks[$name]
             : $default;
     }
@@ -132,10 +144,10 @@ class PhpEngine
      * Block begins
      * @param string $name
      */
-    protected function beginBlock($name)
+    protected function beginBlock(string $name): void
     {
-        array_push($this->blockStack, $name);
-        ob_start();
+        \array_push($this->blockStack, $name);
+        \ob_start();
     }
 
     /**
@@ -143,14 +155,19 @@ class PhpEngine
      * @param boolean $overwrite
      * @return string
      */
-    protected function endBlock($overwrite = false)
+    protected function endBlock(bool $overwrite = false): string
     {
-        $name = array_pop($this->blockStack);
-        if ($overwrite || !array_key_exists($name, $this->blocks)) {
-            $this->blocks[$name] = ob_get_clean();
+        $name = \array_pop($this->blockStack);
+
+        if (
+            $overwrite
+            || ! \array_key_exists($name, $this->blocks)
+        ) {
+            $this->blocks[$name] = \ob_get_clean();
         } else {
-            $this->blocks[$name] .= ob_get_clean();
+            $this->blocks[$name] .= \ob_get_clean();
         }
+
         return $name;
     }
 }

+ 5 - 5
vendor/composer/installed.json

@@ -6,21 +6,21 @@
         "source": {
             "type": "git",
             "url": "https://github.com/MioVisman/dirk.git",
-            "reference": "05cbba461274158699668c9e8075fb2d8fab5967"
+            "reference": "82c1ba51883c32c62e7eda121efcab5109acbbcc"
         },
         "dist": {
             "type": "zip",
-            "url": "https://api.github.com/repos/MioVisman/dirk/zipball/05cbba461274158699668c9e8075fb2d8fab5967",
-            "reference": "05cbba461274158699668c9e8075fb2d8fab5967",
+            "url": "https://api.github.com/repos/MioVisman/dirk/zipball/82c1ba51883c32c62e7eda121efcab5109acbbcc",
+            "reference": "82c1ba51883c32c62e7eda121efcab5109acbbcc",
             "shasum": ""
         },
         "require": {
-            "php": ">=5.4.0"
+            "php": ">=7.3.0"
         },
         "require-dev": {
             "phpunit/phpunit": "4.0.*"
         },
-        "time": "2019-11-28T13:14:33+00:00",
+        "time": "2020-10-16T13:32:17+00:00",
         "type": "library",
         "installation-source": "dist",
         "autoload": {