Skip to content

Commit ed03c58

Browse files
authored
Merge pull request #423 from rappasoft/develop
v1.12.0
2 parents e273ef7 + 37594eb commit ed03c58

File tree

6 files changed

+281
-1
lines changed

6 files changed

+281
-1
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
44

55
## [Unreleased]
66

7+
## [1.12.0] - 2021-07-31
8+
9+
### Added
10+
11+
- [Make datatable command](https://github.com/rappasoft/laravel-livewire-tables/pull/408)
12+
713
## [1.11.0] - 2021-07-10
814

915
### Added
@@ -428,7 +434,8 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
428434

429435
- Initial release
430436

431-
[Unreleased]: https://github.com/rappasoft/laravel-livewire-tables/compare/v1.11.0...development
437+
[Unreleased]: https://github.com/rappasoft/laravel-livewire-tables/compare/v1.12.0...development
438+
[1.12.0]: https://github.com/rappasoft/laravel-livewire-tables/compare/v1.11.0...v1.12.0
432439
[1.11.0]: https://github.com/rappasoft/laravel-livewire-tables/compare/v1.10.4...v1.11.0
433440
[1.10.4]: https://github.com/rappasoft/laravel-livewire-tables/compare/v1.10.3...v1.10.4
434441
[1.10.3]: https://github.com/rappasoft/laravel-livewire-tables/compare/v1.10.2...v1.10.3

src/Commands/MakeCommand.php

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\File;
7+
use Illuminate\Support\Str;
8+
use Livewire\Commands\ComponentParser;
9+
use Livewire\Commands\MakeCommand as LivewireMakeCommand;
10+
11+
/**
12+
* Class MakeCommand
13+
*
14+
* @package Rappasoft\LaravelLivewireTables\Commands
15+
*/
16+
class MakeCommand extends Command
17+
{
18+
19+
/**
20+
* @var
21+
*/
22+
protected $parser;
23+
24+
/**
25+
* @var
26+
*/
27+
protected $model;
28+
29+
/**
30+
* @var
31+
*/
32+
protected $viewPath;
33+
34+
/**
35+
* The name and signature of the console command.
36+
*
37+
* @var string
38+
*/
39+
protected $signature = 'make:datatable
40+
{name : The name of your Livewire class}
41+
{model? : The name of the model you want to use in this table}
42+
{--view : We will generate a row view for you}
43+
{--force}';
44+
45+
/**
46+
* The console command description.
47+
*
48+
* @var string
49+
*/
50+
protected $description = 'Generate a Laravel Livewire datatable class and view.';
51+
52+
public function handle(): void
53+
{
54+
$this->parser = new ComponentParser(
55+
config('livewire.class_namespace'),
56+
config('livewire.view_path'),
57+
$this->argument('name')
58+
);
59+
60+
$livewireMakeCommand = new LivewireMakeCommand();
61+
62+
if ($livewireMakeCommand->isReservedClassName($name = $this->parser->className())) {
63+
$this->line("<fg=red;options=bold>Class is reserved:</> {$name}");
64+
65+
return;
66+
}
67+
68+
$this->model = Str::studly($this->argument('model'));
69+
$force = $this->option('force');
70+
71+
$this->viewPath = $this->createView($force);
72+
$this->createClass($force);
73+
74+
$this->info('Livewire Datatable Created: ' . $this->parser->className());
75+
}
76+
77+
/**
78+
* @param bool $force
79+
*
80+
* @return false
81+
*/
82+
protected function createClass(bool $force = false)
83+
{
84+
$classPath = $this->parser->classPath();
85+
86+
if (! $force && File::exists($classPath)) {
87+
$this->line("<fg=red;options=bold>Class already exists:</> {$this->parser->relativeClassPath()}");
88+
89+
return false;
90+
}
91+
92+
$this->ensureDirectoryExists($classPath);
93+
94+
File::put($classPath, $this->classContents());
95+
96+
return $classPath;
97+
}
98+
99+
/**
100+
* @param bool $force
101+
*
102+
* @return false|string|null
103+
*/
104+
protected function createView(bool $force = false)
105+
{
106+
if (! $this->option('view')) {
107+
return null;
108+
}
109+
110+
$viewPath = base_path('resources/views/livewire-tables/rows/' . Str::snake($this->parser->className()->__toString()) . '.blade.php');
111+
112+
if (! $force && File::exists($viewPath)) {
113+
$this->line("<fg=red;options=bold>View already exists:</> {$viewPath}");
114+
115+
return false;
116+
}
117+
118+
$this->ensureDirectoryExists($viewPath);
119+
120+
File::put($viewPath, $this->viewContents());
121+
122+
return $viewPath;
123+
}
124+
125+
/**
126+
* @param $path
127+
*/
128+
protected function ensureDirectoryExists($path): void
129+
{
130+
if (! File::isDirectory(dirname($path))) {
131+
File::makeDirectory(dirname($path), 0777, true, true);
132+
}
133+
}
134+
135+
/**
136+
* @return string
137+
*/
138+
public function classContents(): string
139+
{
140+
if ($this->model) {
141+
$template = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'table-with-model.stub');
142+
143+
$contents = str_replace(
144+
['[namespace]', '[class]', '[model]', '[model_import]'],
145+
[$this->parser->classNamespace(), $this->parser->className(), $this->model, $this->getModelImport()],
146+
$template
147+
);
148+
} else {
149+
$template = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'table.stub');
150+
151+
$contents = str_replace(
152+
['[namespace]', '[class]'],
153+
[$this->parser->classNamespace(), $this->parser->className()],
154+
$template
155+
);
156+
}
157+
158+
if ($this->viewPath) {
159+
$contents = Str::replaceLast(
160+
"}\n",
161+
"
162+
public function rowView(): string
163+
{
164+
return '" . $this->getViewPathForRowView() . "';
165+
}
166+
}\n",
167+
$contents
168+
);
169+
}
170+
171+
return $contents;
172+
}
173+
174+
/**
175+
* @return string
176+
*/
177+
private function getViewPathForRowView(): string
178+
{
179+
return Str::replace('/', '.', Str::before(Str::after($this->viewPath, 'resources/views/'), '.blade.php'));
180+
}
181+
182+
/**
183+
* @return false|string
184+
*/
185+
public function viewContents()
186+
{
187+
return file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'view.stub');
188+
}
189+
190+
/**
191+
* @return string
192+
*/
193+
public function getModelImport(): string
194+
{
195+
if (File::exists(app_path('Models/' . $this->model . '.php'))) {
196+
return 'App\Models\\' . $this->model;
197+
}
198+
199+
if (File::exists(app_path($this->model . '.php'))) {
200+
return 'App\\' . $this->model;
201+
}
202+
203+
$this->error('Could not find path to model.');
204+
205+
return 'App\Models\\' . $this->model;
206+
}
207+
}

