dbmsv4/app/Controllers/AbstractCRUDController.php
2025-11-24 11:11:53 +09:00

209 lines
8.0 KiB
PHP

<?php
namespace App\Controllers;
use App\Entities\CommonEntity;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\Validation\Exceptions\ValidationException;
use RuntimeException;
/**
* AbstractCRUDController
* 단일 레코드 생성(C), 조회(R), 수정(U), 삭제(D) 로직을 담당합니다. (SRP: Single Record Management)
*/
abstract class AbstractCRUDController extends AbstractWebController
{
// 💡 핵심 1: 각 자식 클래스가 사용할 Entity 클래스 경로를 반환하도록 강제
// 이 메서드는 자식 클래스에서 반드시 구현되어야 합니다.
abstract protected function getEntityClass(): string;
// --- 생성 (Create) ---
protected function create_form_process(array $formDatas = []): array
{
// Form Default값 설정 (오버라이드 포인트)
return $formDatas;
}
protected function create_form_result_process(string $action): string|RedirectResponse
{
return $this->action_render_process($action, $this->getViewDatas(), $this->request->getVar('ActionTemplate'));
}
public function create_form(): string|RedirectResponse
{
try {
$action = __FUNCTION__;
$this->action_init_process($action);
$this->addViewDatas('formDatas', $this->create_form_process());
return $this->create_form_result_process($action);
} catch (\Exception $e) {
return $this->action_redirect_process('error', "{$this->getTitle()}에서 생성폼 오류:" . $e->getMessage());
}
}
protected function create_process(): CommonEntity
{
// POST 데이터를 DTO 객체로 변환 (getPost()는 POST 요청 본문만 가져옵니다.)
$dto = $this->service->createDTO($this->request->getPost());
return $this->service->create($dto);
}
protected function create_result_process(CommonEntity $entity): string|RedirectResponse
{
return redirect()->to(
'/' . implode('/', [...$this->getActionPaths(), 'view']) . '/' . $entity->getPK()
)->with('message', "{$this->getTitle()}에서 {$entity->getTitle()} 생성이 완료되었습니다.");
}
final public function create(): string|RedirectResponse
{
try {
$action = __FUNCTION__;
$this->action_init_process($action);
$entity = $this->create_process();
// 💡 동적으로 가져온 Entity 클래스 이름으로 instanceof 검사
$entityClass = $this->getEntityClass();
if (!$entity instanceof $entityClass) {
throw new RuntimeException(__METHOD__ . "에서 오류발생:Return Type은 {$entityClass}만 가능");
}
return $this->create_result_process($entity);
} catch (ValidationException $e) {
return $this->action_redirect_process('error', "{$this->getTitle()}에서 생성 검증오류:" . $e->getMessage());
} catch (\Exception $e) {
return $this->action_redirect_process('error', "{$this->getTitle()}에서 생성 오류:" . $e->getMessage());
}
}
// --- 수정 (Modify) ---
protected function modify_form_process($uid): CommonEntity
{
if (!$uid) {
throw new \Exception("{$this->getTitle()}에 번호가 정의 되지 않았습니다.");
}
$entity = $this->service->getEntity($uid);
if (!$entity instanceof CommonEntity) {
throw new \Exception("{$uid}에 해당하는 {$this->getTitle()}을 찾을수 없습니다.");
}
return $entity;
}
protected function modify_form_result_process(string $action): string|RedirectResponse
{
return $this->action_render_process($action, $this->getViewDatas(), $this->request->getVar('ActionTemplate'));
}
final public function modify_form($uid): string|RedirectResponse
{
try {
$entity = $this->modify_form_process($uid);
// 💡 동적으로 가져온 Entity 클래스 이름으로 instanceof 검사
$entityClass = $this->getEntityClass();
if (!$entity instanceof $entityClass) {
throw new RuntimeException(__METHOD__ . "에서 오류발생:Return Type은 {$entityClass}만 가능");
}
$this->addViewDatas('entity', $entity);
$action = __FUNCTION__;
$this->action_init_process($action, $entity);
return $this->modify_form_result_process($action);
} catch (\Exception $e) {
return $this->action_redirect_process('error', "{$this->getTitle()}에서 {$uid} 수정폼 오류:" . $e->getMessage());
}
}
protected function modify_process($uid): CommonEntity
{
// POST 데이터를 DTO 객체로 변환
$dto = $this->service->createDTO($this->request->getPost());
return $this->service->modify($uid, $dto);
}
protected function modify_result_process(CommonEntity $entity): string|RedirectResponse
{
return redirect()->to(
'/' . implode('/', [...$this->getActionPaths(), 'view']) . '/' . $entity->getPK()
)->with('message', "{$this->getTitle()}에서 {$entity->getTitle()} 수정이 완료되었습니다.");
}
final public function modify($uid): string|RedirectResponse
{
try {
$action = __FUNCTION__;
$this->action_init_process($action);
$entity = $this->modify_process($uid);
// 💡 동적으로 가져온 Entity 클래스 이름으로 instanceof 검사
$entityClass = $this->getEntityClass();
if (!$entity instanceof $entityClass) {
throw new RuntimeException(__METHOD__ . "에서 오류발생:Return Type은 {$entityClass}만 가능");
}
$this->addViewDatas('entity', $entity);
return $this->modify_result_process($entity);
} catch (ValidationException $e) {
return $this->action_redirect_process('error', "{$this->getTitle()}에서 {$uid} 수정 검증오류:" . $e->getMessage());
} catch (\Exception $e) {
return $this->action_redirect_process('error', "{$this->getTitle()}에서 {$uid} 수정 오류:" . $e->getMessage());
}
}
// --- 삭제 (Delete) ---
protected function delete_process($uid): CommonEntity
{
return $this->service->delete($uid);
}
protected function delete_result_process(CommonEntity $entity): string|RedirectResponse
{
return $this->action_redirect_process('info', "{$this->getTitle()}에서 {$entity->getTitle()} 삭제가 완료되었습니다.");
}
final public function delete($uid): RedirectResponse
{
try {
$entity = $this->delete_process($uid);
// 💡 동적으로 가져온 Entity 클래스 이름으로 instanceof 검사
$entityClass = $this->getEntityClass();
if (!$entity instanceof $entityClass) {
throw new RuntimeException(__METHOD__ . "에서 오류발생:Return Type은 {$entityClass}만 가능");
}
return $this->delete_result_process($entity);
} catch (\Exception $e) {
return $this->action_redirect_process('error', "{$this->getTitle()}에서 {$uid} 삭제 오류:" . $e->getMessage());
}
}
// --- 상세보기 (View) ---
protected function view_process($uid): CommonEntity
{
if (!$uid) {
throw new \Exception("{$this->getTitle()}에 번호가 정의 되지 않았습니다.");
}
$entity = $this->service->getEntity($uid);
if (!$entity instanceof CommonEntity) {
throw new \Exception("{$uid}에 해당하는 {$this->getTitle()}을 찾을수 없습니다.");
}
return $entity;
}
protected function view_result_process(string $action): string
{
return $this->action_render_process($action, $this->getViewDatas(), $this->request->getVar('ActionTemplate'));
}
final public function view($uid): string|RedirectResponse
{
try {
$action = __FUNCTION__;
$this->action_init_process($action);
$entity = $this->view_process($uid);
// 💡 동적으로 가져온 Entity 클래스 이름으로 instanceof 검사
$entityClass = $this->getEntityClass();
if (!$entity instanceof $entityClass) {
throw new RuntimeException(__METHOD__ . "에서 오류발생:Return Type은 {$entityClass}만 가능");
}
$this->addViewDatas('entity', $entity);
return $this->view_result_process($action);
} catch (\Exception $e) {
return $this->action_redirect_process('error', "{$this->getTitle()}에서 {$uid} 상세보기 오류:" . $e->getMessage());
}
}
}