From a5b0a6feca2e505a4dd6a293ef9f3b4e081f0416 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0?= Date: Tue, 10 Sep 2024 00:12:54 +0900 Subject: [PATCH] Automation init...3 --- app/Config/Constants.php | 5 ++ app/Entities/Mangboard/FileEntity.php | 12 +---- app/Entities/MyStorage/FileEntity.php | 11 +++- app/Libraries/MyCrawler/MyCrawlerLibrary.php | 4 +- app/Libraries/MyCrawler/YamapLibrary.php | 5 +- app/Libraries/MyStorage/FileLibrary.php | 12 +++-- app/Libraries/MyStorage/MangboardLibrary.php | 28 +++++----- app/Models/CommonModel.php | 57 +++++++++++--------- app/Models/Mangboard/BoardModel.php | 6 +-- app/Models/Mangboard/FileModel.php | 4 +- app/Models/Mangboard/UserModel.php | 6 +-- app/Models/SNSUserModel.php | 4 +- app/Models/UserModel.php | 6 +-- 13 files changed, 87 insertions(+), 73 deletions(-) diff --git a/app/Config/Constants.php b/app/Config/Constants.php index 973884e..146c2f8 100644 --- a/app/Config/Constants.php +++ b/app/Config/Constants.php @@ -130,6 +130,11 @@ define('AUTH_FIELDS', [ 'TITLE' => 'title', 'ROLE' => 'role' ]); +//DBAction +define('DB_ACTION', [ + 'CREATE' => 'create', + 'MODIFY' => 'modify', +]); define("MESSENGERS", [ "skype" => [ diff --git a/app/Entities/Mangboard/FileEntity.php b/app/Entities/Mangboard/FileEntity.php index 90345c8..dfad72d 100644 --- a/app/Entities/Mangboard/FileEntity.php +++ b/app/Entities/Mangboard/FileEntity.php @@ -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; - } } diff --git a/app/Entities/MyStorage/FileEntity.php b/app/Entities/MyStorage/FileEntity.php index c666228..aeb2978 100644 --- a/app/Entities/MyStorage/FileEntity.php +++ b/app/Entities/MyStorage/FileEntity.php @@ -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; + } } diff --git a/app/Libraries/MyCrawler/MyCrawlerLibrary.php b/app/Libraries/MyCrawler/MyCrawlerLibrary.php index 3ad3625..ae01f11 100644 --- a/app/Libraries/MyCrawler/MyCrawlerLibrary.php +++ b/app/Libraries/MyCrawler/MyCrawlerLibrary.php @@ -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; } ); diff --git a/app/Libraries/MyCrawler/YamapLibrary.php b/app/Libraries/MyCrawler/YamapLibrary.php index 07be3fa..25c0e8f 100644 --- a/app/Libraries/MyCrawler/YamapLibrary.php +++ b/app/Libraries/MyCrawler/YamapLibrary.php @@ -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; } } diff --git a/app/Libraries/MyStorage/FileLibrary.php b/app/Libraries/MyStorage/FileLibrary.php index 02458ff..d11597b 100644 --- a/app/Libraries/MyStorage/FileLibrary.php +++ b/app/Libraries/MyStorage/FileLibrary.php @@ -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; } } diff --git a/app/Libraries/MyStorage/MangboardLibrary.php b/app/Libraries/MyStorage/MangboardLibrary.php index 138e456..acd5f44 100644 --- a/app/Libraries/MyStorage/MangboardLibrary.php +++ b/app/Libraries/MyStorage/MangboardLibrary.php @@ -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); + } } } diff --git a/app/Models/CommonModel.php b/app/Models/CommonModel.php index 26a5a93..71742db 100644 --- a/app/Models/CommonModel.php +++ b/app/Models/CommonModel.php @@ -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); } } diff --git a/app/Models/Mangboard/BoardModel.php b/app/Models/Mangboard/BoardModel.php index 814df9f..ec8f816 100644 --- a/app/Models/Mangboard/BoardModel.php +++ b/app/Models/Mangboard/BoardModel.php @@ -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용 diff --git a/app/Models/Mangboard/FileModel.php b/app/Models/Mangboard/FileModel.php index ea50cb3..860f9d7 100644 --- a/app/Models/Mangboard/FileModel.php +++ b/app/Models/Mangboard/FileModel.php @@ -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; diff --git a/app/Models/Mangboard/UserModel.php b/app/Models/Mangboard/UserModel.php index 34d3bf4..845fced 100644 --- a/app/Models/Mangboard/UserModel.php +++ b/app/Models/Mangboard/UserModel.php @@ -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용 diff --git a/app/Models/SNSUserModel.php b/app/Models/SNSUserModel.php index db88744..1d468fc 100644 --- a/app/Models/SNSUserModel.php +++ b/app/Models/SNSUserModel.php @@ -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; diff --git a/app/Models/UserModel.php b/app/Models/UserModel.php index 3b094cb..1611efd 100644 --- a/app/Models/UserModel.php +++ b/app/Models/UserModel.php @@ -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;