ErrorMsg auto log if log level is debug, Form\Generate split out DBArrayIO class

This commit is contained in:
2023-10-02 12:29:50 +09:00
parent 2d7c3c2bba
commit dad6b797e0
5 changed files with 593 additions and 413 deletions

View File

@@ -154,7 +154,7 @@ class EditBase
$q = "UPDATE " . $table_name $q = "UPDATE " . $table_name
. " SET order_number = " . $row_data_order[$i] . " SET order_number = " . $row_data_order[$i]
. " WHERE " . $table_name . "_id = " . $row_data_id[$i]; . " WHERE " . $table_name . "_id = " . $row_data_id[$i];
$q = $this->form->dbExec($q); $q = $this->form->dba->dbExec($q);
} }
} // for all article ids ... } // for all article ids ...
} // if write } // if write
@@ -173,7 +173,7 @@ class EditBase
$options_name = []; $options_name = [];
$options_selected = []; $options_selected = [];
// DB read data for menu // DB read data for menu
while (is_array($res = $this->form->dbReturn($q))) { while (is_array($res = $this->form->dba->dbReturn($q))) {
$row_data[] = [ $row_data[] = [
"id" => $res[$table_name . "_id"], "id" => $res[$table_name . "_id"],
"name" => $res["name"], "name" => $res["name"],
@@ -431,9 +431,9 @@ class EditBase
$elements[] = $this->form->formCreateElement('template'); $elements[] = $this->form->formCreateElement('template');
break; break;
case 'edit_pages': case 'edit_pages':
if (!isset($this->form->table_array['edit_page_id']['value'])) { if (!isset($this->form->dba->getTableArray()['edit_page_id']['value'])) {
$q = "DELETE FROM temp_files"; $q = "DELETE FROM temp_files";
$this->form->dbExec($q); $this->form->dba->dbExec($q);
// gets all files in the current dir and dirs given ending with .php // gets all files in the current dir and dirs given ending with .php
$folders = ['../admin/', '../frontend/']; $folders = ['../admin/', '../frontend/'];
$files = ['*.php']; $files = ['*.php'];
@@ -461,16 +461,16 @@ class EditBase
if ($t_q) { if ($t_q) {
$t_q .= ', '; $t_q .= ', ';
} }
$t_q .= "('" . $this->form->dbEscapeString($pathinfo['dirname']) . "', '" $t_q .= "('" . $this->form->dba->dbEscapeString($pathinfo['dirname']) . "', '"
. $this->form->dbEscapeString($pathinfo['basename']) . "')"; . $this->form->dba->dbEscapeString($pathinfo['basename']) . "')";
} }
$this->form->dbExec($q . $t_q, 'NULL'); $this->form->dba->dbExec($q . $t_q, 'NULL');
$elements[] = $this->form->formCreateElement('filename'); $elements[] = $this->form->formCreateElement('filename');
} else { } else {
// show file menu // show file menu
// just show name of file ... // just show name of file ...
$this->DATA['filename_exist'] = 1; $this->DATA['filename_exist'] = 1;
$this->DATA['filename'] = $this->form->table_array['filename']['value']; $this->DATA['filename'] = $this->form->dba->getTableArray()['filename']['value'];
} // File Name View IF } // File Name View IF
$elements[] = $this->form->formCreateElement('hostname'); $elements[] = $this->form->formCreateElement('hostname');
$elements[] = $this->form->formCreateElement('name'); $elements[] = $this->form->formCreateElement('name');

View File

@@ -39,13 +39,13 @@ class ArrayIO extends \CoreLibs\DB\IO
{ {
// main calss variables // main calss variables
/** @var array<mixed> */ /** @var array<mixed> */
public array $table_array; // the array from the table to work on private array $table_array; // the array from the table to work on
/** @var string */ /** @var string */
public string $table_name; // the table_name private string $table_name; // the table_name
/** @var string */ /** @var string */
public string $pk_name = ''; // the primary key from this table private string $pk_name = ''; // the primary key from this table
/** @var int|string|null */ /** @var int|string|null */
public int|string|null $pk_id; // the PK id private int|string|null $pk_id; // the PK id
// security values // security values
/** @var int base acl for current page */ /** @var int base acl for current page */
private int $base_acl_level = 0; private int $base_acl_level = 0;
@@ -74,24 +74,21 @@ class ArrayIO extends \CoreLibs\DB\IO
// instance db_io class // instance db_io class
parent::__construct($db_config, $log); parent::__construct($db_config, $log);
// more error vars for this class // more error vars for this class
$this->error_string['1999'] = 'No table array or table name set'; $this->error_string['1998'] = 'No table name set';
$this->error_string['1999'] = 'No table array set';
$this->error_string['1021'] = 'No Primary Key given'; $this->error_string['1021'] = 'No Primary Key given';
$this->error_string['1022'] = 'Could not run Array Query'; $this->error_string['1022'] = 'Could not run Array Query';
$this->table_array = $table_array; $this->setTableArray($table_array);
$this->table_name = $table_name; $this->setTableName($table_name);
// error abort if no table array or no table name
if (empty($table_array) || empty($table_name)) {
$this->__dbError(1999, false, 'MAJOR ERROR: Core settings missing');
throw new \RuntimeException('MAJOR ERROR: Core settings missing', 1999);
}
// set primary key for given table_array // set primary key for given table_array
foreach ($this->table_array as $key => $value) { foreach ($this->table_array as $key => $value) {
if (!empty($value['pk'])) { if (empty($value['pk'])) {
$this->pk_name = $key; continue;
} }
$this->setPkName($key);
break;
} }
$this->dbArrayIOSetAcl($base_acl_level, $acl_admin); $this->dbArrayIOSetAcl($base_acl_level, $acl_admin);
} }
@@ -104,6 +101,144 @@ class ArrayIO extends \CoreLibs\DB\IO
parent::__destruct(); parent::__destruct();
} }
/**
* Set the overall table array
*
* @param array<mixed> $table_array
* @return void
* @throws \RuntimeException 1999 for empty table array
*/
public function setTableArray(array $table_array): void
{
$this->table_array = $table_array;
if (empty($this->table_array)) {
$this->__dbError(1999, false, 'MAJOR ERROR: Core settings missing: table_arrry');
throw new \RuntimeException('MAJOR ERROR: Core settings missing: table_array', 1999);
}
}
/**
* return full table array, or [] if empty
* of reset is set to true, will reset array first
*
* @param bool $reset [=false] run a reset before returning
* @return array<mixed>
*/
public function getTableArray(bool $reset = false): array
{
if (!$reset) {
return $this->table_array ?? [];
}
$table_array = $this->table_array ?? [];
reset($table_array);
return $table_array;
}
/**
* get a table array entry under the key with element pos
*
* @param string $key
* @param string $pos
* @return mixed
*/
public function getTableArrayEntry(string $key, string $pos): mixed
{
return $this->table_array[$key][$pos] ?? null;
}
/**
* set a new value at key with pos
*
* @param mixed $value
* @param string $key
* @param string $pos
* @return void
*/
public function setTableArrayEntry(mixed $value, string $key, string $pos): void
{
$this->table_array[$key][$pos] = $value;
}
/**
* unset entry at key with pos
*
* @param string $key
* @param string $pos
* @return void
*/
public function unsetTableArrayEntry(string $key, string $pos): void
{
unset($this->table_array[$key][$pos]);
}
/**
* Set table name
*
* @param string $table_name
* @return void
* @throws \RuntimeException 1998 for empty table name
*/
public function setTableName(string $table_name): void
{
$this->table_name = $table_name;
if (empty($this->table_name)) {
$this->__dbError(1998, false, 'MAJOR ERROR: Core settings missing: table_name');
throw new \RuntimeException('MAJOR ERROR: Core settings missing: table_name', 1998);
}
}
/**
* Return table name or empty string if not net
*
* @return string
*/
public function getTableName(): string
{
return $this->table_name ?? '';
}
/**
* Set primary key name
*
* @param string $pk_name
* @return void
*/
public function setPkName(string $pk_name): void
{
$this->pk_name = $pk_name;
}
/**
* get primary key name
*
* @return string
*/
public function getPkName(): string
{
return $this->pk_name;
}
/**
* set primary key id, can be null for not yet set
*
* @param int|string|null $pk_id
* @return void
*/
public function setPkId(int|string|null $pk_id): void
{
$this->pk_id = $pk_id;
}
/**
* return primary key id, or null if not set
*
* @return int|string|null
*/
public function getPkId(): int|string|null
{
return $this->pk_id;
}
/** /**
* set the base acl level and admin acl flag * set the base acl level and admin acl flag
* This is needed for table array ACL checks * This is needed for table array ACL checks
@@ -198,8 +333,8 @@ class ArrayIO extends \CoreLibs\DB\IO
public function dbCheckPkSet(): bool public function dbCheckPkSet(): bool
{ {
// if pk_id is set, overrule ... // if pk_id is set, overrule ...
if (!empty($this->pk_id)) { if (!empty($this->getPkId())) {
$this->table_array[$this->pk_name]['value'] = $this->pk_id; $this->table_array[$this->pk_name]['value'] = $this->getPkId();
} }
// if not set ... produce error // if not set ... produce error
if (!$this->table_array[$this->pk_name]['value']) { if (!$this->table_array[$this->pk_name]['value']) {
@@ -287,7 +422,7 @@ class ArrayIO extends \CoreLibs\DB\IO
$q .= ' AND ' . $q_where; $q .= ' AND ' . $q_where;
} }
// if 0, error // if 0, error
$this->pk_id = null; $this->setPkId(null);
if (!$this->dbExec($q)) { if (!$this->dbExec($q)) {
$this->__dbError(1022); $this->__dbError(1022);
} }
@@ -374,7 +509,7 @@ class ArrayIO extends \CoreLibs\DB\IO
} }
} }
// possible dbFetchArray errors ... // possible dbFetchArray errors ...
$this->pk_id = $this->table_array[$this->pk_name]['value']; $this->setPkId($this->table_array[$this->pk_name]['value']);
} else { } else {
$this->__dbError(1022); $this->__dbError(1022);
} }
@@ -397,10 +532,6 @@ class ArrayIO extends \CoreLibs\DB\IO
if (count($table_array)) { if (count($table_array)) {
$this->table_array = $table_array; $this->table_array = $table_array;
} }
// PK ID check
// if ($this->pk_id && !$this->table_array[$this->pk_name]["value"]) {
// $this->table_array[$this->pk_name]["value"]=$this->pk_id;
// }
// checken ob PKs gesetzt, wenn alle -> update, wenn keiner -> insert, wenn ein paar -> ERROR! // checken ob PKs gesetzt, wenn alle -> update, wenn keiner -> insert, wenn ein paar -> ERROR!
if (!$this->table_array[$this->pk_name]['value']) { if (!$this->table_array[$this->pk_name]['value']) {
$insert = 1; $insert = 1;
@@ -624,16 +755,11 @@ class ArrayIO extends \CoreLibs\DB\IO
$q .= ' AND ' . $q_where; $q .= ' AND ' . $q_where;
} }
// set pk_id ... if it has changed or so // set pk_id ... if it has changed or so
$this->pk_id = $this->table_array[$this->pk_name]['value']; $this->setPkId($this->table_array[$this->pk_name]['value']);
} else { } else {
$q = 'INSERT INTO ' . $this->table_name . ' '; $q = 'INSERT INTO ' . $this->table_name . ' ';
$q .= '(' . $q_vars . ') '; $q .= '(' . $q_vars . ') ';
$q .= 'VALUES (' . $q_data . ')'; $q .= 'VALUES (' . $q_data . ')';
// write primary key too
// if ($q_data)
// $q .= ", ";
// $q .= $this->pk_name." = ".$this->table_array[$this->pk_name]['value']." ";
// $this->pk_id = $this->table_array[$this->pk_name]['value'];
} }
// return success or not // return success or not
if (!$this->dbExec($q)) { if (!$this->dbExec($q)) {
@@ -646,7 +772,7 @@ class ArrayIO extends \CoreLibs\DB\IO
$insert_id = 0; $insert_id = 0;
} }
$this->table_array[$this->pk_name]['value'] = $insert_id; $this->table_array[$this->pk_name]['value'] = $insert_id;
$this->pk_id = $insert_id; $this->setPkId($insert_id);
} }
// return the table if needed // return the table if needed
return $this->table_array; return $this->table_array;

View File

@@ -27,13 +27,19 @@ class ErrorMessage
* init ErrorMessage * init ErrorMessage
* *
* @param \CoreLibs\Logging\Logging $log * @param \CoreLibs\Logging\Logging $log
* @param bool $log_error [=false] * @param null|bool $log_error [=null], defaults to false if log is not level debug
*/ */
public function __construct( public function __construct(
\CoreLibs\Logging\Logging $log, \CoreLibs\Logging\Logging $log,
bool $log_error = false ?bool $log_error = null
) { ) {
$this->log = $log; $this->log = $log;
// if log default logging is debug then log_error is default set to true
if ($this->log->loggingLevelIsDebug() && $log_error === null) {
$log_error = true;
} else {
$log_error = $log_error ?? false;
}
$this->log_error = $log_error; $this->log_error = $log_error;
} }

File diff suppressed because it is too large Load Diff

View File

@@ -96,9 +96,9 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
public function testErrorMessageLevelOk(string $level, string $str, string $expected): void public function testErrorMessageLevelOk(string $level, string $str, string $expected): void
{ {
$log = new \CoreLibs\Logging\Logging([ $log = new \CoreLibs\Logging\Logging([
'log_file_id' => 'testErrorMessages', 'log_file_id' => 'testErrorMessagesLevelOk',
'log_folder' => self::LOG_FOLDER, 'log_folder' => self::LOG_FOLDER,
'log_level' => Level::Debug, 'log_level' => Level::Error,
]); ]);
$em = new \CoreLibs\Logging\ErrorMessage($log); $em = new \CoreLibs\Logging\ErrorMessage($log);
$em->setMessage( $em->setMessage(
@@ -128,9 +128,9 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
public function testErrorMessageOk(): void public function testErrorMessageOk(): void
{ {
$log = new \CoreLibs\Logging\Logging([ $log = new \CoreLibs\Logging\Logging([
'log_file_id' => 'testErrorMessages', 'log_file_id' => 'testErrorMessagesOk',
'log_folder' => self::LOG_FOLDER, 'log_folder' => self::LOG_FOLDER,
'log_level' => Level::Debug 'log_level' => Level::Error
]); ]);
$em = new \CoreLibs\Logging\ErrorMessage($log); $em = new \CoreLibs\Logging\ErrorMessage($log);
$em->setErrorMsg( $em->setErrorMsg(
@@ -314,7 +314,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
* Undocumented function * Undocumented function
* *
* @dataProvider providerErrorMessageLog * @dataProvider providerErrorMessageLog
* @testdox Test Log writing [$_dataName] * @testdox Test Log writing with log level Error [$_dataName]
* *
* @param string $id * @param string $id
* @param string $level * @param string $level
@@ -324,7 +324,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
* @param string $expected * @param string $expected
* @return void * @return void
*/ */
public function testErrorMessageLog( public function testErrorMessageLogErrorLevel(
string $id, string $id,
string $level, string $level,
string $str, string $str,
@@ -333,7 +333,63 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
string $expected string $expected
): void { ): void {
$log = new \CoreLibs\Logging\Logging([ $log = new \CoreLibs\Logging\Logging([
'log_file_id' => 'testErrorMessages', 'log_file_id' => 'testErrorMessagesLogError',
'log_folder' => self::LOG_FOLDER,
'log_level' => Level::Notice,
'log_per_run' => true
]);
$em = new \CoreLibs\Logging\ErrorMessage($log);
$em->setErrorMsg(
$id,
$level,
$str,
message: $message,
log_error: $log_error
);
$file_content = '';
if (is_file($log->getLogFolder() . $log->getLogFile())) {
$file_content = file_get_contents(
$log->getLogFolder() . $log->getLogFile()
) ?: '';
}
// if error, if null or false, it will not be logged
if ($level == 'error' && ($log_error === null || $log_error === false)) {
$this->assertStringNotContainsString(
$expected,
$file_content
);
} else {
$this->assertStringContainsString(
$expected,
$file_content
);
}
}
/**
* Undocumented function
*
* @dataProvider providerErrorMessageLog
* @testdox Test Log writing with log Level Debug [$_dataName]
*
* @param string $id
* @param string $level
* @param string $str
* @param string|null $message
* @param bool|null $log_error
* @param string $expected
* @return void
*/
public function testErrorMessageLogErrorDebug(
string $id,
string $level,
string $str,
?string $message,
?bool $log_error,
string $expected
): void {
$log = new \CoreLibs\Logging\Logging([
'log_file_id' => 'testErrorMessagesLogDebug',
'log_folder' => self::LOG_FOLDER, 'log_folder' => self::LOG_FOLDER,
'log_level' => Level::Debug, 'log_level' => Level::Debug,
'log_per_run' => true 'log_per_run' => true
@@ -352,8 +408,8 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
$log->getLogFolder() . $log->getLogFile() $log->getLogFolder() . $log->getLogFile()
) ?: ''; ) ?: '';
} }
// if n // if error, and log is debug level, only explicit false are not logged
if ($level == 'error' && ($log_error === null || $log_error === false)) { if ($level == 'error' && $log_error === false) {
$this->assertStringNotContainsString( $this->assertStringNotContainsString(
$expected, $expected,
$file_content $file_content