76 lines
2.3 KiB
PHP
76 lines
2.3 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Console\Commands;
|
||
|
|
|
||
|
|
use Illuminate\Console\Command;
|
||
|
|
use Illuminate\Support\Facades\Artisan;
|
||
|
|
use Illuminate\Support\Facades\File;
|
||
|
|
|
||
|
|
class MakeModelWithRelationships extends Command
|
||
|
|
{
|
||
|
|
protected $signature = 'make:model-with-docs {name} {--migration} {--controller} {--resource}';
|
||
|
|
protected $description = '모델 생성 후 자동으로 관계 문서 업데이트';
|
||
|
|
|
||
|
|
public function handle()
|
||
|
|
{
|
||
|
|
$modelName = $this->argument('name');
|
||
|
|
|
||
|
|
// 기본 모델 생성
|
||
|
|
$options = [];
|
||
|
|
if ($this->option('migration')) $options['--migration'] = true;
|
||
|
|
if ($this->option('controller')) $options['--controller'] = true;
|
||
|
|
if ($this->option('resource')) $options['--resource'] = true;
|
||
|
|
|
||
|
|
Artisan::call('make:model', array_merge(['name' => $modelName], $options));
|
||
|
|
$this->info("✅ 모델 생성 완료: {$modelName}");
|
||
|
|
|
||
|
|
// 관계 템플릿 추가
|
||
|
|
$this->addRelationshipTemplate($modelName);
|
||
|
|
|
||
|
|
// 논리적 관계 문서 업데이트
|
||
|
|
Artisan::call('db:update-relationships');
|
||
|
|
$this->info('✅ 논리적 관계 문서 업데이트 완료');
|
||
|
|
}
|
||
|
|
|
||
|
|
private function addRelationshipTemplate(string $modelName): void
|
||
|
|
{
|
||
|
|
$modelPath = app_path("Models/{$modelName}.php");
|
||
|
|
|
||
|
|
if (!File::exists($modelPath)) {
|
||
|
|
$this->error("모델 파일을 찾을 수 없습니다: {$modelPath}");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$content = File::get($modelPath);
|
||
|
|
|
||
|
|
// 관계 메서드 템플릿 추가
|
||
|
|
$template = "
|
||
|
|
// ========================================
|
||
|
|
// 관계 메서드 (Relationships)
|
||
|
|
// ========================================
|
||
|
|
//
|
||
|
|
// 예시:
|
||
|
|
// public function category()
|
||
|
|
// {
|
||
|
|
// return \$this->belongsTo(Category::class);
|
||
|
|
// }
|
||
|
|
//
|
||
|
|
// public function items()
|
||
|
|
// {
|
||
|
|
// return \$this->hasMany(Item::class);
|
||
|
|
// }
|
||
|
|
//
|
||
|
|
// ⚠️ 새 관계 추가 후 'php artisan db:update-relationships' 실행
|
||
|
|
";
|
||
|
|
|
||
|
|
// 클래스 끝 부분에 템플릿 삽입
|
||
|
|
$content = str_replace(
|
||
|
|
'}' . PHP_EOL,
|
||
|
|
$template . PHP_EOL . '}' . PHP_EOL,
|
||
|
|
$content
|
||
|
|
);
|
||
|
|
|
||
|
|
File::put($modelPath, $content);
|
||
|
|
$this->info("📝 관계 템플릿 추가: {$modelName}");
|
||
|
|
}
|
||
|
|
}
|