Change Model

This commit is contained in:
Visman 2019-12-27 18:20:24 +07:00
parent 4205a69c3e
commit fd292bf4c7
10 changed files with 205 additions and 116 deletions

View file

@ -26,7 +26,7 @@ class Model extends ParentModel
public function __construct(array $options, Container $container)
{
parent::__construct($container);
$this->a = $options + [
$options = $options + [
'prefix' => '',
'domain' => '',
'path' => '',
@ -35,6 +35,7 @@ class Model extends ParentModel
'key1' => 'key1',
'key2' => 'key2',
];
$this->setAttrs($options);
$this->init();
$this->noSet = true;
}

View file

@ -14,11 +14,11 @@ class Model extends ParentModel
public function init()
{
if ($this->c->Cache->has('db_map')) {
$this->a = $this->c->Cache->get('db_map');
$this->setAttrs($this->c->Cache->get('db_map'));
} else {
$map = $this->c->DB->getMap();
$this->c->Cache->set('db_map', $map);
$this->a = $map;
$this->setAttrs($map);
}
return $this;
}

View file

@ -12,16 +12,17 @@ class DataModel extends Model
* Массив флагов измененных свойств модели
* @var array
*/
protected $modified = [];
protected $zModFlags = [];
/**
* Массив состояний отслеживания изменений в свойствах модели
* @var array
*/
protected $track = [];
protected $zTrackFlags = [];
/**
* Устанавливает значения для свойств
* Сбрасывает вычисленные свойства
* Флаги модификации свойст сброшены
*
* @param array $attrs
@ -30,11 +31,10 @@ class DataModel extends Model
*/
public function setAttrs(array $attrs)
{
$this->a = $attrs; //????
$this->aCalc = [];
$this->modified = [];
$this->track = [];
return $this;
$this->zModFlags = [];
$this->zTrackFlags = [];
return parent::setAttrs($attrs);
}
/**
@ -52,7 +52,7 @@ class DataModel extends Model
$this->__set($name, $value);
if (! $setFlags) {
unset($this->modified[$name]);
unset($this->zModFlags[$name]);
}
}
@ -66,7 +66,7 @@ class DataModel extends Model
*/
public function getAttrs()
{
return $this->a; //????
return $this->zAttrs; //????
}
/**
@ -76,7 +76,7 @@ class DataModel extends Model
*/
public function getModified()
{
return \array_keys($this->modified);
return \array_keys($this->zModFlags);
}
/**
@ -84,17 +84,17 @@ class DataModel extends Model
*/
public function resModified()
{
$this->modified = [];
$this->track = [];
$this->zModFlags = [];
$this->zTrackFlags = [];
}
/**
* Устанавливает значение для свойства
*
* @param string $name
* @param mixed $val
* @param mixed $value
*/
public function __set($name, $val)
public function __set($name, $value)
{
// без отслеживания
if (\strpos($name, '__') === 0) {
@ -103,30 +103,30 @@ class DataModel extends Model
// с отслеживанием
} else {
$track = false;
if (\array_key_exists($name, $this->a)) {
if (\array_key_exists($name, $this->zAttrs)) {
$track = true;
$old = $this->a[$name];
$old = $this->zAttrs[$name];
// fix
if (\is_int($val) && \is_numeric($old) && \is_int(0 + $old)) {
if (\is_int($value) && \is_numeric($old) && \is_int(0 + $old)) {
$old = (int) $old;
}
}
}
$this->track[$name] = $track;
$this->zTrackFlags[$name] = $track;
parent::__set($name, $val);
parent::__set($name, $value);
unset($this->track[$name]);
unset($this->zTrackFlags[$name]);
if (null === $track) {
return;
}
if ((! $track && \array_key_exists($name, $this->a))
|| ($track && $old !== $this->a[$name])
if ((! $track && \array_key_exists($name, $this->zAttrs))
|| ($track && $old !== $this->zAttrs[$name])
) {
$this->modified[$name] = true;
$this->zModFlags[$name] = true;
}
}
@ -141,8 +141,7 @@ class DataModel extends Model
{
// без вычисления
if (\strpos($name, '__') === 0) {
$name = \substr($name, 2);
return isset($this->a[$name]) ? $this->a[$name] : null;
return $this->getAttr(\substr($name, 2));
// с вычислениями
} else {
return parent::__get($name);

View file

@ -58,11 +58,14 @@ class Model extends DataModel
protected function getsubforums()
{
$sub = [];
if (! empty($this->a['subforums'])) {
foreach ($this->a['subforums'] as $id) {
$attr = $this->getAttr('subforums');
if (\is_array($attr)) {
foreach ($attr as $id) {
$sub[$id] = $this->c->forums->get($id);
}
}
return $sub;
}
@ -74,11 +77,14 @@ class Model extends DataModel
protected function getdescendants()
{
$all = [];
if (! empty($this->a['descendants'])) {
foreach ($this->a['descendants'] as $id) {
$attr = $this->getAttr('descendants');
if (\is_array($attr)) {
foreach ($attr as $id) {
$all[$id] = $this->c->forums->get($id);
}
}
return $all;
}
@ -154,13 +160,13 @@ class Model extends DataModel
*/
protected function getmoderators()
{
if (empty($this->a['moderators'])) {
$attr = $this->getAttr('moderators');
if (empty($attr) || ! \is_array($attr)) {
return [];
}
if ($this->c->user->g_view_users == '1') {
$arr = $this->a['moderators'];
foreach($arr as $id => &$cur) {
foreach($attr as $id => &$cur) {
$cur = [
$this->c->Router->link('User', [
'id' => $id,
@ -170,10 +176,9 @@ class Model extends DataModel
];
}
unset($cur);
return $arr;
} else {
return $this->a['moderators'];
}
return $attr;
}
/**
@ -185,16 +190,19 @@ class Model extends DataModel
*/
public function modAdd(...$users)
{
$moderators = empty($this->a['moderators']) ? [] : $this->a['moderators'];
$attr = $this->getAttr('moderators');
if (empty($attr) || ! \is_array($attr)) {
$attr = [];
}
foreach ($users as $user) {
if (! $user instanceof User) {
throw new InvalidArgumentException('Expected User');
}
$moderators[$user->id] = $user->username;
$attr[$user->id] = $user->username;
}
$this->moderators = $moderators;
$this->moderators = $attr;
}
/**
@ -206,20 +214,19 @@ class Model extends DataModel
*/
public function modDelete(...$users)
{
if (empty($this->a['moderators'])) {
$attr = $this->getAttr('moderators');
if (empty($attr) || ! \is_array($attr)) {
return;
}
$moderators = $this->a['moderators'];
foreach ($users as $user) {
if (! $user instanceof User) {
throw new InvalidArgumentException('Expected User');
}
unset($moderators[$user->id]);
unset($attr[$user->id]);
}
$this->moderators = $moderators;
$this->moderators = $attr;
}
/**
@ -229,7 +236,9 @@ class Model extends DataModel
*/
protected function gettree()
{
if (empty($this->a['tree'])) { //????
$attr = $this->getAttr('tree');
if (empty($attr)) { //????
$numT = (int) $this->num_topics;
$numP = (int) $this->num_posts;
$time = (int) $this->last_post;
@ -248,7 +257,7 @@ class Model extends DataModel
$topic = $children->last_topic;
}
}
$this->a['tree'] = $this->c->forums->create([
$attr = $this->c->forums->create([
'num_topics' => $numT,
'num_posts' => $numP,
'last_post' => $time,
@ -257,8 +266,10 @@ class Model extends DataModel
'last_topic' => $topic,
'newMessages' => $fnew,
]);
$this->setAttr('tree', $attr);
}
return $this->a['tree'];
return $attr;
}
/**

View file

@ -17,19 +17,19 @@ class Model
* Данные модели
* @var array
*/
protected $a = [];
protected $zAttrs = [];
/**
* Вычисленные данные модели
* @var array
*/
protected $aCalc = [];
protected $zAttrsCalc = [];
/**
* Зависимости свойств
* @var array
*/
protected $dependProp = [];
protected $zDepend = [];
/**
* Конструктор
@ -50,8 +50,8 @@ class Model
*/
public function __isset($name)
{
return \array_key_exists($name, $this->a)
|| \array_key_exists($name, $this->aCalc)
return \array_key_exists($name, $this->zAttrs)
|| \array_key_exists($name, $this->zAttrsCalc)
|| \method_exists($this, 'get' . $name);
}
@ -62,11 +62,10 @@ class Model
*/
public function __unset($name)
{
unset($this->a[$name]); //????
unset($this->aCalc[$name]); //????
unset($this->zAttrs[$name], $this->zAttrsCalc[$name]); //????
if (isset($this->dependProp[$name])) {
$this->aCalc = \array_diff_key($this->aCalc, \array_flip($this->dependProp[$name]));
if (isset($this->zDepend[$name])) {
$this->zAttrsCalc = \array_diff_key($this->zAttrsCalc, \array_flip($this->zDepend[$name]));
}
}
@ -74,23 +73,52 @@ class Model
* Устанавливает значение для свойства
*
* @param string $name
* @param mixed $val
* @param mixed $value
*/
public function __set($name, $val)
public function __set($name, $value)
{
unset($this->aCalc[$name]);
if (isset($this->dependProp[$name])) {
$this->aCalc = \array_diff_key($this->aCalc, \array_flip($this->dependProp[$name]));
}
$this->__unset($name);
if (\method_exists($this, $method = 'set' . $name)) {
$this->$method($val);
$this->$method($value);
} else {
$this->a[$name] = $val;
$this->zAttrs[$name] = $value;
}
}
/**
* Устанавливает значение для свойства
* Без вычислений, но со сбросом зависимых свойст и вычисленного значения
*
* @param string $name
* @param mixed $value
*
* @return Model
*/
public function setAttr($name, $value)
{
$this->__unset($name);
$this->zAttrs[$name] = $value;
return $this;
}
/**
* Устанавливает значения для свойств
* Сбрасывает вычисленные свойства
*
* @param array $attrs
*
* @return Model
*/
public function setAttrs(array $attrs)
{
$this->zAttrs = $attrs; //????
$this->zAttrsCalc = [];
return $this;
}
/**
* Возвращает значение свойства
*
@ -100,15 +128,29 @@ class Model
*/
public function __get($name)
{
if (\array_key_exists($name, $this->aCalc)) {
return $this->aCalc[$name];
if (\array_key_exists($name, $this->zAttrsCalc)) {
return $this->zAttrsCalc[$name];
} elseif (\method_exists($this, $method = 'get' . $name)) {
return $this->aCalc[$name] = $this->$method();
return $this->zAttrsCalc[$name] = $this->$method();
} else {
return isset($this->a[$name]) ? $this->a[$name] : null;
return isset($this->zAttrs[$name]) ? $this->zAttrs[$name] : null;
}
}
/**
* Возвращает значение свойства
* Без вычислений
*
* @param string $name
* @param mixed $default
*
* @return mixed
*/
public function getAttr($name, $default = null)
{
return \array_key_exists($name, $this->zAttrs) ? $this->zAttrs[$name] : $default;
}
/**
* Выполняет подгружаемый метод при его наличии
*

View file

@ -144,7 +144,7 @@ abstract class Page extends Model
protected function maintenance()
{
if ($this->c->config->o_maintenance == '1' && $this->user->isAdmin) {
$this->a['fIswev']['w']['maintenance'] = \ForkBB\__('Maintenance mode enabled', $this->c->Router->link('AdminMaintenance'));
$this->fIswev = ['w', \ForkBB\__('Maintenance mode enabled', $this->c->Router->link('AdminMaintenance'))];
}
}
@ -173,15 +173,20 @@ abstract class Page extends Model
*/
protected function getpageHeaders()
{
$headers = [['link', 'rel="stylesheet" type="text/css" href="' . $this->c->PUBLIC_URL . '/style/' . $this->user->style . '/style.css' . '"']];
$headers = [
['link', 'rel="stylesheet" type="text/css" href="' . $this->c->PUBLIC_URL . '/style/' . $this->user->style . '/style.css' . '"'],
];
if ($this->canonical) {
$headers[] = ['link', 'rel="canonical" href="' . $this->canonical . '"'];
}
if ($this->robots) {
$headers[] = ['meta', 'name="robots" content="' . $this->robots . '"'];
}
if (isset($this->a['pageHeaders']['style'])) {
foreach ($this->a['pageHeaders']['style'] as $style) {
$ph = $this->getAttr('pageHeaders', []);
if (isset($ph['style'])) {
foreach ($ph['style'] as $style) {
$headers[] = ['style', $style];
}
}
@ -192,13 +197,15 @@ abstract class Page extends Model
* Добавляет стиль на страницу
*
* @param string $name
* @param string $val
* @param string $value
*
* @return Page
*/
public function addStyle($name, $val)
public function addStyle($name, $value)
{
$this->a['pageHeaders']['style'][$name] = $val;
$attr = $this->getAttr('pageHeaders', []);
$attr['style'][$name] = $value;
$this->setAttr('pageHeaders', $attr);
return $this;
}
@ -222,7 +229,9 @@ abstract class Page extends Model
} else {
$key .= ':';
}
$this->a['httpHeaders'][] = ["{$key} {$value}", $replace];
$attr = $this->getAttr('httpHeaders', []);
$attr[] = ["{$key} {$value}", $replace];
$this->setAttr('httpHeaders', $attr);
return $this;
}
@ -243,7 +252,7 @@ abstract class Page extends Model
->header('Last-Modified', $now)
->header('Expires', $now);
return $this->a['httpHeaders'];
return $this->getAttr('httpHeaders', []);
}
/**
@ -271,33 +280,32 @@ abstract class Page extends Model
* Дописывает в массив титула страницы новый элемент
* $this->titles = ...
*
* @param string $val
* @param string $value
*/
public function settitles($val)
public function settitles($value)
{
if (empty($this->a['titles'])) {
$this->a['titles'] = [$val];
} else {
$this->a['titles'][] = $val;
}
$attr = $this->getAttr('titles', []);
$attr[] = $value;
$this->setAttr('titles', $attr);
}
/**
* Добавление новой ошибки
* $this->fIswev = ...
*
* @param array $val
* @param array $value
*/
public function setfIswev(array $val)
public function setfIswev(array $value)
{
if (empty($this->a['fIswev'])) {
$this->a['fIswev'] = [];
}
if (isset($val[0], $val[1]) && \is_string($val[0]) && \is_string($val[1])) {
$this->a['fIswev'][$val[0]][] = $val[1];
$attr = $this->getAttr('fIswev', []);
if (isset($value[0], $value[1]) && \is_string($value[0]) && \is_string($value[1])) {
$attr[$value[0]][] = $value[1];
} else {
$this->a['fIswev'] = \array_merge_recursive((array) $this->a['fIswev'], $val);
$attr = \array_merge_recursive($attr, $value); // ???? добавить проверку?
}
$this->setAttr('fIswev', $attr) ;
}
/**

View file

@ -126,7 +126,8 @@ class Install extends Page
$this->fIswev = ['e', \ForkBB\__('No styles')];
}
if ('POST' === $method && ! $changeLang && empty($this->a['fIswev']['e'])) { //????
$fIswev = $this->getAttr('fIswev'); // ????
if ('POST' === $method && ! $changeLang && empty($fIswev['e'])) { //????
$v = $this->c->Validator->reset()
->addValidators([
'check_prefix' => [$this, 'vCheckPrefix'],

View file

@ -22,10 +22,9 @@ class Profile extends Rules
*/
public function setUser(User $curUser)
{
$this->a = [];
$this->aCalc = [];
$this->ready = true;
$this->setAttrs([]);
$this->ready = true;
$this->user = $this->c->user;
$this->curUser = $curUser;
$this->my = $curUser->id === $this->user->id;

View file

@ -17,8 +17,8 @@ class Users extends Rules
*/
public function init()
{
$this->a = [];
$this->aCalc = [];
$this->setAttrs([]);
$this->ready = true;
$this->user = $this->c->user;

View file

@ -26,7 +26,7 @@ class Model extends DataModel
{
parent::__construct($container);
$this->dependProp = [
$this->zDepend = [
'group_id' => ['isUnverified', 'isGuest', 'isAdmin', 'isAdmMod', 'link', 'viewUsers', 'canViewIP', 'showPostCount', 'searchUsers'],
'id' => ['isGuest', 'link', 'avatar', 'online'],
'logged' => ['isLogged'],
@ -109,7 +109,13 @@ class Model extends DataModel
*/
protected function getlogged()
{
return empty($this->a['logged']) ? \time() : $this->a['logged'];
$attr = $this->getAttr('logged');
if (empty($attr)) { // ???? $attr < 1
$attr = \time();
}
return $attr;
}
/**
@ -119,7 +125,8 @@ class Model extends DataModel
*/
protected function getisLogged()
{
return ! empty($this->a['logged']);
$attr = $this->getAttr('logged');
return ! empty($attr);
}
/**
@ -130,10 +137,11 @@ class Model extends DataModel
protected function getlanguage()
{
$langs = $this->c->Func->getLangs();
$lang = $this->getAttr('language');
$lang = empty($this->a['language']) || ! isset($langs[$this->a['language']])
? $this->c->config->o_default_lang
: $this->a['language'];
if (empty($lang) || ! isset($langs[$lang])) {
$lang = $this->c->config->o_default_lang;
}
if (isset($langs[$lang])) {
return $lang;
@ -150,10 +158,11 @@ class Model extends DataModel
protected function getstyle()
{
$styles = $this->c->Func->getStyles();
$style = $this->getAttr('style');
$style = $this->isGuest || empty($this->a['style']) || ! isset($styles[$this->a['style']])
? $this->c->config->o_default_style
: $this->a['style'];
if ($this->isGuest || empty($style) || ! isset($styles[$style])) {
$style = $this->c->config->o_default_style;
}
if (isset($styles[$style])) {
return $style;
@ -317,7 +326,13 @@ class Model extends DataModel
*/
protected function getdisp_topics()
{
return (int) (empty($this->a['disp_topics']) ? $this->c->config->o_disp_topics_default : $this->a['disp_topics']);
$attr = $this->getAttr('disp_topics');
if (empty($attr)) {
$attr = $this->c->config->o_disp_topics_default;
}
return (int) $attr;
}
/**
@ -327,7 +342,13 @@ class Model extends DataModel
*/
protected function getdisp_posts()
{
return (int) (empty($this->a['disp_posts']) ? $this->c->config->o_disp_posts_default : $this->a['disp_posts']);
$attr = $this->getAttr('disp_topics');
if (empty($attr)) {
$attr = $this->c->config->o_disp_posts_default;
}
return (int) $attr;
}
/**
@ -373,11 +394,18 @@ class Model extends DataModel
*/
protected function setemail($email)
{
$this->a['email'] = $email;
$this->setAttr('email', $email);
if (isset($email[0])) {
$property = (! isset($this->track['email']) ? '__' : '') . 'email_normal';
$this->$property = $this->c->NormEmail->normalize($email);
if ('' == $email) {
return;
}
$nEmail = $this->c->NormEmail->normalize($email);
if (isset($this->zTrackFlags['email'])) {
$this->email_normal = $nEmail;
} else {
$this->__email_normal = $nEmail; // ???? $this->setAttr('email_normal', $nEmail);
}
}
}