|
@@ -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) ?>";
|
|
|
}
|