Automation init...3

This commit is contained in:
최준흠 2024-09-10 00:12:54 +09:00
parent 1f42a513f8
commit a5b0a6feca
13 changed files with 87 additions and 73 deletions

View File

@ -130,6 +130,11 @@ define('AUTH_FIELDS', [
'TITLE' => 'title',
'ROLE' => 'role'
]);
//DBAction
define('DB_ACTION', [
'CREATE' => 'create',
'MODIFY' => 'modify',
]);
define("MESSENGERS", [
"skype" => [

View File

@ -8,7 +8,7 @@ class FileEntity extends MyStorageEntity
{
public function __toString(): string
{
return "{$this->getPK()}|{$this->getTitle()}|{$this->getSequence()}번째";
return "{$this->getPK()}|" . parent::__toString();
}
//Common Function
@ -16,14 +16,4 @@ class FileEntity extends MyStorageEntity
{
return $this->attributes['pid'];
}
//한게시물에 여러개가 있을경우 번호
final public function getSequence(): int
{
return $this->attributes['file_sequence'];
}
public function setSequence(int $file_sequence): void
{
$this->attributes['file_sequence'] = $file_sequence;
}
}

View File

@ -12,7 +12,7 @@ class FileEntity extends CommonEntity
}
public function __toString(): string
{
return "{$this->getPath()}|{$this->getTitle()}|{$this->getMimeType()}";
return "{$this->getPath()}|{$this->getTitle()}|{$this->getMimeType()}}|{$this->getSequence()}번째";
}
public function getTitle(): string
{
@ -56,4 +56,13 @@ class FileEntity extends CommonEntity
{
$this->attributes['media_html'] = $media_html;
}
//한게시물에 여러개가 있을경우 번호
final public function getSequence(): int
{
return $this->attributes['file_sequence'];
}
public function setSequence(int $file_sequence): void
{
$this->attributes['file_sequence'] = $file_sequence;
}
}

View File

@ -24,11 +24,11 @@ abstract class MyCrawlerLibrary extends CommonLibrary
return $crawler->filter($tag);
}
final protected function getNodes(Crawler $crawler, array $options, $nodes = []): array
private function getNodes(Crawler $crawler, array $options, $nodes = []): array
{
$crawler->filter($options["tag"])->each(
function (Crawler $node) use (&$options, &$nodes): void {
log_message("debug", sprintf("getNode-> %s", $options["tag"]));
log_message("debug", sprintf("getNode->%s[%s]", $options["tag"], $node->attr($options['attr'])));
$nodes[] = $node;
}
);

View File

@ -132,11 +132,8 @@ class YamapLibrary extends MyCrawlerLibrary
//망보드 게시판에 등록
$entity = $this->getBoardModel()->create($entity);
log_message("debug", "Board DB 등록 완료");
//망보드 파일관리툴에 등록된 파일게시물에 등록한 게시판번호 수정하기
foreach ($fileEntitys as $fileEntity) {
$this->getMyStorage()->updateFileEntityBoardPK($fileEntity, $entity);
}
$this->getMyStorage()->setBoardPID($fileEntitys, $entity->getPK());
return $entity;
}
}

View File

@ -12,10 +12,15 @@ class FileLibrary extends MyStorageLibrary
parent::__construct();
$this->_path = $path;
}
final public function getPath(): string
{
return $this->_path;
}
public function getFileEntity(): FileEntity
{
return new FileEntity();
}
protected function getMediaTag(string $mediaType, FileEntity $entity): string
{
@ -62,13 +67,14 @@ class FileLibrary extends MyStorageLibrary
if (!file_put_contents($saveFilePath, $content)) {
throw new \Exception("파일저장 실패:{$saveFilePath}");
}
$entity = new FileEntity();
$entity = $this->getFileEntity();
$entity->setPath($this->getPath());
$entity->setTitle($fileName);
$entity->setMimeType(mime_content_type($saveFilePath));
$entity->setSize(filesize($saveFilePath));
$entity->setSize(!filesize($saveFilePath) ?: 0);
$entity->setSequence($file_sequence);
$entity->setMediaHTML($this->getMediaTag($mediaType, $entity));
log_message("debug", "{$file_sequence}번째 파일저장 완료->{$entity}");
log_message("debug", "{$file_sequence}번째 " . $entity->getTitle() . " 파일저장 완료");
return $entity;
}
}

