Skip to content

Commit 302aded

Browse files
authored
Merge pull request #21 from misterdebug/20-new-feature-suggested-add-a-commentable-structure-to-any-model
new feature suggested add a commentable structure to any model
2 parents dc4ce59 + fe7e063 commit 302aded

23 files changed

+891
-37
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ And since 1.9.2, a complete **REST API** !
1717

1818
2\. If you don't use Laravel Collective Form package in your project, install it:
1919

20-
``` composer require laravelcollective/html ```
20+
``` composer require laravelcollective/html ``` <sub>not required if you don't need views</sub>
2121

2222
3\. Publish config file and default-theme directory for views
2323

@@ -115,6 +115,9 @@ Add your `Tag` CRUD (with a `column` name)
115115

116116
``` php artisan make:crud tag "name" ```
117117

118+
FYI : `Comment` is a specific case and you can use `make:commentable` command
119+
[Docs about commentable](https://github.com/misterdebug/crud-generator-laravel/wiki/Add-a-commentable-structure-to-any-model)</sub>
120+
118121
Finished 🎉
119122

120123
## Remove a CRUD

composer.json

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,43 @@
22
"name": "mrdebug/crudgen",
33
"description": "Create a Laravel Crud in a few seconds",
44
"license": "MIT",
5+
"keywords": [
6+
"laravel",
7+
"crud",
8+
"rest",
9+
"generator",
10+
"rest-api",
11+
"laravel-package",
12+
"crud-generator",
13+
"laravel-crud-generator",
14+
"composer-package",
15+
"laravel-crud",
16+
"laravel8",
17+
"laravel9",
18+
"laravel10"
19+
],
520
"authors": [
621
{
722
"name": "Jonathan Escarem",
823
"email": "[email protected]"
924
}
1025
],
11-
"require":
26+
"require":
1227
{
13-
"php": ">=5.5.9"
28+
"php": ">=8.0.0"
1429
},
15-
"autoload":
30+
"autoload":
1631
{
17-
"psr-4":
32+
"psr-4":
1833
{
1934
"Mrdebug\\Crudgen\\": "src/"
2035
}
2136
},
22-
"extra":
37+
"extra":
2338
{
24-
"laravel":
39+
"laravel":
2540
{
26-
"providers":
41+
"providers":
2742
[
2843
"Mrdebug\\Crudgen\\CrudgenServiceProvider"
2944
]

src/Console/MakeCommentable.php

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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+
}

src/Console/RemoveCommentable.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Mrdebug\Crudgen\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\File;
7+
use Mrdebug\Crudgen\Services\MakeGlobalService;
8+
use Mrdebug\Crudgen\Services\RemoveCommentableService;
9+
10+
class RemoveCommentable extends Command
11+
{
12+
/**
13+
* The name and signature of the console command.
14+
*
15+
* @var string
16+
*/
17+
protected $signature = 'rm:commentable {commentable_name} {--force}';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Remove a commentable operation';
25+
26+
public RemoveCommentableService $removeCommentableService;
27+
public MakeGlobalService $makeGlobalService;
28+
public function __construct(RemoveCommentableService $removeCommentableService,MakeGlobalService $makeGlobalService)
29+
{
30+
parent::__construct();
31+
$this->removeCommentableService = $removeCommentableService;
32+
$this->makeGlobalService = $makeGlobalService;
33+
}
34+
35+
/**
36+
* Execute the console command.
37+
*
38+
* @return mixed
39+
*/
40+
public function handle()
41+
{
42+
// we create our variables to respect the naming conventions
43+
$commentableName = ucfirst($this->argument('commentable_name'));
44+
$namingConvention = $this->makeGlobalService->getCommentableNamingConvention($commentableName);
45+
$force = $this->option('force');
46+
47+
$this->deleteFile($namingConvention, 'controller', $force);
48+
$this->deleteFile($namingConvention, 'request', $force);
49+
$this->deleteFile($namingConvention, 'model', $force);
50+
}
51+
52+
private function deleteFile($namingConvention, $fileType, $force)
53+
{
54+
if(File::exists($this->removeCommentableService->pathsForFiles($namingConvention)[$fileType]))
55+
{
56+
if ($force || $this->confirm('Do you want to delete this '.$fileType.' '.$this->removeCommentableService->pathsForFiles($namingConvention)[$fileType].'?'))
57+
{
58+
if(File::delete($this->removeCommentableService->pathsForFiles($namingConvention)[$fileType]))
59+
$this->line("<info>".ucfirst($fileType)." deleted</info>");
60+
}
61+
}
62+
}
63+
}

src/CrudgenServiceProvider.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
namespace Mrdebug\Crudgen;
44

55
use Illuminate\Support\ServiceProvider;
6+
use Mrdebug\Crudgen\Console\MakeApiCrud;
7+
use Mrdebug\Crudgen\Console\MakeCommentable;
8+
use Mrdebug\Crudgen\Console\MakeCrud;
9+
use Mrdebug\Crudgen\Console\MakeViews;
10+
use Mrdebug\Crudgen\Console\RemoveApiCrud;
11+
use Mrdebug\Crudgen\Console\RemoveCommentable;
12+
use Mrdebug\Crudgen\Console\RemoveCrud;
613

714
class CrudgenServiceProvider extends ServiceProvider
815
{
@@ -33,11 +40,13 @@ public function register()
3340
$this->mergeConfigFrom(__DIR__.'/../config/crudgen.php', 'crudgen');
3441

3542
$this->commands(
36-
'Mrdebug\Crudgen\Console\MakeCrud',
37-
'Mrdebug\Crudgen\Console\MakeViews',
38-
'Mrdebug\Crudgen\Console\RemoveCrud',
39-
'Mrdebug\Crudgen\Console\MakeApiCrud',
40-
'Mrdebug\Crudgen\Console\RemoveApiCrud'
43+
MakeCrud::class,
44+
MakeViews::class,
45+
RemoveCrud::class,
46+
MakeApiCrud::class,
47+
RemoveApiCrud::class,
48+
MakeCommentable::class,
49+
RemoveCommentable::class,
4150
);
4251
}
4352
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Mrdebug\Crudgen\Exceptions;
4+
5+
use Exception;
6+
use Symfony\Component\Console\Exception\ExceptionInterface;
7+
8+
class ConsoleException extends Exception implements ExceptionInterface
9+
{
10+
11+
}

src/Services/Api/MakeApiControllerService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function makeCompleteApiControllerFile($namingConvention, $columns, $lara
8484
$controllerStub = $this->replaceContentApiControllerStub($namingConvention, $laravelNamespace);
8585
$controllerStub = $this->findAndReplaceApiControllerPlaceholderColumns($columns, $controllerStub, $namingConvention);
8686

87-
// if our controller doesn't exists we create it
87+
// if our controller doesn't exist we create it
8888
$pathNewController = $this->pathsAndNamespacesService->getRealpathBaseCustomApiController($namingConvention);
8989
$this->createApiControllerFile($pathNewController, $controllerStub, $namingConvention);
9090
}

0 commit comments

Comments
 (0)