From e0ab286a262e67d4f2f0a55d2562a163984ddd9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0?= Date: Mon, 2 Mar 2026 12:08:36 +0900 Subject: [PATCH] daemon-idc init --- app/Controllers/AbstractCRUDController.php | 233 ++++++++------------- 1 file changed, 92 insertions(+), 141 deletions(-) diff --git a/app/Controllers/AbstractCRUDController.php b/app/Controllers/AbstractCRUDController.php index 44f4e88..f95e60a 100644 --- a/app/Controllers/AbstractCRUDController.php +++ b/app/Controllers/AbstractCRUDController.php @@ -1,19 +1,10 @@ ' . $action . "에서 {$this->getTitle()}에 번호가 정의 되지 않았습니다."); - } - } - - protected function assertEntityClass(CommonEntity $entity, string $action): void - { - $entityClass = $this->service->getEntityClass(); - if (!$entity instanceof $entityClass) { - throw new RuntimeException(static::class . '->' . $action . "에서 오류발생:Return Type은 {$entityClass}만 가능"); - } - } - - protected function buildViewRedirectUrl(CommonEntity $entity): string - { - return '/' . implode('/', [...$this->getActionPaths(), 'view']) . '/' . $entity->getPK(); - } - - protected function buildListRedirectUrl(): string - { - return implode(DIRECTORY_SEPARATOR, $this->getActionPaths()); - } - - // ========================================================= - // Create Form - // ========================================================= - + // 💡 핵심 1: 각 자식 클래스가 사용할 Entity 클래스 경로를 반환하도록 강제 + // 이 메서드는 자식 클래스에서 반드시 구현되어야 합니다. + // --- 생성 (Create) --- protected function create_form_process(array $formDatas = []): array { - return $this->request->getVar(); + //초기 기본 Default값 지정 + $formDatas = $this->request->getVar(); + return $formDatas; } - public function create_form(): string|RedirectResponse|ResponseInterface + protected function create_form_result_process(string $action): string|RedirectResponse { - $action = __FUNCTION__; + return $this->action_render_process($action, $this->getViewDatas(), $this->request->getVar('ActionTemplate')); + } - return $this->runAction($action, function () use ($action) { + public function create_form(): string|RedirectResponse + { + try { + $action = __FUNCTION__; $formDatas = $this->create_form_process(); $this->action_init_process($action, $formDatas); $this->addViewDatas('formDatas', $formDatas); - return $this->action_render_process($action, $this->getViewDatas(), $this->request->getVar('ActionTemplate')); - }); + return $this->create_form_result_process($action); + } catch (\Throwable $e) { + return $this->action_redirect_process('error', static::class . '->' . __FUNCTION__ . "에서 {$this->getTitle()} 생성폼 오류:" . $e->getMessage()); + } } - - // ========================================================= - // Create - // ========================================================= - protected function create_process(array $formDatas): CommonEntity { return $this->service->create($formDatas); } - final public function create(): RedirectResponse|ResponseInterface|string + protected function create_result_process($entity, ?string $redirect_url = null): string|RedirectResponse { - $action = __FUNCTION__; - - return $this->runAction($action, function () use ($action) { - $this->action_init_process($action); - - $entity = $this->create_process($this->request->getPost()); - $this->assertEntityClass($entity, $action); - - $message = "{$this->getTitle()}에서 {$entity->getTitle()} 생성이 완료되었습니다."; - return $this->okResponse($message, ['id' => $entity->getPK()], $this->buildViewRedirectUrl($entity)); - }); + return $this->action_redirect_process( + 'info', + "{$this->getTitle()}에서 {$entity->getTitle()} 생성이 완료되었습니다.", + $redirect_url ?? '/' . implode('/', [...$this->getActionPaths(), 'view']) . '/' . $entity->getPK() + ); } - // ========================================================= - // Modify Form - // ========================================================= + final public function create(): string|RedirectResponse + { + try { + $action = __FUNCTION__; + $this->action_init_process($action); + $entity = $this->create_process($this->request->getPost()); + // 💡 동적으로 가져온 Entity 클래스 이름으로 instanceof 검사 + $entityClass = $this->service->getEntityClass(); + if (!$entity instanceof $entityClass) { + throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 {$entityClass}만 가능"); + } + return $this->create_result_process($entity); + } catch (\Throwable $e) { + return $this->action_redirect_process('error', static::class . '->' . __FUNCTION__ . "에서 {$this->getTitle()} 생성 오류:" . $e->getMessage()); + } + } + // --- 수정 (Modify) --- protected function modify_form_process($uid): CommonEntity { return $this->service->getEntity($uid); } - protected function modify_form_result_process(string $action): string + 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|ResponseInterface + final public function modify_form($uid): string|RedirectResponse { - $action = __FUNCTION__; - - return $this->runAction($action, function () use ($action, $uid) { - $this->assertUid($uid, $action); - + try { + if (!$uid) { + throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 {$this->getTitle()}에 번호가 정의 되지 않았습니다."); + } $entity = $this->modify_form_process($uid); $this->addViewDatas('entity', $entity); - + $action = __FUNCTION__; + //FormService에서 필요한 기존 데이터를 $entity에서 추출해서 넘김 $this->action_init_process($action, $entity->toArray()); return $this->modify_form_result_process($action); - }); + } catch (\Throwable $e) { + return $this->action_redirect_process('error', static::class . '->' . __FUNCTION__ . "에서 {$this->getTitle()} 수정폼 오류:" . $e->getMessage()); + } } - // ========================================================= - // Modify - // ========================================================= - protected function modify_process($uid, array $formDatas): CommonEntity { return $this->service->modify($uid, $formDatas); } - /** - * 기본 결과 처리 (필요시 자식에서 override) - */ - protected function result_process(CommonEntity $entity, string $message): RedirectResponse|ResponseInterface + protected function modify_result_process($entity, ?string $redirect_url = null): string|RedirectResponse { - return $this->okResponse($message, ['id' => $entity->getPK()], $this->buildViewRedirectUrl($entity)); + return $this->action_redirect_process( + 'info', + "{$this->getTitle()}에서 {$entity->getTitle()} 수정이 완료되었습니다.", + $redirect_url ?? '/' . implode('/', [...$this->getActionPaths(), 'view']) . '/' . $entity->getPK() + ); } - - final public function modify($uid): string|RedirectResponse|ResponseInterface + final public function modify($uid): string|RedirectResponse { - $action = __FUNCTION__; - - return $this->runAction($action, function () use ($action, $uid) { - $this->assertUid($uid, $action); - + try { + if (!$uid) { + throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 {$this->getTitle()}에 번호가 정의 되지 않았습니다."); + } + $action = __FUNCTION__; $this->action_init_process($action); - $entity = $this->modify_process($uid, $this->request->getPost()); - $this->assertEntityClass($entity, $action); - $this->addViewDatas('entity', $entity); - return $this->result_process($entity, "{$this->getTitle()}에서 {$entity->getTitle()} 수정이 완료되었습니다."); - }); + return $this->modify_result_process($entity); + } catch (\Throwable $e) { + return $this->action_redirect_process('error', static::class . '->' . __FUNCTION__ . "에서 {$this->getTitle()} 수정 오류:" . $e->getMessage()); + } } - // ========================================================= - // Delete - // ========================================================= - + // --- 삭제 (Delete) --- protected function delete_process($uid): CommonEntity { return $this->service->delete($uid); } - - protected function delete_result_process(CommonEntity $entity, ?string $redirect_url = null): RedirectResponse|ResponseInterface + protected function delete_result_process($entity, ?string $redirect_url = null): string|RedirectResponse { - // AJAX면 action_redirect_process가 JSON으로 자동 변환 - $redirect_url = $redirect_url ?? $this->buildListRedirectUrl(); return $this->action_redirect_process('info', "{$this->getTitle()}에서 {$entity->getTitle()} 삭제가 완료되었습니다.", $redirect_url); } - - final public function delete($uid): RedirectResponse|ResponseInterface|string + final public function delete($uid): RedirectResponse { - $action = __FUNCTION__; - - return $this->runAction($action, function () use ($action, $uid) { - $this->assertUid($uid, $action); - - $this->action_init_process($action); - - // 삭제 전 존재 확인(기존 로직 유지) - $this->service->getEntity($uid); - + try { + if (!$uid) { + throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 {$this->getTitle()}에 번호가 정의 되지 않았습니다."); + } + $entity = $this->service->getEntity($uid); + //Delete처리 $entity = $this->delete_process($uid); - $this->assertEntityClass($entity, $action); - return $this->delete_result_process($entity); - }); + } catch (\Throwable $e) { + return $this->action_redirect_process('error', static::class . '->' . __FUNCTION__ . "에서 {$this->getTitle()} 삭제 오류:" . $e->getMessage()); + } } - // ========================================================= - // View - // ========================================================= - + // --- 상세보기 (View) --- protected function view_process($uid): CommonEntity { return $this->service->getEntity($uid); } - 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|ResponseInterface + final public function view($uid): string|RedirectResponse { - $action = __FUNCTION__; - - return $this->runAction($action, function () use ($action, $uid) { - $this->assertUid($uid, $action); - + try { + if (!$uid) { + throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 {$this->getTitle()}에 번호가 정의 되지 않았습니다."); + } + //View처리 $entity = $this->view_process($uid); + $action = __FUNCTION__; + //FormService에서 필요한 기존 데이터를 $entity에서 추출해서 넘김 $this->action_init_process($action, $entity->toArray()); $this->addViewDatas('entity', $entity); - return $this->view_result_process($action); - }); + } catch (\Throwable $e) { + return $this->action_redirect_process('error', static::class . '->' . __FUNCTION__ . "에서 {$this->getTitle()} 상세보기 오류:" . $e->getMessage()); + } } }