|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Mrdebug\Crudgen\Console; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | + |
| 7 | +use Illuminate\Support\Facades\File; |
| 8 | +use Illuminate\Support\Str; |
| 9 | +use Mrdebug\Crudgen\Exceptions\ConsoleException; |
| 10 | +use Mrdebug\Crudgen\Services\Commentable\EditCommentableView; |
| 11 | +use Mrdebug\Crudgen\Services\Commentable\MakeCommentableRequestService; |
| 12 | +use Mrdebug\Crudgen\Services\Commentable\MakeCommentableControllerService; |
| 13 | +use Mrdebug\Crudgen\Services\MakeGlobalService; |
| 14 | +use Mrdebug\Crudgen\Services\MakeMigrationService; |
| 15 | +use Mrdebug\Crudgen\Services\MakeModelService; |
| 16 | +use Mrdebug\Crudgen\Services\PathsAndNamespacesService; |
| 17 | + |
| 18 | +class MakeCommentable extends Command |
| 19 | +{ |
| 20 | + /** |
| 21 | + * The name and signature of the console command. |
| 22 | + * |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + protected $signature = 'make:commentable {commentable_name}'; |
| 26 | + |
| 27 | + /** |
| 28 | + * The console command description. |
| 29 | + * |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + protected $description = 'Add commentable fields to existing view'; |
| 33 | + |
| 34 | + /** |
| 35 | + * Create a new command instance. |
| 36 | + * |
| 37 | + * @return void |
| 38 | + */ |
| 39 | + public MakeCommentableControllerService $makeCommentableControllerService; |
| 40 | + public MakeCommentableRequestService $makeCommentableRequestService; |
| 41 | + public MakeMigrationService $makeMigrationService; |
| 42 | + public MakeModelService $makeModelService; |
| 43 | + public EditCommentableView $editCommentableView; |
| 44 | + public MakeGlobalService $makeGlobalService; |
| 45 | + public PathsAndNamespacesService $pathsAndNamespacesService; |
| 46 | + public string $nameParentModel = ""; |
| 47 | + public string $pathViewCommentable = ""; |
| 48 | + |
| 49 | + public function __construct( |
| 50 | + MakeCommentableControllerService $makeCommentableControllerService, |
| 51 | + MakeCommentableRequestService $makeCommentableRequestService, |
| 52 | + MakeMigrationService $makeMigrationService, |
| 53 | + MakeModelService $makeModelService, |
| 54 | + EditCommentableView $editCommentableView, |
| 55 | + MakeGlobalService $makeGlobalService, |
| 56 | + PathsAndNamespacesService $pathsAndNamespacesService, |
| 57 | + ) |
| 58 | + { |
| 59 | + parent::__construct(); |
| 60 | + $this->makeCommentableControllerService = $makeCommentableControllerService; |
| 61 | + $this->makeCommentableRequestService = $makeCommentableRequestService; |
| 62 | + $this->makeMigrationService = $makeMigrationService; |
| 63 | + $this->makeModelService = $makeModelService; |
| 64 | + $this->editCommentableView = $editCommentableView; |
| 65 | + $this->makeGlobalService = $makeGlobalService; |
| 66 | + $this->pathsAndNamespacesService = $pathsAndNamespacesService; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Execute the console command. |
| 71 | + * |
| 72 | + * @return mixed |
| 73 | + */ |
| 74 | + public function handle() |
| 75 | + { |
| 76 | + // we create our variables to respect the naming conventions |
| 77 | + $commentableName = ucfirst($this->argument('commentable_name')); |
| 78 | + $namingConvention = $this->makeGlobalService->getCommentableNamingConvention($commentableName); |
| 79 | + $laravelNamespace = $this->laravel->getNamespace(); |
| 80 | + |
| 81 | + |
| 82 | + /* ************************************************************************* |
| 83 | +
|
| 84 | + REQUEST |
| 85 | +
|
| 86 | + ************************************************************************* */ |
| 87 | + |
| 88 | + $this->makeCommentableRequestService->makeCommentableCompleteRequestFile($namingConvention, $laravelNamespace); |
| 89 | + |
| 90 | + /* ************************************************************************* |
| 91 | +
|
| 92 | + MODEL |
| 93 | +
|
| 94 | + ************************************************************************* */ |
| 95 | + |
| 96 | + if(!File::exists($this->pathsAndNamespacesService->getRealpathBaseModel())) |
| 97 | + File::makeDirectory($this->pathsAndNamespacesService->getRealpathBaseModel()); |
| 98 | + |
| 99 | + // we create our model |
| 100 | + $this->setNameModelRelationship($namingConvention); |
| 101 | + |
| 102 | + /* ************************************************************************* |
| 103 | +
|
| 104 | + CONTROLLER |
| 105 | +
|
| 106 | + ************************************************************************* */ |
| 107 | + |
| 108 | + $namingConventionParent = $this->makeGlobalService->getCommentableParentModelConvention($this->nameParentModel); |
| 109 | + $this->makeCommentableControllerService->makeCompleteCommentableControllerFile($namingConvention, $laravelNamespace, $namingConventionParent['singular_low_variable_name']); |
| 110 | + |
| 111 | + |
| 112 | + /* ************************************************************************* |
| 113 | +
|
| 114 | + MIGRATION |
| 115 | +
|
| 116 | + ************************************************************************* */ |
| 117 | + |
| 118 | + $columns = ['comment:text']; |
| 119 | + if($this->nameParentModel !== "") |
| 120 | + $columns[]= $this->nameParentModel."_id:integer"; |
| 121 | + $this->makeMigrationService->makeCompleteMigrationFile($namingConvention, $columns); |
| 122 | + |
| 123 | + /* ************************************************************************* |
| 124 | +
|
| 125 | + VIEW |
| 126 | +
|
| 127 | + ************************************************************************* */ |
| 128 | + $this->askChangeView($namingConvention); |
| 129 | + } |
| 130 | + |
| 131 | + private function setNameModelRelationship($namingConvention) |
| 132 | + { |
| 133 | + $type = "belongsTo"; |
| 134 | + $infos = []; |
| 135 | + $singularName = $namingConvention['model_name']; |
| 136 | + $nameOtherModel = $this->ask('What is the name of the other model where you want to add commentable part? ex:Post'); |
| 137 | + |
| 138 | + if($nameOtherModel === null) |
| 139 | + throw new ConsoleException('Please provide a model name'); |
| 140 | + |
| 141 | + $this->nameParentModel = $nameOtherModel; |
| 142 | + |
| 143 | + $correctNameOtherModel = ucfirst(Str::singular($nameOtherModel)); |
| 144 | + $correctNameOtherModelWithNamespace = $this->laravel->getNamespace().'Models\\'.$correctNameOtherModel; |
| 145 | + if($this->confirm('Do you confirm the creation of this relationship? "'.'$this->'.$type.'(\''.$correctNameOtherModelWithNamespace .'\')"')) |
| 146 | + { |
| 147 | + $infos[] = ['name'=>$nameOtherModel, 'type'=>$type]; |
| 148 | + $this->makeModelService->makeCompleteModelFile($infos, $singularName, $namingConvention, $this->laravel->getNamespace()); |
| 149 | + } |
| 150 | + else |
| 151 | + $this->setNameModelRelationship($namingConvention); |
| 152 | + } |
| 153 | + |
| 154 | + private function askChangeView($namingConvention) |
| 155 | + { |
| 156 | + $allViews = $this->makeGlobalService->getAllViewsFiles(); |
| 157 | + $chosenView = $this->choice( |
| 158 | + 'On which view do you want to add the comment part?', |
| 159 | + $allViews, |
| 160 | + ); |
| 161 | + $this->info("Before to continue, please indicate this placeholder : {{comment_here}} where you want the form to be displayed here: ".$chosenView); |
| 162 | + $this->editCommentableView->editViewFile($chosenView, $namingConvention, $this->nameParentModel); |
| 163 | + } |
| 164 | +} |
0 commit comments