Skip to content

Commit f9506c7

Browse files
committed
add --view flag
1 parent 407d67d commit f9506c7

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/Commands/MakeCommand.php

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class MakeCommand extends Command
1212
{
1313
protected $parser;
1414
protected $model = null;
15+
protected $viewPath = null;
1516
/**
1617
* The name and signature of the console command.
1718
*
@@ -20,7 +21,8 @@ class MakeCommand extends Command
2021
protected $signature = 'make:table
2122
{name : The name of your Livewire class}
2223
{model? : The name of the model you want to use in this table }
23-
{--force}';
24+
{--V|view : We will generate a row view for you}
25+
{--force }';
2426

2527
/**
2628
* The console command description.
@@ -48,6 +50,7 @@ public function handle()
4850
$this->model = Str::studly($this->argument('model'));
4951
$force = $this->option('force');
5052

53+
$this->viewPath = $this->createView($force);
5154
$this->createClass($force);
5255

5356
$this->info('Livewire Datatable Created: ' . $this->parser->className());
@@ -71,6 +74,26 @@ protected function createClass($force = false)
7174
return $classPath;
7275
}
7376

77+
protected function createView($force = false)
78+
{
79+
if(! $this->option('view')) {
80+
return null;
81+
}
82+
$viewPath = base_path('resources/views/livewire-tables/rows/' . Str::snake($this->parser->className()->__toString()) . '.blade.php');
83+
if (File::exists($viewPath) && ! $force) {
84+
$this->line("<options=bold,reverse;fg=red> WHOOPS-IE-TOOTLES </> 😳 \n");
85+
$this->line("<fg=red;options=bold>View already exists:</> {$viewPath}");
86+
87+
return false;
88+
}
89+
90+
$this->ensureDirectoryExists($viewPath);
91+
92+
File::put($viewPath, $this->viewContents());
93+
94+
return $viewPath;
95+
}
96+
7497
protected function ensureDirectoryExists($path)
7598
{
7699
if (! File::isDirectory(dirname($path))) {
@@ -83,20 +106,38 @@ public function classContents()
83106
if($this->model) {
84107
$template = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'table-with-model.stub');
85108

86-
return preg_replace_array(
109+
$contents = preg_replace_array(
87110
['/\[namespace\]/', '/\[class\]/', '/\[model\]/', '/\[model_import\]/'],
88111
[$this->parser->classNamespace(), $this->parser->className(), $this->model, $this->getModelImport()],
89112
$template
90113
);
91114
} else {
92115
$template = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'table.stub');
93116

94-
return preg_replace_array(
117+
$contents = preg_replace_array(
95118
['/\[namespace\]/', '/\[class\]/'],
96119
[$this->parser->classNamespace(), $this->parser->className()],
97120
$template
98121
);
99122
}
123+
124+
if($this->viewPath) {
125+
$viewPath = Str::after($this->viewPath, 'resources/views/');
126+
$contents = Str::replaceLast("}\n", "
127+
public function rowView(): string
128+
{
129+
return '" . $viewPath . "';
130+
}
131+
}\n",
132+
$contents);
133+
}
134+
135+
return $contents;
136+
}
137+
138+
public function viewContents()
139+
{
140+
return file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'view.stub');
100141
}
101142

102143
public function getModelImport()

src/Commands/view.stub

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<x-livewire-tables::table.cell>
2+
{{-- Note: This is a tailwind cell --}}
3+
{{-- For bootstrap 4, use <x-livewire-tables::bs4.table.cell> --}}
4+
{{-- For bootstrap 5, use <x-livewire-tables::bs5.table.cell> --}}
5+
</x-livewire-tables::table.cell>

0 commit comments

Comments
 (0)