From 83f54434b8a4742e96f9e40e59ada85f4fb5159b Mon Sep 17 00:00:00 2001 From: "choi.jh" Date: Tue, 18 Nov 2025 10:47:20 +0900 Subject: [PATCH] trafficmonitor init...2 --- ...troller.php => AbstractCRUDController.php} | 4 +- app/Controllers/CommonController.php | 2 +- app/Controllers/CommonControllerV1.php | 4 -- app/DTOs/MylogDTO.php | 4 ++ app/Forms/MylogForm.php | 6 ++- app/Language/en/Mylog.php | 2 + app/Libraries/OperationContext.php | 21 ++++++++ app/Libraries/PipelineStep.php | 48 +++++++++++++++++ app/Libraries/PipelineStepInterface.php | 16 ++++++ app/Models/MylogModel.php | 2 + app/Services/MylogService.php | 27 +++++++++- app/Services/PipelineRunnerService.php | 53 ------------------- 12 files changed, 127 insertions(+), 62 deletions(-) rename app/Controllers/{AbstractCrudController.php => AbstractCRUDController.php} (98%) create mode 100644 app/Libraries/OperationContext.php create mode 100644 app/Libraries/PipelineStep.php create mode 100644 app/Libraries/PipelineStepInterface.php delete mode 100644 app/Services/PipelineRunnerService.php diff --git a/app/Controllers/AbstractCrudController.php b/app/Controllers/AbstractCRUDController.php similarity index 98% rename from app/Controllers/AbstractCrudController.php rename to app/Controllers/AbstractCRUDController.php index c8de360..9b1f26b 100644 --- a/app/Controllers/AbstractCrudController.php +++ b/app/Controllers/AbstractCRUDController.php @@ -7,10 +7,10 @@ use CodeIgniter\HTTP\RedirectResponse; use CodeIgniter\Validation\Exceptions\ValidationException; /** - * AbstractCrudController + * AbstractCRUDController * 단일 레코드 생성(C), 조회(R), 수정(U), 삭제(D) 로직을 담당합니다. (SRP: Single Record Management) */ -abstract class AbstractCrudController extends AbstractWebController +abstract class AbstractCRUDController extends AbstractWebController { // --- 생성 (Create) --- diff --git a/app/Controllers/CommonController.php b/app/Controllers/CommonController.php index 78cb4d3..a2cc1d5 100644 --- a/app/Controllers/CommonController.php +++ b/app/Controllers/CommonController.php @@ -15,7 +15,7 @@ use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf; * CommonController * 목록(index), 일괄작업(batchjob), 일괄삭제, 다운로드 로직을 담당합니다. (SRP: Collection Management) */ -abstract class CommonController extends AbstractCrudController +abstract class CommonController extends AbstractCRUDController { // --- 목록 (Index / List) 관련 --- diff --git a/app/Controllers/CommonControllerV1.php b/app/Controllers/CommonControllerV1.php index 55da8e9..66e74fa 100644 --- a/app/Controllers/CommonControllerV1.php +++ b/app/Controllers/CommonControllerV1.php @@ -5,7 +5,6 @@ namespace App\Controllers; use App\Entities\CommonEntity; use App\Controllers\BaseController; use App\Libraries\AuthContext; -use App\Traits\LogTrait; use CodeIgniter\HTTP\RedirectResponse; use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; @@ -15,12 +14,9 @@ use CodeIgniter\HTTP\DownloadResponse; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Reader\Html; use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf; -use CodeIgniter\API\ResponseTrait; abstract class CommonControllerV1 extends BaseController { - use LogTrait; - private array $_action_paths = []; private array $_viewDatas = []; protected $service = null; diff --git a/app/DTOs/MylogDTO.php b/app/DTOs/MylogDTO.php index d83e9e7..4df4a32 100644 --- a/app/DTOs/MylogDTO.php +++ b/app/DTOs/MylogDTO.php @@ -5,8 +5,10 @@ namespace App\DTOs; class MylogDTO extends CommonDTO { public ?int $uid = null; + public ?int $user_uid = null; public ?string $title = null; public ?string $content = null; + public ?string $status = null; public function __construct(array $datas = []) { @@ -22,8 +24,10 @@ class MylogDTO extends CommonDTO { return [ 'uid' => $this->uid, + 'user_uid' => $this->user_uid, 'title' => $this->title, 'content' => $this->content, + 'status' => $this->status, ]; } } diff --git a/app/Forms/MylogForm.php b/app/Forms/MylogForm.php index 6f0c973..2cf61dc 100644 --- a/app/Forms/MylogForm.php +++ b/app/Forms/MylogForm.php @@ -14,8 +14,12 @@ class MylogForm extends CommonForm public function getFormRule(string $action, string $field): string { switch ($field) { + case "user_uid": + $rule = "required|int"; + break; case "title": - $rule = "required|trime|string"; + case "status": + $rule = "required|trim|string"; break; case "content": $rule = "permit_empty|trim|string"; diff --git a/app/Language/en/Mylog.php b/app/Language/en/Mylog.php index 7326bad..2cc6280 100644 --- a/app/Language/en/Mylog.php +++ b/app/Language/en/Mylog.php @@ -3,8 +3,10 @@ return [ 'title' => "작업Log", 'label' => [ 'uid' => "번호", + 'user_uid' => "관리자", 'title' => "제목", 'content' => "내용", + 'status' => "상태", 'updated_at' => "수정일", 'created_at' => "작성일", 'deleted_at' => "삭제일", diff --git a/app/Libraries/OperationContext.php b/app/Libraries/OperationContext.php new file mode 100644 index 0000000..08f5b33 --- /dev/null +++ b/app/Libraries/OperationContext.php @@ -0,0 +1,21 @@ +action = $action; + $this->auth = $auth; + $this->datas = $datas; + } +} diff --git a/app/Libraries/PipelineStep.php b/app/Libraries/PipelineStep.php new file mode 100644 index 0000000..6c1a054 --- /dev/null +++ b/app/Libraries/PipelineStep.php @@ -0,0 +1,48 @@ +logService = $logService; + } + + /** + * 서비스 단계(Step) 배열을 받아 순차적으로 실행합니다. + * @param array $steps 실행할 PipelineStep 객체들의 배열 + * @param OperationContext $context 초기 컨텍스트 + * @return OperationContext 최종 컨텍스트 + */ + public function run(array $steps, OperationContext $context): OperationContext + { + $db = \Config\Database::connect(); + $db->transBegin(); + try { + // 1. Log START: 파이프라인 시작 로깅 + $this->logService->log($context, 'START'); + // 2. 단계(Steps) 순차 실행 + foreach ($steps as $step) { + // 각 단계는 컨텍스트를 받아 처리하고 업데이트된 컨텍스트를 반환합니다. + $context = $step->handle($context); + } + // 3. Log SUCCESS: 모든 단계 성공 로깅 + $this->logService->log($context, 'SUCCESS'); + return $context; + } catch (\Throwable $e) { + $db->transRollback(); + // 4. Log FAILURE: 오류 발생 시 즉시 로깅 + $context->error = $e; + $this->logService->log($context, 'FAILURE'); + // 5. 오류를 상위 계층으로 전파 + throw $e; + } + } +} diff --git a/app/Libraries/PipelineStepInterface.php b/app/Libraries/PipelineStepInterface.php new file mode 100644 index 0000000..4620aa8 --- /dev/null +++ b/app/Libraries/PipelineStepInterface.php @@ -0,0 +1,16 @@ +_helper; } + /** + * LogService는 START/SUCCESS/FAILURE 로그를 기록합니다. + * LogService는 파이프라인의 시작과 끝에서만 Executor에 의해 직접 호출됩니다. + */ + public function log(OperationContext $context, string $status): void + { + if ($status === STATUS['FAILED'] && $context->error) { + $message = "[{$context->action}_{$status}] Error: {$context->error->getMessage()}"; + } else { + $message = "[{$context->action}_{$status}] Steps Executed: " . count($context->pipelineDatas); + } + log_message('debug', $message); + $formDatas = []; + $formDatas['user'] = $context->auth->getUID(); + $this->create(new MylogDTO()); + } + + // PipelineStep 구현은 이 예시에서는 사용하지 않습니다. (로그는 Executor가 감쌈) + public function handle(OperationContext $context): OperationContext + { + return $context; + } + //기본 기능부분 protected function getEntity_process(mixed $entity): MylogEntity { diff --git a/app/Services/PipelineRunnerService.php b/app/Services/PipelineRunnerService.php deleted file mode 100644 index 867a75a..0000000 --- a/app/Services/PipelineRunnerService.php +++ /dev/null @@ -1,53 +0,0 @@ -steps[] = $step; - return $this; - } - - /** - * 파이프라인의 모든 단계를 순차적으로 실행하고 컨텍스트를 전달합니다. - */ - public function run(array $initialContext = []): array - { - $db = \Config\Database::connect(); - $db->transBegin(); // 1. 트랜잭션 시작 (원자성 확보) - try { - // 1. 초기값 선언 - $context = $initialContext; - // 2. 파이프라인 정의 및 Step에 서비스 인스턴스 주입 - $this->log_info("--- ✅ Step 시작 ---\n"); - foreach ($this->steps as $step) { - if (!method_exists($step, 'execute')) { - throw new \RuntimeException(sprintf("[%s]에서 'execute' 없습니다.", get_class($step))); - } - // 3. 파이프라인 실행 - // 이전 단계에서 반환된 context를 현재 단계에 전달하고, 현재 단계의 반환값을 다음 context로 설정합니다. - $context = $step->execute($context); - } - $this->log_info("--- ✅ Step 완료 ---\n"); - } catch (Throwable $e) { - $db->transRollback(); - // 여기서 $e는 Step에서 발생한 원본 Throwable 객체입니다. - $this->log_error(static::class . "에서 오류발생:" . $e->getMessage()); - } - return $context; - } -}