Skip to content

Commit 8db94bd

Browse files
author
Jonathan
committed
--with-livewire option and datatable
1 parent 9fdf6e9 commit 8db94bd

19 files changed

+699
-71
lines changed

README.md

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

2626
<sub>(Note: This step is not required if you don't need views.)</sub>
2727

28-
3\. Publish the configuration file and the default-theme directory for views:
28+
3\. Publish the configuration file, stubs and the default-theme directory for views:
2929

3030
``` php artisan vendor:publish --provider="Mrdebug\Crudgen\CrudgenServiceProvider" ```
3131

@@ -52,6 +52,8 @@ Let's do this 🙂
5252

5353
<sub>[Available options](https://github.com/misterdebug/crud-generator-laravel/wiki/Available-options-when-you-use-make:crud-command)</sub>
5454

55+
<sub>[Generate CRUD with livewire datatable](https://github.com/misterdebug/crud-generator-laravel/wiki/Generate-CRUD-with-livewire-datatable)</sub>
56+
5557
When you call this command, the controller, views and request are generated with your fields (in this case, title and content).
5658
![image](https://user-images.githubusercontent.com/23297600/192172786-1703f7b8-f577-45c1-b0f9-296999827af2.png)
5759

src/Console/MakeCrud.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Support\Facades\File;
88
use Illuminate\Support\Str;
9+
use Mrdebug\Crudgen\Services\Livewire\MakeDatatableService;
910
use Mrdebug\Crudgen\Services\MakeControllerService;
1011
use Mrdebug\Crudgen\Services\MakeGlobalService;
1112
use Mrdebug\Crudgen\Services\MakeMigrationService;
@@ -20,7 +21,7 @@ class MakeCrud extends Command
2021
*
2122
* @var string
2223
*/
23-
protected $signature = 'make:crud {crud_name} {columns}';
24+
protected $signature = 'make:crud {crud_name} {columns} {{--with-livewire}}';
2425

2526
/**
2627
* The console command description.
@@ -38,6 +39,7 @@ class MakeCrud extends Command
3839
public MakeRequestService $makeRequestService;
3940
public MakeMigrationService $makeMigrationService;
4041
public MakeModelService $makeModelService;
42+
public MakeDatatableService $makeDatatableService;
4143
public MakeGlobalService $makeGlobalService;
4244
public PathsAndNamespacesService $pathsAndNamespacesService;
4345

@@ -46,6 +48,7 @@ public function __construct(
4648
MakeRequestService $makeRequestService,
4749
MakeMigrationService $makeMigrationService,
4850
MakeModelService $makeModelService,
51+
MakeDatatableService $makeDatatableService,
4952
MakeGlobalService $makeGlobalService,
5053
PathsAndNamespacesService $pathsAndNamespacesService
5154
)
@@ -55,6 +58,7 @@ public function __construct(
5558
$this->makeRequestService = $makeRequestService;
5659
$this->makeMigrationService = $makeMigrationService;
5760
$this->makeModelService = $makeModelService;
61+
$this->makeDatatableService = $makeDatatableService;
5862
$this->makeGlobalService = $makeGlobalService;
5963
$this->pathsAndNamespacesService = $pathsAndNamespacesService;
6064
}
@@ -68,6 +72,7 @@ public function handle()
6872
{
6973
// we create our variables to respect the naming conventions
7074
$crudName = ucfirst($this->argument('crud_name'));
75+
$withLivewire = $this->option('with-livewire');
7176
$namingConvention = $this->makeGlobalService->getNamingConvention($crudName);
7277
$columns = $this->makeGlobalService->parseColumns($this->argument('columns'));
7378
$laravelNamespace = $this->laravel->getNamespace();
@@ -78,7 +83,18 @@ public function handle()
7883
7984
************************************************************************* */
8085

81-
$this->makeControllerService->makeCompleteControllerFile($namingConvention, $columns, $laravelNamespace);
86+
$this->makeControllerService->makeCompleteControllerFile($namingConvention, $columns, $laravelNamespace, $withLivewire);
87+
88+
/* *************************************************************************
89+
90+
DATATABLE
91+
92+
************************************************************************* */
93+
if($withLivewire)
94+
{
95+
$columnNameInLivewireSearch = $this->setNameColumnInLivewireSearch($columns);
96+
$this->makeDatatableService->makeCompleteDatatableFile($namingConvention, $laravelNamespace, $columnNameInLivewireSearch);
97+
}
8298

8399
/* *************************************************************************
84100
@@ -91,10 +107,13 @@ public function handle()
91107
'make:views',
92108
[
93109
'directory'=> $crudName,
94-
'columns'=> $this->argument('columns')
110+
'columns'=> $this->argument('columns'),
111+
'--with-livewire' => $withLivewire,
112+
'searchableColumn' => $columnNameInLivewireSearch ?? null
95113
]
96114
);
97115

116+
98117
/* *************************************************************************
99118
100119
REQUEST
@@ -170,4 +189,15 @@ private function setNameModelRelationship($type, $namingConvention, $infos)
170189
else
171190
$this->setNameModelRelationship($type, $namingConvention, $infos);
172191
}
192+
193+
private function setNameColumnInLivewireSearch(array $columns)
194+
{
195+
$choices = $this->makeGlobalService->getColumnsNameFromInputConsole($columns);
196+
$columnInSearch = $this->choice(
197+
'Please specify which column you would like to use in the Livewire search?',
198+
$choices
199+
);
200+
201+
return $columnInSearch;
202+
}
173203
}

src/Console/MakeViews.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class MakeViews extends Command
1616
*
1717
* @var string
1818
*/
19-
protected $signature = 'make:views {directory} {columns}';
19+
protected $signature = 'make:views {directory} {columns} {--with-livewire} {searchableColumn?}';
2020

2121
/**
2222
* The console command description.
@@ -50,6 +50,8 @@ public function handle()
5050
{
5151
$templateViewsDirectory = config('crudgen.views_style_directory');
5252
$separateStyleAccordingToActions = config('crudgen.separate_style_according_to_actions');
53+
$withLivewire = $this->option('with-livewire');
54+
$columnInSearch = $this->argument('searchableColumn');
5355

5456
if(!File::isDirectory($this->pathsAndNamespacesService->getCrudgenViewsStubCustom($templateViewsDirectory)))
5557
{
@@ -93,8 +95,14 @@ public function handle()
9395

9496
/* ************************** index view *************************** */
9597

96-
$contentIndex = $this->makeViewsService->findAndReplaceIndexViewPlaceholderColumns($columns, $templateViewsDirectory, $namingConvention, $separateStyleAccordingToActions);
97-
$this->makeViewsService->createFileOrError($namingConvention, $contentIndex, 'index.blade.php');
98+
$contentIndex = $this->makeViewsService->findAndReplaceIndexViewPlaceholderColumns($columns, $templateViewsDirectory, $namingConvention, $separateStyleAccordingToActions, $withLivewire, $columnInSearch);
99+
$this->makeViewsService->createFileOrError($namingConvention, $contentIndex, $withLivewire ? $namingConvention['singular_low_name'].'-datatable.blade.php' : 'index.blade.php');
100+
101+
if($withLivewire)
102+
{
103+
$contentDatableIndexLivewire = $this->makeViewsService->findAndReplaceIndexViewPlaceholderLivewire($templateViewsDirectory, $namingConvention, $separateStyleAccordingToActions, $withLivewire);
104+
$this->makeViewsService->createFileOrError($namingConvention, $contentDatableIndexLivewire, 'index.blade.php');
105+
}
98106

99107

100108
/* ************************** create view *************************** */

src/Console/RemoveCrud.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function handle()
4848
$this->deleteDirectory($namingConvention, 'views', $force);
4949
$this->deleteFile($namingConvention, 'request', $force);
5050
$this->deleteFile($namingConvention, 'model', $force);
51+
$this->deleteFile($namingConvention, 'datatable', $force);
5152
}
5253

5354
private function deleteFile($namingConvention, $fileType, $force)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Mrdebug\Crudgen\Services\Livewire;
4+
5+
6+
use Illuminate\Support\Facades\File;
7+
use Illuminate\Console\Concerns\InteractsWithIO;
8+
use Symfony\Component\Console\Output\ConsoleOutput;
9+
use Illuminate\Contracts\Foundation\Application;
10+
use Mrdebug\Crudgen\Services\MakeGlobalService;
11+
use Mrdebug\Crudgen\Services\PathsAndNamespacesService;
12+
13+
class MakeDatatableService
14+
{
15+
use InteractsWithIO;
16+
17+
public PathsAndNamespacesService $pathsAndNamespacesService;
18+
public MakeGlobalService $makeGlobalService;
19+
public function __construct(
20+
PathsAndNamespacesService $pathsAndNamespacesService,
21+
ConsoleOutput $consoleOutput,
22+
Application $application,
23+
MakeGlobalService $makeGlobalService
24+
)
25+
{
26+
$this->pathsAndNamespacesService = $pathsAndNamespacesService;
27+
$this->output = $consoleOutput;
28+
$this->laravel = $application->getNamespace();
29+
$this->makeGlobalService = $makeGlobalService;
30+
}
31+
32+
public function replaceContentDatatableStub($namingConvention, $laravelNamespace, $columnInSearch)
33+
{
34+
$datatableStub = File::get($this->pathsAndNamespacesService->getDatatableStubPath());
35+
$datatableStub = str_replace('DummyClass', $namingConvention['singular_name'].'Datatable', $datatableStub);
36+
$datatableStub = str_replace('DummyModel', $namingConvention['singular_name'], $datatableStub);
37+
$datatableStub = str_replace('DummyVariable', $namingConvention['plural_low_name'], $datatableStub);
38+
$datatableStub = str_replace('DummyNamespace', $this->pathsAndNamespacesService->getDefaultNamespaceDatatable($laravelNamespace), $datatableStub);
39+
$datatableStub = str_replace('DummyRootNamespace', $laravelNamespace, $datatableStub);
40+
$datatableStub = str_replace('DummyCreateVariable$', '$'.$namingConvention['plural_low_name'], $datatableStub);
41+
$datatableStub = str_replace('{{name-component}}', $namingConvention['singular_low_name'], $datatableStub);
42+
$datatableStub = str_replace('{{directory-views}}', $namingConvention['plural_low_name'], $datatableStub);
43+
$datatableStub = str_replace('{{column-in-search}}', $columnInSearch, $datatableStub);
44+
45+
return $datatableStub;
46+
}
47+
48+
public function createDatatableFile($pathNewDatatable, $datatableStub, $namingConvention)
49+
{
50+
if(!File::exists($pathNewDatatable))
51+
{
52+
File::put($pathNewDatatable, $datatableStub);
53+
$this->line("<info>Created Datatable:</info> ".$namingConvention['singular_name']);
54+
}
55+
else
56+
$this->error('Datatable '.$namingConvention['singular_name'].' already exists');
57+
}
58+
59+
public function makeCompleteDatatableFile($namingConvention, $laravelNamespace, $columnInSearch)
60+
{
61+
$datatableStub = $this->replaceContentDatatableStub($namingConvention, $laravelNamespace, $columnInSearch);
62+
63+
// if our datatable doesn't exists we create it
64+
$pathNewDatatable = $this->pathsAndNamespacesService->getRealpathBaseCustomDatatable($namingConvention);
65+
$this->createDatatableFile($pathNewDatatable, $datatableStub, $namingConvention);
66+
}
67+
}

src/Services/MakeControllerService.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ public function __construct(
2727
$this->makeGlobalService = $makeGlobalService;
2828
}
2929

30-
public function replaceContentControllerStub($namingConvention, $laravelNamespace)
30+
public function replaceContentControllerStub($namingConvention, $laravelNamespace, $withLivewire)
3131
{
32-
$controllerStub = File::get($this->pathsAndNamespacesService->getControllerStubPath());
32+
$controllerStubPath = $withLivewire ? $this->pathsAndNamespacesService->getControllerLivewireStubPath() : $this->pathsAndNamespacesService->getControllerStubPath();
33+
$controllerStub = File::get($controllerStubPath);
3334
$controllerStub = str_replace('DummyClass', $namingConvention['plural_name'].'Controller', $controllerStub);
3435
$controllerStub = str_replace('DummyModel', $namingConvention['singular_name'], $controllerStub);
3536
$controllerStub = str_replace('DummyVariableSing', $namingConvention['singular_low_name'], $controllerStub);
@@ -73,9 +74,9 @@ public function createControllerFile($pathNewController, $controllerStub, $namin
7374
$this->error('Controller '.$namingConvention['plural_name'].' already exists');
7475
}
7576

76-
public function makeCompleteControllerFile($namingConvention, $columns, $laravelNamespace)
77+
public function makeCompleteControllerFile($namingConvention, $columns, $laravelNamespace, $withLivewire)
7778
{
78-
$controllerStub = $this->replaceContentControllerStub($namingConvention, $laravelNamespace);
79+
$controllerStub = $this->replaceContentControllerStub($namingConvention, $laravelNamespace, $withLivewire);
7980
$controllerStub = $this->findAndReplaceControllerPlaceholderColumns($columns, $controllerStub, $namingConvention);
8081

8182
// if our controller doesn't exists we create it

src/Services/MakeGlobalService.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,15 @@ public function getCommentableParentModelConvention($commentableParentName): arr
6262
'plural_low_variable_name' => Str::plural(Str::camel(Str::lower($commentableParentName))),
6363
];
6464
}
65+
66+
public function getColumnsNameFromInputConsole($columns)
67+
{
68+
$columnsName = [];
69+
foreach($columns as $column)
70+
{
71+
$type = explode(':', trim($column));
72+
$columnsName[]= $type[0];
73+
}
74+
return $columnsName;
75+
}
6576
}

src/Services/MakeViewsService.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function findAndReplaceControllerPlaceholderColumns($columns, $controller
6969
return $controllerStub;
7070
}
7171

72-
public function findAndReplaceIndexViewPlaceholderColumns($columns, $templateViewsDirectory, $namingConvention, $separateStyleAccordingToActions)
72+
public function findAndReplaceIndexViewPlaceholderColumns($columns, $templateViewsDirectory, $namingConvention, $separateStyleAccordingToActions, $withLivewire, $columnInSearch)
7373
{
7474
$thIndex=$indexView='';
7575
foreach ($columns as $column)
@@ -79,10 +79,17 @@ public function findAndReplaceIndexViewPlaceholderColumns($columns, $templateVie
7979

8080
// our placeholders
8181
$thIndex .=str_repeat("\t", 4)."<th>".trim($column)."</th>\n";
82-
$indexView .=str_repeat("\t", 5)."<td>{{ DummyCreateVariableSing$->".trim($column)." }}</td>\n";
82+
83+
if($column == $columnInSearch)
84+
$indexView .=str_repeat("\t", 5).'<td>{!! $this->search ? $this->highlightTitle(DummyCreateVariableSing$->'.$columnInSearch.') : DummyCreateVariableSing$->'.$columnInSearch.' !!}</td>'."\n";
85+
else
86+
$indexView .=str_repeat("\t", 5)."<td>{{ DummyCreateVariableSing$->".trim($column)." }}</td>\n";
8387
}
8488

85-
$indexStub = File::get($this->pathsAndNamespacesService->getCrudgenViewsStubCustom($templateViewsDirectory).DIRECTORY_SEPARATOR.'index.stub');
89+
$indexStub = $withLivewire
90+
? File::get($this->pathsAndNamespacesService->getCrudgenViewsStubCustom($templateViewsDirectory).DIRECTORY_SEPARATOR.'livewire'.DIRECTORY_SEPARATOR.'index-datatable.stub')
91+
: File::get($this->pathsAndNamespacesService->getCrudgenViewsStubCustom($templateViewsDirectory).DIRECTORY_SEPARATOR.'index.stub');
92+
8693
$indexStub = str_replace('DummyCreateVariable$', '$'.$namingConvention['plural_low_name'], $indexStub);
8794
$indexStub = str_replace('DummyCreateVariableSing$', '$'.$namingConvention['singular_low_name'], $indexStub);
8895
$indexStub = str_replace('DummyHeaderTable', $thIndex, $indexStub);
@@ -95,6 +102,18 @@ public function findAndReplaceIndexViewPlaceholderColumns($columns, $templateVie
95102
return $indexStub;
96103
}
97104

105+
public function findAndReplaceIndexViewPlaceholderLivewire($templateViewsDirectory, $namingConvention, $separateStyleAccordingToActions, $withLivewire)
106+
{
107+
if($withLivewire)
108+
{
109+
$indexStub = File::get($this->pathsAndNamespacesService->getCrudgenViewsStubCustom($templateViewsDirectory).DIRECTORY_SEPARATOR.'index-livewire.stub');
110+
$indexStub = str_replace('{{nameSingLower}}', $namingConvention['singular_low_name'], $indexStub);
111+
$indexStub = str_replace('DummyExtends', $separateStyleAccordingToActions['index']['extends'], $indexStub);
112+
$indexStub = str_replace('DummySection', $separateStyleAccordingToActions['index']['section'], $indexStub);
113+
return $indexStub;
114+
}
115+
}
116+
98117
public function findAndReplaceCreateViewPlaceholderColumns($columns, $templateViewsDirectory, $namingConvention, $separateStyleAccordingToActions)
99118
{
100119
$formCreate='';
@@ -155,11 +174,15 @@ public function findAndReplaceEditViewPlaceholderColumns($columns, $templateView
155174
return $editStub;
156175
}
157176

158-
public function createFileOrError($namingConvention, $contentFile, $fileName)
177+
public function createFileOrError($namingConvention, $contentFile, $fileName, $withLivewire=false)
159178
{
160-
if(!File::exists($this->pathsAndNamespacesService->getRealpathBaseCustomViews($namingConvention).DIRECTORY_SEPARATOR.$fileName))
179+
$path = $withLivewire
180+
? $this->pathsAndNamespacesService->getRealpathBaseCustomLivewireViews($namingConvention).DIRECTORY_SEPARATOR.$fileName
181+
: $this->pathsAndNamespacesService->getRealpathBaseCustomViews($namingConvention).DIRECTORY_SEPARATOR.$fileName;
182+
183+
if(!File::exists($path))
161184
{
162-
File::put($this->pathsAndNamespacesService->getRealpathBaseCustomViews($namingConvention).DIRECTORY_SEPARATOR.$fileName, $contentFile);
185+
File::put($path, $contentFile);
163186
$this->line("<info>Created View:</info> ".$fileName);
164187
}
165188
else

0 commit comments

Comments
 (0)