View File

@ -3,13 +3,12 @@
namespace App\Libraries\MyStorage;
use App\Libraries\MyStorage\FileLibrary;
use App\Libraries\MyStorage\FileLibrary as MyStorageLibrary;
use App\Models\Mangboard\FileModel;
use App\Entities\Mangboard\UserEntity;
use App\Entities\Mangboard\FileEntity;
use App\Entities\Mangboard\BoardEntity;
class MangboardLibrary extends FileLibrary
class MangboardLibrary extends MyStorageLibrary
{
private $_user = null;
private $_boardName = "";
@ -27,9 +26,10 @@ class MangboardLibrary extends FileLibrary
}
return $this->_model;
}
//override
public function getFileEntity(): FileEntity
{
return new FileEntity();
}
public function getUser(): UserEntity
{
if ($this->_user === null) {
@ -81,8 +81,6 @@ class MangboardLibrary extends FileLibrary
$entity = parent::save($fileName, $mediaType, $content, $file_sequence);
log_message("debug", "{$file_sequence}번째 File DB등록 준비->{$entity->getTitle()}|{$entity->getMimeType()}");
//망보드 파일관리 table에 등록
$entity = new FileEntity($entity->toArray()); //형변환을 위해서
$entity->setPath($this->getPath());
$entity->user_pid = $this->getUser()->getPK();
$entity->user_name = $this->getUser()->getTitle();
$entity->board_name = $this->getBoardName();
@ -91,16 +89,20 @@ class MangboardLibrary extends FileLibrary
$entity->file_caption = $fileName;
$entity->file_alt = $entity->getTitle();
$entity->file_description = "Filedata";
$entity->setSequence($file_sequence);
log_message("debug", var_export($entity, true));
$entity = $this->getModel()->create($entity);
exit;
log_message("debug", "{$file_sequence}번째 File DB등록 완료->{$entity}");
return $entity;
}
public function updateFileEntityBoardPK(FileEntity $entity, BoardEntity $boardEntity): FileEntity
public function setBoardPID(array $fileEntitys, int $board_pid)
{
$entity->board_pid = $boardEntity->getPK();
$this->getModel()->setFields(["board_pid"]);
return $this->getModel()->modify($entity);
//망보드 파일관리툴에 등록된 파일게시물에 등록한 게시판번호 수정하기
foreach ($fileEntitys as $fileEntity) {
$fileEntity->board_pid = $board_pid;
$this->getModel()->setFields(["board_pid"]);
$this->getModel()->modify($fileEntity);
}
}
}

View File

