mirror of
https://github.com/filamentphp/filament.git
synced 2026-09-24 15:42:09 +08:00
Allow widgets to be generated outside of the app
This commit is contained in:
@@ -79,7 +79,7 @@ class MakePageCommand extends Command
|
||||
$context = (count($contexts) > 1) ? $contexts[$this->choice(
|
||||
'Which context would you like to create this in?',
|
||||
array_map(
|
||||
fn (Context $context) => $context->getId(),
|
||||
fn (Context $context): string => $context->getId(),
|
||||
$contexts,
|
||||
),
|
||||
Filament::getDefaultContext()->getId(),
|
||||
@@ -94,7 +94,7 @@ class MakePageCommand extends Command
|
||||
$view = str($page)
|
||||
->prepend(
|
||||
(string) str($resource === null ? "{$namespace}\\" : "{$resourceNamespace}\\{$resource}\\pages\\")
|
||||
->replace('App\\', '')
|
||||
->replaceFirst('App\\', '')
|
||||
)
|
||||
->replace('\\', '/')
|
||||
->explode('/')
|
||||
|
||||
@@ -55,7 +55,7 @@ class MakeRelationManagerCommand extends Command
|
||||
$context = (count($contexts) > 1) ? $contexts[$this->choice(
|
||||
'Which context would you like to create this in?',
|
||||
array_map(
|
||||
fn (Context $context) => $context->getId(),
|
||||
fn (Context $context): string => $context->getId(),
|
||||
$contexts,
|
||||
),
|
||||
Filament::getDefaultContext()->getId(),
|
||||
|
||||
@@ -60,7 +60,7 @@ class MakeResourceCommand extends Command
|
||||
$context = (count($contexts) > 1) ? $contexts[$this->choice(
|
||||
'Which context would you like to create this in?',
|
||||
array_map(
|
||||
fn (Context $context) => $context->getId(),
|
||||
fn (Context $context): string => $context->getId(),
|
||||
$contexts,
|
||||
),
|
||||
Filament::getDefaultContext()->getId(),
|
||||
|
||||
@@ -93,7 +93,6 @@ class FilamentServiceProvider extends PackageServiceProvider
|
||||
Commands\MakeRelationManagerCommand::class,
|
||||
Commands\MakeResourceCommand::class,
|
||||
Commands\MakeUserCommand::class,
|
||||
Commands\MakeWidgetCommand::class,
|
||||
];
|
||||
|
||||
$aliases = [];
|
||||
|
||||
+2
-2
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Filament\Commands\Aliases;
|
||||
namespace Filament\Widgets\Commands\Aliases;
|
||||
|
||||
use Filament\Commands;
|
||||
use Filament\Widgets\Commands;
|
||||
|
||||
class MakeWidgetCommand extends Commands\MakeWidgetCommand
|
||||
{
|
||||
+48
-40
@@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Filament\Commands;
|
||||
namespace Filament\Widgets\Commands;
|
||||
|
||||
use Arr;
|
||||
use Filament\Context;
|
||||
use Filament\Facades\Filament;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Support\Commands\Concerns\CanManipulateFiles;
|
||||
use Filament\Support\Commands\Concerns\CanValidateInput;
|
||||
use Illuminate\Console\Command;
|
||||
@@ -34,52 +34,60 @@ class MakeWidgetCommand extends Command
|
||||
$resource = null;
|
||||
$resourceClass = null;
|
||||
|
||||
$resourceInput = $this->option('resource') ?? $this->ask('(Optional) Resource (e.g. `BlogPostResource`)');
|
||||
if (class_exists(Resource::class)) {
|
||||
$resourceInput = $this->option('resource') ?? $this->ask('(Optional) Resource (e.g. `BlogPostResource`)');
|
||||
|
||||
if ($resourceInput !== null) {
|
||||
$resource = (string) str($resourceInput)
|
||||
->studly()
|
||||
->trim('/')
|
||||
->trim('\\')
|
||||
->trim(' ')
|
||||
->replace('/', '\\');
|
||||
if ($resourceInput !== null) {
|
||||
$resource = (string) str($resourceInput)
|
||||
->studly()
|
||||
->trim('/')
|
||||
->trim('\\')
|
||||
->trim(' ')
|
||||
->replace('/', '\\');
|
||||
|
||||
if (! str($resource)->endsWith('Resource')) {
|
||||
$resource .= 'Resource';
|
||||
if (! str($resource)->endsWith('Resource')) {
|
||||
$resource .= 'Resource';
|
||||
}
|
||||
|
||||
$resourceClass = (string) str($resource)
|
||||
->afterLast('\\');
|
||||
}
|
||||
}
|
||||
|
||||
$context = null;
|
||||
|
||||
if (class_exists(Context::class)) {
|
||||
$context = $this->option('context');
|
||||
|
||||
if ($context) {
|
||||
$context = Filament::getContext($context);
|
||||
}
|
||||
|
||||
$resourceClass = (string) str($resource)
|
||||
->afterLast('\\');
|
||||
if (! $context) {
|
||||
$contexts = Filament::getContexts();
|
||||
|
||||
/** @var ?Context $context */
|
||||
$context = $contexts[$this->choice(
|
||||
'Where would you like to create this widget?',
|
||||
array_unique(array_merge(
|
||||
array_map(
|
||||
fn (Context $context): string => $context->getWidgetNamespace() ?? 'App\\Filament\\Widgets',
|
||||
$contexts,
|
||||
),
|
||||
['' => 'App\\Http\\Livewire' . ($widgetNamespace ? '\\' . $widgetNamespace : '')],
|
||||
)),
|
||||
)] ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
$context = $this->option('context');
|
||||
|
||||
if ($context) {
|
||||
$context = Filament::getContext($context);
|
||||
}
|
||||
|
||||
if (! $context) {
|
||||
$contexts = Filament::getContexts();
|
||||
|
||||
/** @var Context $context */
|
||||
$context = (count($contexts) > 1) ? $contexts[$this->choice(
|
||||
'Which context would you like to create this in?',
|
||||
array_map(
|
||||
fn (Context $context) => $context->getId(),
|
||||
$contexts,
|
||||
),
|
||||
Filament::getDefaultContext()->getId(),
|
||||
)] : Arr::first($contexts);
|
||||
}
|
||||
|
||||
$path = $context->getWidgetDirectory() ?? app_path('Filament/Widgets/');
|
||||
$namespace = $context->getWidgetNamespace() ?? 'App\\Filament\\Widgets';
|
||||
$resourcePath = $context->getResourceDirectory() ?? app_path('Filament/Resources/');
|
||||
$resourceNamespace = $context->getResourceNamespace() ?? 'App\\Filament\\Resources';
|
||||
$path = $context ? ($context->getWidgetDirectory() ?? app_path('Filament/Widgets/')) : app_path('Http/Livewire/');
|
||||
$namespace = $context ? ($context->getWidgetNamespace() ?? 'App\\Filament\\Widgets') : 'App\\Http\\Livewire';
|
||||
$resourcePath = $context ? ($context->getResourceDirectory() ?? app_path('Filament/Resources/')) : null;
|
||||
$resourceNamespace = $context ? ($context->getResourceNamespace() ?? 'App\\Filament\\Resources') : null;
|
||||
|
||||
$view = str($widget)->prepend(
|
||||
(string) str($resource === null ? "{$namespace}\\" : "{$resourceNamespace}\\{$resource}\\widgets\\")
|
||||
->replace('App\\', '')
|
||||
(string) str($resource === null ? ($context ? "{$namespace}\\" : 'livewire\\') : "{$resourceNamespace}\\{$resource}\\widgets\\")
|
||||
->replaceFirst('App\\', '')
|
||||
)
|
||||
->replace('\\', '/')
|
||||
->explode('/')
|
||||
@@ -14,6 +14,7 @@ class WidgetsServiceProvider extends PackageServiceProvider
|
||||
{
|
||||
$package
|
||||
->name('filament-widgets')
|
||||
->hasCommands($this->getCommands())
|
||||
->hasViews();
|
||||
}
|
||||
|
||||
@@ -26,4 +27,28 @@ class WidgetsServiceProvider extends PackageServiceProvider
|
||||
], 'filament/widgets');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string>
|
||||
*/
|
||||
protected function getCommands(): array
|
||||
{
|
||||
$commands = [
|
||||
Commands\MakeWidgetCommand::class,
|
||||
];
|
||||
|
||||
$aliases = [];
|
||||
|
||||
foreach ($commands as $command) {
|
||||
$class = 'Filament\\Widgets\\Commands\\Aliases\\' . class_basename($command);
|
||||
|
||||
if (! class_exists($class)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$aliases[] = $class;
|
||||
}
|
||||
|
||||
return array_merge($commands, $aliases);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user