feat: Удаление магических методов в шаблонах

This commit is contained in:
origami11@yandex.ru 2025-12-02 19:43:10 +03:00
parent 18cc1cad01
commit 61b5bc1c0f
13 changed files with 148 additions and 111 deletions

View file

@ -24,18 +24,10 @@ class Composite extends View
}
function set(string $key, mixed $val): void {
if ($key == 'title') {
$this->setTitle($val);
}
$this->tal->set($key, $val);
}
function __set(string $key, mixed $val): void {
$this->tal->set($key, $val);
}
function execute(): string
{
function execute(): string {
$this->processChild();
return $this->tal->execute();
}

View file

@ -1,32 +0,0 @@
<?php
namespace ctiso\View;
class FakeTemplate extends \stdClass {
/** @var array */
public $_data = [];
/** @var string */
public $_name = '';
/**
* @param string $name
*/
function __construct($name) {
$this->_name = $name;
}
/**
* @param string $key
* @param mixed $value
*/
function __set($key, $value): void {
$this->_data[$key] = $value;
}
/**
* @return string
*/
function execute() {
return json_encode($this->_data) ?: '';
}
}

33
src/View/Json.php Normal file
View file

@ -0,0 +1,33 @@
<?php
namespace ctiso\View;
class Json implements Template {
/** @var array */
private $_data = [];
/** @var string */
private $_file;
/**
* @param string $_file
*/
function __construct($_file) {
$this->_file = $_file;
}
function getFile(): string {
return $this->_file;
}
/**
* @param string $key
* @param mixed $value
*/
function set($key, $value): void {
$this->_data[$key] = $value;
}
function execute(): string {
return json_encode($this->_data) ?: '';
}
}

View file

@ -5,7 +5,7 @@ namespace ctiso\View;
/**
* Шаблон для PHP
*/
class Plain extends \stdClass
class Plain implements Template
{
/** @var string */
protected $document;
@ -40,23 +40,13 @@ class Plain extends \stdClass
$this->values = array_merge($this->values, $list);
}
/**
* @param string $key ключ
* @param mixed $value значение
*/
public function __set($key, $value): void
{
$this->set($key, $value);
}
/**
* Выполнение шаблона
* @return string
*/
public function execute()
public function execute(): string
{
$result = $this->values;
return self::getTemplateContent ($this->document, $result);
return self::getTemplateContent($this->document, $result);
}
/**

27
src/View/TalView.php Normal file
View file

@ -0,0 +1,27 @@
<?php
namespace ctiso\View;
use PHPTAL;
class TalView implements Template {
private PHPTAL $tal;
function __construct(string $file)
{
$this->tal = new PHPTAL($file);
$this->tal->setPhpCodeDestination(PHPTAL_PHP_CODE_DESTINATION);
$this->tal->setEncoding(PHPTAL_DEFAULT_ENCODING);
$this->tal->setTemplateRepository(PHPTAL_TEMPLATE_REPOSITORY);
$this->tal->setOutputMode(PHPTAL::HTML5);
$this->tal->stripComments(true);
}
function set(string $key, mixed $val): void {
$this->tal->set($key, $val);
}
function execute(): string {
return $this->tal->execute();
}
}

10
src/View/Template.php Normal file
View file

@ -0,0 +1,10 @@
<?php
namespace ctiso\View;
interface Template {
/** Установка значений шаблона */
function set(string $key, mixed $value): void;
/** Преобразование шаблона в строку */
function execute(): string;
}

View file

@ -6,15 +6,10 @@ use ctiso\View\Composite;
class Top extends Composite
{
/**
* Общая строка заголовка
* @var int
*/
public $mid = 0;
/** @var int Счетчик для идентификторов */
private $mid = 0;
/** @var array */
public $require = [];
/** @var array */
public $deps = [];
/** @var \ctiso\Registry */
public $config;
@ -57,7 +52,6 @@ class Top extends Composite
$this->set('stylesheet', array_unique($this->getStyleSheet()));
$this->require = ['admin' => 'ma'];
$this->deps = [];
$startup = [];
foreach ($this->_section as $s) {
@ -83,23 +77,9 @@ class Top extends Composite
foreach ($s->_values as $key => $v) {
$script .= $value . "." . $key . " = " . json_encode($v /*, JSON_PRETTY_PRINT*/) . ";\n";
}
$init = [];
/** @var View $item */
foreach ($s->_section as $key => $item) {
if ($item->codeGenerator !== null) {
// функцию которая вычисляет а не результат
$part = call_user_func($item->codeGenerator, $this, $key, $value);
$init[] = $part;
}
}
$script .= $value . ".execute('" . $s->module_action . "', " . json_encode($init) . ");\n";
$startup[] = $script;
}
$this->set('startup', implode("", $startup) . $this->getScriptStartup());
$this->set('require', implode(",", array_map(function ($x) {
return "'$x'"; }, array_keys($this->require))));

View file

@ -3,7 +3,7 @@
namespace ctiso\View;
use Exception;
class View extends \stdClass
class View implements Template
{
/** @var array<View|string> Вложенные шаблоны */
protected array $_section = [];
@ -32,7 +32,7 @@ class View extends \stdClass
/** @var array<string, string|array<string, string>> */
public array $alias = [];
// public $codeGenerator = null;
/** @var View|null */
public $parent_view = null;
@ -130,8 +130,7 @@ class View extends \stdClass
}
*/
/*abstract*/ public function set(string $key, mixed $value): void
public function set(string $key, mixed $value): void
{
}