@ -45,6 +45,7 @@ abstract class CommonModel extends Model
protected $beforeDelete = [];
protected $afterDelete = [];
private $_action = DB_ACTION["CREATE"];
protected function __construct(array $fields)
{
parent::__construct();
@ -52,10 +53,18 @@ abstract class CommonModel extends Model
}
abstract public function getTitleField(): string;
public function getPKField(): string
final public function getPKField(): string
{
return $this->primaryKey;
}
final public function getAction(): string
{
return $this->_action;
}
final public function setAction(string $action): string
{
return $this->_action = $action;
}
final public function getFields(array $options = []): array //options=>except or only
{
if (isset($options['except'])) {
@ -68,29 +77,22 @@ abstract class CommonModel extends Model
final public function setFields(array $fields): void
{
$this->allowedFields = $fields;
$this->setRules($this->allowedFields);
}
final public function getRules(array $options): array //options=>except or only
{
return $this->getValidationRules($options);
}
final public function setRules(array $fields, $rules = [], $isCreate = true): void
final public function setFieldRules(array $fields, array $rules = []): void
{
foreach ($fields as $field) {
$rules = $this->getFieldRule($field, $rules, $isCreate);
$rules = $this->getFieldRule($field, $rules);
}
$this->setValidationRules($rules);
$this->validationRules[$field] = $rules;
}
protected function getFieldRule(string $field, array $rules, $isCreate = true): array
public function getFieldRule(string $field, array $rules): array
{
switch ($field) {
case $this->getPKField():
//수동입력인경우
if (!$this->useAutoIncrement) {
$rules[$field] = "required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]";
$rules[$field] = $isCreate ? "|is_unique[{$this->table}.{$field}]" : "";
$rules[$field] .= $this->getAction() == DB_ACTION["CREATE"] ? "|is_unique[{$this->table}.{$field}]" : "";
} else {
$rules[$field] = "required|numeric";
};
@ -99,8 +101,8 @@ abstract class CommonModel extends Model
$rules[$field] = "required|string";
break;
case "passwd":
$rules[$field] = $isCreate ? "required|trim|string" : "if_exist|trim|string";
$rules["confirmpassword"] = $isCreate ? "required|trim|string|matches[passwd]" : "if_exist|trim|string|matches[passwd]";
$rules[$field] = $this->getAction() == DB_ACTION["CREATE"] ? "required|trim|string" : "if_exist|trim|string";
$rules["confirmpassword"] = $this->getAction() == DB_ACTION["CREATE"] ? "required|trim|string|matches[passwd]" : "if_exist|trim|string|matches[passwd]";
break;
case "email":
$rules[$field] = "if_exist|trim|valid_email";
@ -130,11 +132,14 @@ abstract class CommonModel extends Model
}
//create , modify 직전 작업용 작업
protected function setEntityData($entity, $field): mixed
final protected function convertEntityData($entity, $field): mixed
{
if ($entity->$field === null) {
return $entity;
}
switch ($field) {
case $this->getPKField():
if ($$entity->$field === null) {
if ($entity->$field === null) {
$randomBytes = bin2hex(random_bytes(32));
$entity->$field = sprintf(
'%08s-%04s-%04x-%04x-%12s',
@ -150,9 +155,7 @@ abstract class CommonModel extends Model
$entity->$field = password_hash($entity->$field, PASSWORD_DEFAULT);
break;
case "content":
if ($entity->$field !== null) {
$entity->$field = htmlentities($entity->$field, ENT_QUOTES);
}
$entity->$field = htmlentities($entity->$field, ENT_QUOTES);
break;
}
return $entity;
@ -160,10 +163,14 @@ abstract class CommonModel extends Model
private function save_process($entity): mixed
{
//변경이 필요한 Field 재정의
foreach ($this->getFields() as $field) {
$entity = $this->setEntityData($entity, $field);
$entity = $this->convertEntityData($entity, $field);
}
//최종 변경사항이 있으면 저장
if ($entity->hasChanged()) {
//Field에 맞는 Validation Rule 재정의
$this->setFieldRules($this->getFields());
if (!$this->save($entity)) {
log_message("error", __FUNCTION__ . "에서 호출:" . $this->getLastQuery());
log_message("error", implode("\n", $this->errors()));
@ -176,14 +183,13 @@ abstract class CommonModel extends Model
}
final protected function create_process($entity): mixed
{
$this->setAction(DB_ACTION['CREATE']);
//primaryKey가 수동입력이면
$pkField = $this->getPKField();
if (!$this->useAutoIncrement && $entity->$pkField === null) {
//PrimayKey Field를 allowedFields의 맨앞에 넣기 -> array_unshif
array_unshift($this->allowedFields, $this->getPKField());
$entity = $this->setEntityData($entity, $this->getPKField());
//Create용 Rule다시적용
$this->setRules($this->getFields(), [], true);
$entity = $this->convertEntityData($entity, $this->getPKField());
}
$entity = $this->save_process($entity);
//primaryKey가 자동입력이면
@ -194,8 +200,7 @@ abstract class CommonModel extends Model
}
final protected function modify_process($entity): mixed
{
//Create용 Rule다시적용
$this->setRules($this->getFields(), [], false);
$this->setAction(DB_ACTION['MODIFY']);
return $this->save_process($entity);
}
}

View File

@ -94,7 +94,7 @@ class BoardModel extends CommonModel
{
return 'title';
}
public function getFieldRule(string $field, array $rules, $isCreate = true): array
public function getFieldRule(string $field, array $rules): array
{
switch ($field) {
case 'gid':
@ -118,7 +118,7 @@ class BoardModel extends CommonModel
$rules[$field] = "if_exist|numeric";
break;
default:
$rules = parent::getFieldRule($field, $rules, $isCreate);
$rules = parent::getFieldRule($field, $rules);
break;
}
return $rules;
@ -142,7 +142,7 @@ class BoardModel extends CommonModel
//GID값이 PK랑 같은 값 전달 후 Entity 수정
$entity->gid = $entity->getPK();
$this->setFields(["gid"]);
return $this->modify_process($entity);
return $this->modify($entity);
}
//modify용

View File

@ -60,7 +60,7 @@ class FileModel extends CommonModel
{
return 'file_name';
}
public function getFieldRule(string $field, array $rules, $isCreate = true): array
public function getFieldRule(string $field, array $rules): array
{
switch ($field) {
case "board_pid":
@ -85,7 +85,7 @@ class FileModel extends CommonModel
$rules[$field] = "if_exist|valid_date";
break;
default:
$rules = parent::getFieldRule($field, $rules, $isCreate);
$rules = parent::getFieldRule($field, $rules);
break;
}
return $rules;

View File

@ -109,7 +109,7 @@ class UserModel extends CommonModel
{
return 'user_name';
}
public function getFieldRule(string $field, array $rules, $isCreate = true): array
public function getFieldRule(string $field, array $rules): array
{
switch ($field) {
case "user_id":
@ -127,7 +127,7 @@ class UserModel extends CommonModel
$rules[$field] = "if_exist|numeric";
break;
default:
$rules = parent::getFieldRule($field, $rules, $isCreate);
$rules = parent::getFieldRule($field, $rules);
break;
}
return $rules;
@ -225,7 +225,7 @@ class UserModel extends CommonModel
//GID값이 PK랑 같은 값 전달 후 Entity 수정
$entity->gid = $entity->getPK();
$this->setFields(["gid"]);
return $this->modify_process($entity);
return $this->modify($entity);
}
//modify용

View File

@ -20,7 +20,7 @@ class SNSUserModel extends CommonModel
{
return 'name';
}
public function getFieldRule(string $field, array $rules, $isCreate = true): array
public function getFieldRule(string $field, array $rules): array
{
switch ($field) {
case "id":
@ -33,7 +33,7 @@ class SNSUserModel extends CommonModel
$rules[$field] = "if_exist|trim|valid_email";
break;
default:
$rules = parent::getFieldRule($field, $rules, $isCreate);
$rules = parent::getFieldRule($field, $rules);
break;
}
return $rules;

View File

@ -20,11 +20,11 @@ class UserModel extends CommonModel
{
return 'name';
}
public function getFieldRule(string $field, array $rules, $isCreate = true): array
public function getFieldRule(string $field, array $rules): array
{
switch ($field) {
case "id":
if ($isCreate) {
if ($this->getAction() == DB_ACTION["CREATE"]) {
$rules[$field] = "required|trim|min_length[4]|max_length[20]|is_unique[{$this->table}.{$field}]";
} else {
$rules[$field] = "required|trim|min_length[4]|max_length[20]";
@ -44,7 +44,7 @@ class UserModel extends CommonModel
break;
case "passwd":
default:
$rules = parent::getFieldRule($field, $rules, $isCreate);
$rules = parent::getFieldRule($field, $rules);
break;
}
return $rules;