This repository has been archived by the owner on May 27, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new crudtrait command, remove non-closure from conditionals, refa…
…ctor stubs, separate timestamps for easier indexing, force db seeding for production
- Loading branch information
Showing
18 changed files
with
332 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
resources/stubs/crudtrait/app/Http/Livewire/DummyModels/Create.php.stub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace App\Http\Livewire\DummyModels; | ||
|
||
use App\Models\DummyModel; | ||
use Redbastie\Swift\Components\SwiftComponent as S; | ||
use Redbastie\Swift\Livewire\SwiftComponent; | ||
|
||
class Create extends SwiftComponent | ||
{ | ||
use Crud; | ||
|
||
protected $listeners = ['showCreateModal']; | ||
|
||
public function view() | ||
{ | ||
return S::form( | ||
S::modal('create-modal')->fade() | ||
->heading('Create DummyTitle') | ||
->body($this->formFields()) | ||
->footer( | ||
S::button('Cancel')->secondary()->click('$emit', 'hideModal', 'create-modal'), | ||
S::button('Save')->submit()->primary() | ||
) | ||
)->submitPrevent('save'); | ||
} | ||
|
||
public function showCreateModal() | ||
{ | ||
$this->reset('model'); | ||
$this->emit('showModal', 'create-modal'); | ||
} | ||
|
||
public function save() | ||
{ | ||
$validatedData = $this->validate(); | ||
|
||
DummyModel::query()->create($validatedData); | ||
|
||
$this->emit('hideModal', 'create-modal'); | ||
$this->emit('toastSuccess', 'DummyTitle created!'); | ||
$this->emit('$refresh'); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
resources/stubs/crudtrait/app/Http/Livewire/DummyModels/Crud.php.stub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App\Http\Livewire\DummyModels; | ||
|
||
use Redbastie\Swift\Components\SwiftComponent as S; | ||
|
||
trait Crud | ||
{ | ||
public $dummyVariable; | ||
|
||
protected function formFields() | ||
{ | ||
return S::div( | ||
S::input('name')->label('Name')->modelDefer(), | ||
); | ||
} | ||
|
||
protected function rules() | ||
{ | ||
return [ | ||
'name' => ['required'], | ||
]; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
resources/stubs/crudtrait/app/Http/Livewire/DummyModels/Index.php.stub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace App\Http\Livewire\DummyModels; | ||
|
||
use App\Models\DummyModel; | ||
use Redbastie\Swift\Components\SwiftComponent as S; | ||
use Redbastie\Swift\Livewire\SwiftComponent; | ||
|
||
class Index extends SwiftComponent | ||
{ | ||
public $routeUri = '/dummy-routes'; | ||
public $routeName = 'dummy-routes'; | ||
public $routeMiddleware = 'auth'; | ||
public $pageTitle = 'DummyTitles'; | ||
protected $listeners = ['$refresh']; | ||
|
||
public function query() | ||
{ | ||
$query = DummyModel::query(); | ||
|
||
if (!empty($this->model['search'])) { | ||
$query->where('name', 'like', '%' . $this->model['search'] . '%'); | ||
} | ||
|
||
return $query->orderBy('name'); | ||
} | ||
|
||
public function view() | ||
{ | ||
$dummyVariables = $this->query()->paginate(); | ||
|
||
return S::div( | ||
S::livewire('layouts.navbar'), | ||
|
||
S::container( | ||
S::heading($this->pageTitle), | ||
|
||
S::row( | ||
S::col(S::input('search')->type('search')->placeholder('Search')->modelDebounce())->mdAuto()->mb(3), | ||
S::col(S::button('Create DummyTitle')->primary()->click('$emit', 'showCreateModal'))->mdAuto()->mb(3) | ||
)->justifyContentBetween(), | ||
|
||
S::listGroup( | ||
S::each($dummyVariables, function ($dummyVariable) { | ||
return S::listGroupItem( | ||
S::row( | ||
S::col(e($dummyVariable->name))->md(), | ||
S::col( | ||
S::button(S::icon('eye'))->title('Read')->link()->p(1)->click('$emit', 'showReadModal', $dummyVariable->id), | ||
S::button(S::icon('edit'))->title('Update')->link()->p(1)->click('$emit', 'showUpdateModal', $dummyVariable->id), | ||
S::button(S::icon('trash-alt'))->title('Delete')->link()->p(1)->click('delete', $dummyVariable->id)->confirm(), | ||
)->mdAuto() | ||
)->alignItemsCenter() | ||
)->action(); | ||
})->empty( | ||
S::listGroupItem('No DummyTitles to display.') | ||
) | ||
)->mb(3), | ||
|
||
S::pagination($dummyVariables) | ||
)->py(4), | ||
|
||
S::livewire('dummy-routes.create'), | ||
S::livewire('dummy-routes.read'), | ||
S::livewire('dummy-routes.update') | ||
); | ||
} | ||
|
||
public function delete(DummyModel $dummyVariable) | ||
{ | ||
$dummyVariable->delete(); | ||
$this->emit('toastSuccess', 'DummyTitle deleted!'); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
resources/stubs/crudtrait/app/Http/Livewire/DummyModels/Read.php.stub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace App\Http\Livewire\DummyModels; | ||
|
||
use App\Models\DummyModel; | ||
use Illuminate\Support\Str; | ||
use Redbastie\Swift\Components\SwiftComponent as S; | ||
use Redbastie\Swift\Livewire\SwiftComponent; | ||
|
||
class Read extends SwiftComponent | ||
{ | ||
public $dummyVariable; | ||
protected $listeners = ['showReadModal']; | ||
|
||
public function view() | ||
{ | ||
return S::modal('read-modal')->fade() | ||
->heading('DummyTitle') | ||
->body( | ||
S::each($this->dummyVariable ? $this->dummyVariable->toArray() : [], function ($value, $key) { | ||
return S::div( | ||
S::label(Str::title(str_replace('_', ' ', Str::snake($key))))->textMuted(), | ||
|
||
is_array($value) ? | ||
S::pre(json_encode($value, JSON_PRETTY_PRINT)) : | ||
S::paragraph($value ?? 'N/A') | ||
); | ||
}) | ||
) | ||
->footer( | ||
S::button('Close')->secondary()->click('$emit', 'hideModal', 'read-modal') | ||
); | ||
} | ||
|
||
public function showReadModal(DummyModel $dummyVariable) | ||
{ | ||
$this->dummyVariable = $dummyVariable; | ||
$this->emit('showModal', 'read-modal'); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
resources/stubs/crudtrait/app/Http/Livewire/DummyModels/Update.php.stub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace App\Http\Livewire\DummyModels; | ||
|
||
use App\Models\DummyModel; | ||
use Redbastie\Swift\Components\SwiftComponent as S; | ||
use Redbastie\Swift\Livewire\SwiftComponent; | ||
|
||
class Update extends SwiftComponent | ||
{ | ||
use Crud; | ||
|
||
protected $listeners = ['showUpdateModal']; | ||
|
||
public function view() | ||
{ | ||
return S::form( | ||
S::modal('update-modal')->fade() | ||
->heading('Update DummyTitle') | ||
->body($this->formFields()) | ||
->footer( | ||
S::button('Cancel')->secondary()->click('$emit', 'hideModal', 'update-modal'), | ||
S::button('Save')->submit()->primary() | ||
) | ||
)->submitPrevent('save'); | ||
} | ||
|
||
public function showUpdateModal(DummyModel $dummyVariable) | ||
{ | ||
$this->dummyVariable = $dummyVariable; | ||
$this->model = $dummyVariable->toArray(); | ||
$this->emit('showModal', 'update-modal'); | ||
} | ||
|
||
public function save() | ||
{ | ||
$validatedData = $this->validate(); | ||
|
||
$this->dummyVariable->update($validatedData); | ||
|
||
$this->emit('hideModal', 'update-modal'); | ||
$this->emit('toastSuccess', 'DummyTitle updated!'); | ||
$this->emit('$refresh'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Faker\Generator; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Redbastie\Swift\Traits\SwiftModel; | ||
|
||
class DummyModel extends Model | ||
{ | ||
use SwiftModel; | ||
|
||
public function migration(Blueprint $table) | ||
{ | ||
$table->id(); | ||
$table->string('name'); | ||
$table->timestamp('created_at')->nullable(); | ||
$table->timestamp('updated_at')->nullable(); | ||
} | ||
|
||
public function definition(Generator $faker) | ||
{ | ||
return [ | ||
'name' => $faker->name, | ||
]; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
resources/stubs/crudtrait/database/factories/DummyFactory.php.stub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Database\Factories; | ||
|
||
use App\Models\DummyModel; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class DummyFactory extends Factory | ||
{ | ||
protected $model = DummyModel::class; | ||
|
||
public function definition() | ||
{ | ||
return app($this->model)->definition($this->faker); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Redbastie\Swift\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Str; | ||
|
||
class MakeCrudTraitCommand extends Command | ||
{ | ||
use CreatesFiles; | ||
|
||
protected $signature = 'make:crudtrait {model}'; | ||
|
||
public function handle() | ||
{ | ||
$this->argument = $this->argument('model'); | ||
$this->stubDir = __DIR__ . '/../../resources/stubs/crudtrait'; | ||
|
||
$this->createFiles(); | ||
|
||
$navbar = 'app/Http/Livewire/Layouts/Navbar.php'; | ||
|
||
if ($this->filesystem->exists($navbar)) { | ||
$navbarContents = $this->filesystem->get($navbar); | ||
$navItem = "S::navItem(S::navLink('" . $this->replaces['DummyTitles'] . "')->href(route('" . $this->replaces['dummy-routes'] . "'))),"; | ||
|
||
if (!Str::contains($navbarContents, $navItem)) { | ||
$hook = '// crud command hook'; | ||
preg_match('/(.*)' . str_replace('/', '\/', $hook) . '/', $navbarContents, $indent); | ||
|
||
$this->filesystem->put($navbar, str_replace($hook, $navItem . PHP_EOL . $indent[1] . $hook, $this->filesystem->get($navbar))); | ||
$this->info('Nav item inserted: ' . $navbar); | ||
} | ||
} | ||
|
||
$this->info($this->argument . ' CRUD + Trait generated!'); | ||
$this->warn("Don't forget to update the new model migration and run the <info>migrate:auto</info> command."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.