Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Widget Command Livewire Path Solution #15871

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions packages/widgets/src/Commands/MakeWidgetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ public function handle(): int

$path = (string) str($widget)
->prepend('/')
->prepend($resource === null ? $path : "{$resourcePath}\\{$resource}\\Widgets\\")
->prepend(
! $panel
? app_path('Livewire/')
: ($resource === null ? $path : "{$resourcePath}\\{$resource}\\Widgets\\")
)
->replace('\\', '/')
->replace('//', '/')
->append('.php');
Expand Down Expand Up @@ -199,7 +203,11 @@ public function handle(): int

$this->copyStubToApp('ChartWidget', $path, [
'class' => $widgetClass,
'namespace' => filled($resource) ? "{$resourceNamespace}\\{$resource}\\Widgets" . ($widgetNamespace !== '' ? "\\{$widgetNamespace}" : '') : $namespace . ($widgetNamespace !== '' ? "\\{$widgetNamespace}" : ''),
'namespace' => ! $panel
? $namespace . ($widgetNamespace !== '' ? "\\{$widgetNamespace}" : '')
: (filled($resource)
? "{$resourceNamespace}\\{$resource}\\Widgets" . ($widgetNamespace !== '' ? "\\{$widgetNamespace}" : '')
: $namespace . ($widgetNamespace !== '' ? "\\{$widgetNamespace}" : '')),
'type' => match ($chartType) {
'Bar chart' => 'bar',
'Bubble chart' => 'bubble',
Expand All @@ -214,17 +222,29 @@ public function handle(): int
} elseif ($type === 'Stats overview') {
$this->copyStubToApp('StatsOverviewWidget', $path, [
'class' => $widgetClass,
'namespace' => filled($resource) ? "{$resourceNamespace}\\{$resource}\\Widgets" . ($widgetNamespace !== '' ? "\\{$widgetNamespace}" : '') : $namespace . ($widgetNamespace !== '' ? "\\{$widgetNamespace}" : ''),
'namespace' => ! $panel
? $namespace . ($widgetNamespace !== '' ? "\\{$widgetNamespace}" : '')
: (filled($resource)
? "{$resourceNamespace}\\{$resource}\\Widgets" . ($widgetNamespace !== '' ? "\\{$widgetNamespace}" : '')
: $namespace . ($widgetNamespace !== '' ? "\\{$widgetNamespace}" : '')),
]);
} elseif ($type === 'Table') {
$this->copyStubToApp('TableWidget', $path, [
'class' => $widgetClass,
'namespace' => filled($resource) ? "{$resourceNamespace}\\{$resource}\\Widgets" . ($widgetNamespace !== '' ? "\\{$widgetNamespace}" : '') : $namespace . ($widgetNamespace !== '' ? "\\{$widgetNamespace}" : ''),
'namespace' => ! $panel
? $namespace . ($widgetNamespace !== '' ? "\\{$widgetNamespace}" : '')
: (filled($resource)
? "{$resourceNamespace}\\{$resource}\\Widgets" . ($widgetNamespace !== '' ? "\\{$widgetNamespace}" : '')
: $namespace . ($widgetNamespace !== '' ? "\\{$widgetNamespace}" : '')),
]);
} else {
$this->copyStubToApp('Widget', $path, [
'class' => $widgetClass,
'namespace' => filled($resource) ? "{$resourceNamespace}\\{$resource}\\Widgets" . ($widgetNamespace !== '' ? "\\{$widgetNamespace}" : '') : $namespace . ($widgetNamespace !== '' ? "\\{$widgetNamespace}" : ''),
'namespace' => ! $panel
? $namespace . ($widgetNamespace !== '' ? "\\{$widgetNamespace}" : '')
: (filled($resource)
? "{$resourceNamespace}\\{$resource}\\Widgets" . ($widgetNamespace !== '' ? "\\{$widgetNamespace}" : '')
: $namespace . ($widgetNamespace !== '' ? "\\{$widgetNamespace}" : '')),
'view' => $view,
]);

Expand Down
30 changes: 30 additions & 0 deletions tests/src/Commands/WidgetCommandLivewireTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Filament\Tests\Commands;

use Filament\Tests\TestCase;

use function Pest\Laravel\artisan;

uses(TestCase::class);

test('can create a table widget in livewire directory', function () {
$widgetPath = $this->app->basePath('app/Livewire/TestWidget.php');

if (file_exists($widgetPath)) {
unlink($widgetPath);
}

expect(file_exists($widgetPath))->toBeFalse();

artisan('make:filament-widget', [
'name' => 'TestWidget',
'--force' => true,
])
->expectsQuestion('What type of widget do you want to create?', 'Table')
->expectsQuestion('What is the resource you would like to create this in?', '')
->expectsQuestion('Where would you like to create this?', 'App\\Livewire alongside other Livewire components')
->assertSuccessful();

expect(file_exists($widgetPath))->toBeTrue();
});
2 changes: 1 addition & 1 deletion tests/src/Panels/Resources/Pages/EditRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
$transactionLevel = DB::transactionLevel();

try {
livewire(PostResource\Pages\EditPost::class, [
livewire(EditPost::class, [
'record' => $post->getKey(),
])
->callAction('randomize_title');
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Panels/Resources/Pages/ListRecordsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
$transactionLevel = DB::transactionLevel();

try {
livewire(PostResource\Pages\ListPosts::class)
livewire(ListPosts::class)
->callTableAction('randomize_title', $post);
} catch (Exception $e) {
// This can be catched and handled somewhere else, code continues...
Expand Down