src/Commands/table-with-model.stub

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace [namespace];
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Rappasoft\LaravelLivewireTables\DataTableComponent;
7+
use Rappasoft\LaravelLivewireTables\Views\Column;
8+
use [model_import];
9+
10+
class [class] extends DataTableComponent
11+
{
12+
13+
public function columns(): array
14+
{
15+
return [
16+
Column::make('Column Name'),
17+
];
18+
}
19+
20+
public function query(): Builder
21+
{
22+
return [model]::query();
23+
}
24+
}

src/Commands/table.stub

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace [namespace];
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Rappasoft\LaravelLivewireTables\DataTableComponent;
7+
use Rappasoft\LaravelLivewireTables\Views\Column;
8+
9+
class [class] extends DataTableComponent
10+
{
11+
12+
public function columns(): array
13+
{
14+
return [
15+
Column::make('Column Name'),
16+
];
17+
}
18+
19+
public function query(): Builder
20+
{
21+
22+
}
23+
}

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>

src/LaravelLivewireTablesServiceProvider.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Rappasoft\LaravelLivewireTables;
44

55
use Illuminate\Support\Facades\Blade;
6+
use Rappasoft\LaravelLivewireTables\Commands\MakeCommand;
67
use Spatie\LaravelPackageTools\Package;
78
use Spatie\LaravelPackageTools\PackageServiceProvider;
89

@@ -32,6 +33,19 @@ public function bootingPackage(): void
3233
Blade::component('livewire-tables::bootstrap-5.components.table.heading', 'livewire-tables::bs5.table.heading');
3334
Blade::component('livewire-tables::bootstrap-5.components.table.row', 'livewire-tables::bs5.table.row');
3435
Blade::component('livewire-tables::bootstrap-5.components.table.cell', 'livewire-tables::bs5.table.cell');
36+
37+
$this->registerCommands();
38+
}
39+
40+
protected function registerCommands(): void
41+
{
42+
if (! $this->app->runningInConsole()) {
43+
return;
44+
}
45+
46+
$this->commands([
47+
MakeCommand::class,
48+
]);
3549
}
3650

3751
/**

0 commit comments

Comments
 (0)