forkbb/app/Core/View.php

123 lines
3.4 KiB
PHP
Raw Normal View History

2017-02-14 13:05:26 +00:00
<?php
namespace ForkBB\Core;
use R2\Templating\Dirk;
2017-11-03 13:06:22 +00:00
use ForkBB\Models\Page;
2017-02-14 13:05:26 +00:00
use RuntimeException;
class View extends Dirk
{
public function __construct ($cache, $views)
{
$config = [
'views' => $views,
'cache' => $cache,
2018-03-16 08:54:05 +00:00
'ext' => '.forkbb.php',
2018-03-08 12:39:54 +00:00
'echo' => '\\htmlspecialchars(%s, \\ENT_HTML5 | \\ENT_QUOTES | \\ENT_SUBSTITUTE, \'UTF-8\')',
2017-02-14 13:05:26 +00:00
'separator' => '/',
];
$this->compilers[] = 'Transformations';
parent::__construct($config);
}
2017-12-02 06:27:03 +00:00
/**
* Compile Statements that start with "@"
*/
protected function compileStatements(/* string */ $value) /* : mixed */
2017-12-02 06:27:03 +00:00
{
2018-03-04 05:30:07 +00:00
return \preg_replace_callback(
2017-12-09 16:23:35 +00:00
'/[ \t]*+\B@(\w+)(?: [ \t]*( \( ( (?>[^()]+) | (?2) )* \) ) )?/x',
2017-12-02 06:27:03 +00:00
function($match) {
2018-03-04 05:30:07 +00:00
if (\method_exists($this, $method = 'compile' . \ucfirst($match[1]))) {
2017-12-09 16:23:35 +00:00
return isset($match[2]) ? $this->$method($match[2]) : $this->$method('');
2017-12-02 06:27:03 +00:00
} else {
return $match[0];
}
},
$value
);
}
2017-02-14 13:05:26 +00:00
/**
* Трансформация скомпилированного шаблона
*/
protected function compileTransformations(/* string */ $value) /* string */
2017-02-14 13:05:26 +00:00
{
if ('<?xml ' === \substr($value, 0, 6)) {
$value = \str_replace(' \\ENT_HTML5 | \\ENT_QUOTES | \\ENT_SUBSTITUTE,', ' \\ENT_XML1,', $value);
}
2017-12-14 12:16:09 +00:00
$perfix = <<<'EOD'
<?php
use function \ForkBB\{__, num, dt, size};
2017-12-14 12:16:09 +00:00
?>
EOD;
2020-06-28 05:10:59 +00:00
if (false === \strpos($value, '<!-- inline -->')) {
2017-12-14 12:16:09 +00:00
return $perfix . $value;
2017-02-14 13:05:26 +00:00
}
2020-07-03 08:28:10 +00:00
2018-03-04 05:30:07 +00:00
return $perfix . \preg_replace_callback(
2017-04-09 14:32:49 +00:00
'%<!-- inline -->([^<]*(?:<(?!!-- endinline -->)[^<]*)*+)(?:<!-- endinline -->)?%',
2017-02-14 13:05:26 +00:00
function ($matches) {
2018-03-04 05:30:07 +00:00
return \preg_replace('%\h*\R\s*%', '', $matches[1]);
2017-02-14 13:05:26 +00:00
},
2017-09-14 13:10:47 +00:00
$value
);
2017-02-14 13:05:26 +00:00
}
2017-11-03 13:06:22 +00:00
/**
* Return result of templating
*/
public function rendering(Page $p): ?string
2017-02-14 13:05:26 +00:00
{
foreach ($p->httpHeaders as $catHeader) {
foreach ($catHeader as $header) {
\header($header[0], $header[1]);
}
2017-02-14 13:05:26 +00:00
}
2017-11-03 13:06:22 +00:00
if (null === $p->nameTpl) {
2017-02-14 13:05:26 +00:00
return null;
}
2017-11-03 13:06:22 +00:00
$p->prepare();
$this->templates[] = $p->nameTpl;
2018-03-04 05:30:07 +00:00
while ($_name = \array_shift($this->templates)) {
2017-11-03 13:06:22 +00:00
$this->beginBlock('content');
foreach ($this->composers as $_cname => $_cdata) {
2018-03-04 05:30:07 +00:00
if (\preg_match($_cname, $_name)) {
2017-11-03 13:06:22 +00:00
foreach ($_cdata as $_citem) {
2018-03-04 05:30:07 +00:00
\extract((\is_callable($_citem) ? $_citem($this) : $_citem) ?: []);
2017-11-03 13:06:22 +00:00
}
}
}
require($this->prepare($_name));
$this->endBlock(true);
}
2020-07-03 08:28:10 +00:00
2017-11-03 13:06:22 +00:00
return $this->block('content');
2017-02-14 13:05:26 +00:00
}
2018-03-16 06:47:55 +00:00
/**
* Compile the if statements
*/
protected function compileIf(/* string */ $expression) /* : string */
2018-03-16 06:47:55 +00:00
{
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 parent::compileIf($expression);
}
}
2017-02-14 13:05:26 +00:00
}