trafficmonitor init...2
This commit is contained in:
parent
b6f6c90452
commit
83f54434b8
@ -7,10 +7,10 @@ use CodeIgniter\HTTP\RedirectResponse;
|
|||||||
use CodeIgniter\Validation\Exceptions\ValidationException;
|
use CodeIgniter\Validation\Exceptions\ValidationException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AbstractCrudController
|
* AbstractCRUDController
|
||||||
* 단일 레코드 생성(C), 조회(R), 수정(U), 삭제(D) 로직을 담당합니다. (SRP: Single Record Management)
|
* 단일 레코드 생성(C), 조회(R), 수정(U), 삭제(D) 로직을 담당합니다. (SRP: Single Record Management)
|
||||||
*/
|
*/
|
||||||
abstract class AbstractCrudController extends AbstractWebController
|
abstract class AbstractCRUDController extends AbstractWebController
|
||||||
{
|
{
|
||||||
// --- 생성 (Create) ---
|
// --- 생성 (Create) ---
|
||||||
|
|
||||||
@ -15,7 +15,7 @@ use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
|
|||||||
* CommonController
|
* CommonController
|
||||||
* 목록(index), 일괄작업(batchjob), 일괄삭제, 다운로드 로직을 담당합니다. (SRP: Collection Management)
|
* 목록(index), 일괄작업(batchjob), 일괄삭제, 다운로드 로직을 담당합니다. (SRP: Collection Management)
|
||||||
*/
|
*/
|
||||||
abstract class CommonController extends AbstractCrudController
|
abstract class CommonController extends AbstractCRUDController
|
||||||
{
|
{
|
||||||
// --- 목록 (Index / List) 관련 ---
|
// --- 목록 (Index / List) 관련 ---
|
||||||
|
|
||||||
|
|||||||
@ -5,7 +5,6 @@ namespace App\Controllers;
|
|||||||
use App\Entities\CommonEntity;
|
use App\Entities\CommonEntity;
|
||||||
use App\Controllers\BaseController;
|
use App\Controllers\BaseController;
|
||||||
use App\Libraries\AuthContext;
|
use App\Libraries\AuthContext;
|
||||||
use App\Traits\LogTrait;
|
|
||||||
use CodeIgniter\HTTP\RedirectResponse;
|
use CodeIgniter\HTTP\RedirectResponse;
|
||||||
use CodeIgniter\HTTP\RequestInterface;
|
use CodeIgniter\HTTP\RequestInterface;
|
||||||
use CodeIgniter\HTTP\ResponseInterface;
|
use CodeIgniter\HTTP\ResponseInterface;
|
||||||
@ -15,12 +14,9 @@ use CodeIgniter\HTTP\DownloadResponse;
|
|||||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||||
use PhpOffice\PhpSpreadsheet\Reader\Html;
|
use PhpOffice\PhpSpreadsheet\Reader\Html;
|
||||||
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
|
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
|
||||||
use CodeIgniter\API\ResponseTrait;
|
|
||||||
|
|
||||||
abstract class CommonControllerV1 extends BaseController
|
abstract class CommonControllerV1 extends BaseController
|
||||||
{
|
{
|
||||||
use LogTrait;
|
|
||||||
|
|
||||||
private array $_action_paths = [];
|
private array $_action_paths = [];
|
||||||
private array $_viewDatas = [];
|
private array $_viewDatas = [];
|
||||||
protected $service = null;
|
protected $service = null;
|
||||||
|
|||||||
@ -5,8 +5,10 @@ namespace App\DTOs;
|
|||||||
class MylogDTO extends CommonDTO
|
class MylogDTO extends CommonDTO
|
||||||
{
|
{
|
||||||
public ?int $uid = null;
|
public ?int $uid = null;
|
||||||
|
public ?int $user_uid = null;
|
||||||
public ?string $title = null;
|
public ?string $title = null;
|
||||||
public ?string $content = null;
|
public ?string $content = null;
|
||||||
|
public ?string $status = null;
|
||||||
|
|
||||||
public function __construct(array $datas = [])
|
public function __construct(array $datas = [])
|
||||||
{
|
{
|
||||||
@ -22,8 +24,10 @@ class MylogDTO extends CommonDTO
|
|||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'uid' => $this->uid,
|
'uid' => $this->uid,
|
||||||
|
'user_uid' => $this->user_uid,
|
||||||
'title' => $this->title,
|
'title' => $this->title,
|
||||||
'content' => $this->content,
|
'content' => $this->content,
|
||||||
|
'status' => $this->status,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,8 +14,12 @@ class MylogForm extends CommonForm
|
|||||||
public function getFormRule(string $action, string $field): string
|
public function getFormRule(string $action, string $field): string
|
||||||
{
|
{
|
||||||
switch ($field) {
|
switch ($field) {
|
||||||
|
case "user_uid":
|
||||||
|
$rule = "required|int";
|
||||||
|
break;
|
||||||
case "title":
|
case "title":
|
||||||
$rule = "required|trime|string";
|
case "status":
|
||||||
|
$rule = "required|trim|string";
|
||||||
break;
|
break;
|
||||||
case "content":
|
case "content":
|
||||||
$rule = "permit_empty|trim|string";
|
$rule = "permit_empty|trim|string";
|
||||||
|
|||||||
@ -3,8 +3,10 @@ return [
|
|||||||
'title' => "작업Log",
|
'title' => "작업Log",
|
||||||
'label' => [
|
'label' => [
|
||||||
'uid' => "번호",
|
'uid' => "번호",
|
||||||
|
'user_uid' => "관리자",
|
||||||
'title' => "제목",
|
'title' => "제목",
|
||||||
'content' => "내용",
|
'content' => "내용",
|
||||||
|
'status' => "상태",
|
||||||
'updated_at' => "수정일",
|
'updated_at' => "수정일",
|
||||||
'created_at' => "작성일",
|
'created_at' => "작성일",
|
||||||
'deleted_at' => "삭제일",
|
'deleted_at' => "삭제일",
|
||||||
|
|||||||
21
app/Libraries/OperationContext.php
Normal file
21
app/Libraries/OperationContext.php
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Libraries;
|
||||||
|
|
||||||
|
use App\Libraries\AuthContext;
|
||||||
|
|
||||||
|
class OperationContext
|
||||||
|
{
|
||||||
|
public string $action;
|
||||||
|
public AuthContext $auth;
|
||||||
|
public array $datas;
|
||||||
|
public array $pipelineDatas = []; // 파이프라인 단계별 데이터를 저장할 공간
|
||||||
|
public ?\Throwable $error = null;
|
||||||
|
|
||||||
|
public function __construct(string $action, array $datas, AuthContext $auth)
|
||||||
|
{
|
||||||
|
$this->action = $action;
|
||||||
|
$this->auth = $auth;
|
||||||
|
$this->datas = $datas;
|
||||||
|
}
|
||||||
|
}
|
||||||
48
app/Libraries/PipelineStep.php
Normal file
48
app/Libraries/PipelineStep.php
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Libraries;
|
||||||
|
|
||||||
|
use App\Libraries\OperationContext;
|
||||||
|
use App\Services\MylogService;
|
||||||
|
|
||||||
|
class PipelineStep
|
||||||
|
{
|
||||||
|
protected MylogService $logService;
|
||||||
|
|
||||||
|
// CI4의 DI (의존성 주입)를 통해 LogService만 받습니다.
|
||||||
|
public function __construct(MylogService $logService)
|
||||||
|
{
|
||||||
|
$this->logService = $logService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 서비스 단계(Step) 배열을 받아 순차적으로 실행합니다.
|
||||||
|
* @param array<PipelineStep> $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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
16
app/Libraries/PipelineStepInterface.php
Normal file
16
app/Libraries/PipelineStepInterface.php
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Libraries;
|
||||||
|
|
||||||
|
use App\Libraries\OperationContext;
|
||||||
|
|
||||||
|
interface PipelineStepInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 각 서비스가 파이프라인의 한 단계로 동작하기 위한 표준 메서드입니다.
|
||||||
|
* 컨텍스트를 받아 처리하고, 다음 단계로 전달하기 위해 컨텍스트를 반환합니다.
|
||||||
|
* @param OperationContext $context
|
||||||
|
* @return OperationContext
|
||||||
|
*/
|
||||||
|
public function handle(OperationContext $context): OperationContext;
|
||||||
|
}
|
||||||
@ -14,8 +14,10 @@ class MylogModel extends CommonModel
|
|||||||
protected $returnType = MylogEntity::class;
|
protected $returnType = MylogEntity::class;
|
||||||
protected $allowedFields = [
|
protected $allowedFields = [
|
||||||
"uid",
|
"uid",
|
||||||
|
"user_uid",
|
||||||
"title",
|
"title",
|
||||||
"content",
|
"content",
|
||||||
|
"status",
|
||||||
"updated_at"
|
"updated_at"
|
||||||
];
|
];
|
||||||
public function __construct()
|
public function __construct()
|
||||||
|
|||||||
@ -6,10 +6,12 @@ use App\DTOs\MylogDTO;
|
|||||||
use App\Entities\MylogEntity;
|
use App\Entities\MylogEntity;
|
||||||
use App\Forms\MylogForm;
|
use App\Forms\MylogForm;
|
||||||
use App\Helpers\MylogHelper;
|
use App\Helpers\MylogHelper;
|
||||||
|
use App\Libraries\OperationContext;
|
||||||
|
use App\Libraries\PipelineStepInterface;
|
||||||
use App\Models\MylogModel;
|
use App\Models\MylogModel;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
|
|
||||||
class MylogService extends CommonService
|
class MylogService extends CommonService implements PipelineStepInterface
|
||||||
{
|
{
|
||||||
private $_form = null;
|
private $_form = null;
|
||||||
private $_helper = null;
|
private $_helper = null;
|
||||||
@ -50,6 +52,29 @@ class MylogService extends CommonService
|
|||||||
}
|
}
|
||||||
return $this->_helper;
|
return $this->_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
|
protected function getEntity_process(mixed $entity): MylogEntity
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,53 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Services;
|
|
||||||
|
|
||||||
use App\Traits\LogTrait;
|
|
||||||
use Throwable;
|
|
||||||
|
|
||||||
use function PHPUnit\Framework\throwException;
|
|
||||||
|
|
||||||
class PipelineRunnerService
|
|
||||||
{
|
|
||||||
use LogTrait;
|
|
||||||
protected array $steps = [];
|
|
||||||
/**
|
|
||||||
* 파이프라인에 처리 단계를 추가합니다.
|
|
||||||
* @param object $stepStep 클래스의 인스턴스
|
|
||||||
*/
|
|
||||||
public function addStep(object $step): self
|
|
||||||
{
|
|
||||||
// 단계 클래스를 저장합니다.
|
|
||||||
$this->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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user