forkbb/app/Core/View.php

99 lines
2.6 KiB
PHP
Raw Normal View History

2017-02-14 13:05:26 +00:00
<?php
2020-12-21 10:40:19 +00:00
/**
* This file is part of the ForkBB <https://github.com/forkbb>.
*
* @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
* @license The MIT License (MIT)
*/
2017-02-14 13:05:26 +00:00
2020-10-14 13:01:43 +00:00
declare(strict_types=1);
2017-02-14 13:05:26 +00:00
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
{
2020-10-16 13:40:22 +00:00
public function __construct (string $cache, string $views)
2017-02-14 13:05:26 +00:00
{
$config = [
'views' => $views,
'cache' => $cache,
2018-03-16 08:54:05 +00:00
'ext' => '.forkbb.php',
2020-10-16 13:40:22 +00:00
'echo' => '\\htmlspecialchars((string) %s, \\ENT_HTML5 | \\ENT_QUOTES | \\ENT_SUBSTITUTE, \'UTF-8\')',
2017-02-14 13:05:26 +00:00
'separator' => '/',
];
$this->compilers[] = 'Transformations';
parent::__construct($config);
}
/**
* Трансформация скомпилированного шаблона
*/
2020-10-16 13:40:22 +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
2020-10-15 13:02:24 +00:00
declare(strict_types=1);
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
}
}