From b3d8daa9b9f3941b5f8ea1c02d18c3077bf28ada Mon Sep 17 00:00:00 2001 From: Fady Mondy Date: Mon, 16 Sep 2024 14:50:02 +0300 Subject: [PATCH] add package to module converter command --- README.md | 10 +- resources/views/pages/table.blade.php | 2 +- src/Console/FilamentPageGenerate.php | 36 ++++--- src/Console/FilamentPublishModule.php | 111 ++++++++++++++++++++++ src/Console/FilamentResourceGenerate.php | 15 ++- src/Console/FilamentWidgetGenerate.php | 33 ++++--- src/FilamentPluginsPlugin.php | 4 +- src/FilamentPluginsServiceProvider.php | 17 +++- src/Models/Plugin.php | 81 +++++----------- src/Pages/Plugins.php | 33 ++++--- src/Resources/TableResource.php | 9 +- src/Services/Concerns/GenerateFolders.php | 40 ++++---- src/Services/Concerns/GenerateModel.php | 31 ++++-- 13 files changed, 280 insertions(+), 142 deletions(-) create mode 100644 src/Console/FilamentPublishModule.php diff --git a/README.md b/README.md index 172f637..9797f2d 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ it will generate the files for you and you can use it directly, please note that ## List your package on the plugins list -you can list your package on the plugins list by adding this a json file in your package root folder with name `filament-plugin.json` with content like this: +you can list your package on the plugins list by adding this a json file in your package root folder with name `module.json` with content like this: ```json { @@ -122,6 +122,14 @@ make sure you allow packages scan on the `filament-plugins.php` config file 'scan' => true ``` +now you can publish your package as module with the very easy way use this command + +```php +php artisan filament-plugins:publish +``` + +and input your package name from the list and it will move it to your module folder and register the provider to you, so you can custom anything you like on the package. + ## Use Selected Module in your panel you can use the selected module in your panel by using this code in your PanelProvider diff --git a/resources/views/pages/table.blade.php b/resources/views/pages/table.blade.php index 6bbfeef..447d821 100644 --- a/resources/views/pages/table.blade.php +++ b/resources/views/pages/table.blade.php @@ -29,7 +29,7 @@
@if($item['type'] !== 'lib') - @if((bool)config('filament-plugins.allow_generator')) + @if(((bool)config('filament-plugins.allow_generator') ) && !str(module_path($item['module_name']))->contains('vendor')) diff --git a/src/Console/FilamentPageGenerate.php b/src/Console/FilamentPageGenerate.php index d523a5f..4db5d92 100644 --- a/src/Console/FilamentPageGenerate.php +++ b/src/Console/FilamentPageGenerate.php @@ -28,6 +28,14 @@ public function handle(): int $moduleName = $this->argument('module') ?? text('In which Module should we create the page?', 'e.g Blog', required: true); $moduleStudlyName = str($moduleName)->studly()->toString(); $module = Module::find($moduleName); + + $module = Module::find($moduleName); + $appPath = 'app'; + $moduleDir = File::directories($module->getPath()); + if(in_array($module->getPath() .'/src', $moduleDir)){ + $appPath = 'src'; + } + $page = (string) str( $this->argument('name') ?? text( @@ -197,13 +205,13 @@ public function handle(): int //Create Directory For Selected Panel in Module If Not Exists if(count($pageDirectories) < 1 && count($pageNamespaces) < 1){ $modulePath = module_path($moduleName); - if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()))){ - File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId())); + if(!File::exists($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()))){ + File::makeDirectory($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId())); } - if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Pages')){ - File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Pages'); + if(!File::exists($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()).'/Pages')){ + File::makeDirectory($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()).'/Pages'); } - $pageDirectories[] = $modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Pages'; + $pageDirectories[] = $modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()).'/Pages'; $pageNamespaces[] = $module->appNamespace().'\\Filament\\'.Str::studly($panel->getId()).'\\Pages'; } @@ -224,20 +232,20 @@ public function handle(): int //Create Directory For Selected Panel in Module If Not Exists if(count($resourceDirectories) < 1 && count($resourceNamespaces) < 1){ $modulePath = module_path($moduleName); - if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()))){ - File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId())); + if(!File::exists($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()))){ + File::makeDirectory($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId())); } - if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource')){ - File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource'); + if(!File::exists($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()).'/Resource')){ + File::makeDirectory($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()).'/Resource'); } - if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource)){ - File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource); + if(!File::exists($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource)){ + File::makeDirectory($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource); } - if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Pages')){ - File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Pages'); + if(!File::exists($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Pages')){ + File::makeDirectory($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Pages'); } - $resourceDirectories[] = $modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Pages'; + $resourceDirectories[] = $modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Pages'; $resourceNamespaces[] = $module->appNamespace().'\\Filament\\'.Str::studly($panel->getId()).'\\Resource\\'.$resource.'\\Pages'; } diff --git a/src/Console/FilamentPublishModule.php b/src/Console/FilamentPublishModule.php new file mode 100644 index 0000000..3916265 --- /dev/null +++ b/src/Console/FilamentPublishModule.php @@ -0,0 +1,111 @@ +filter(function ($item){ + if(str($item->getPath())->contains('vendor')){ + return true; + } + + return false; + }); + $module = $this->argument('module') && $this->argument('module') != "0" ? $this->argument('module') : search( + label: 'Please input your module name you went to publish?', + options: fn (string $value) => strlen($value) > 0 + ? $modules->filter(function ($item, $key) use ($value){ + return str($item->getName())->contains($value) ? (string)$item->getName() : null; + })->toArray() + : [], + placeholder: "ex: FilamentAccounts", + scroll: 10 + ); + + $this->module = \Nwidart\Modules\Facades\Module::find($module); + + $this->info("Publishing module: {$module}"); + + $this->moveFolder(); + $this->registerProvider(); + $this->updateComposer(); + + + $this->info("Module: {$module} published successfully"); + } + + + public function moveFolder() + { + $basePath = $this->module->getPath(); + $newFolderName = $this->module->getStudlyName(); + + File::move($basePath, base_path("Modules/{$newFolderName}")); + + $this->newPath = base_path("Modules/{$newFolderName}"); + } + + public function updateComposer() + { + $composerJson = json_decode(File::get(base_path('composer.json')), true); + $packageName = str($this->module->getPath())->remove(base_path('/vendor/')); + $composerCollect = collect($composerJson['require'])->filter(function ($item, $key) use ($packageName){ + return !str($key)->contains($packageName); + }); + $composerJson['require'] = $composerCollect->toArray(); + + File::put(base_path('composer.json'), json_encode($composerJson, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES )); + + system('composer update'); + } + + public function registerProvider() + { + $info = json_decode(File::get($this->newPath . "/module.json")); + $providers = include base_path('bootstrap/providers.php'); + foreach ($info->providers as $provider){ + $providers[] = $provider; + } + + $array = ""; + foreach ($providers as $provider){ + $array .= "\t".$provider."::class,\n"; + } + File::put(base_path('bootstrap/providers.php'), "argument('module') ?? text('In which Module should we create the resource?', 'e.g Blog', required: true); $moduleStudlyName = str($moduleName)->studly()->toString(); $module = Module::find($moduleName); + $appPath = 'app'; + $moduleDir = File::directories($module->getPath()); + if(in_array($module->getPath() .'/src', $moduleDir)){ + $appPath = 'src'; + } $modelNamespace = $this->option('model-namespace') ?? $module->appNamespace('Models'); $modelNamespace = str($modelNamespace)->rtrim('\\')->toString(); @@ -91,13 +96,13 @@ public function handle(): int //Create Directory For Selected Panel in Module If Not Exists if(count($resourceDirectories) < 1 && count($resourceNamespaces) < 1){ $modulePath = module_path($moduleName); - if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()))){ - File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId())); + if(!File::exists($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()))){ + File::makeDirectory($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId())); } - if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resources')){ - File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resources'); + if(!File::exists($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Resources')){ + File::makeDirectory($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Resources'); } - $resourceDirectories[] = $modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resources'; + $resourceDirectories[] = $modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Resources'; $resourceNamespaces[] = $module->appNamespace().'\\Filament\\'.Str::studly($panel->getId()).'\\Resources'; } diff --git a/src/Console/FilamentWidgetGenerate.php b/src/Console/FilamentWidgetGenerate.php index 41f623a..de6ccd6 100644 --- a/src/Console/FilamentWidgetGenerate.php +++ b/src/Console/FilamentWidgetGenerate.php @@ -24,6 +24,11 @@ public function handle(): int $moduleName = $this->argument('module') ?? text('In which Module should we create this?', 'e.g Blog', required: true); $moduleStudlyName = str($moduleName)->studly()->toString(); $module = Module::find($moduleName); + $appPath = 'app'; + $moduleDir = File::directories($module->getPath()); + if(in_array($module->getPath() .'/src', $moduleDir)){ + $appPath = 'src'; + } $widget = (string) str($this->argument('name') ?? text( label: 'What is the widget name?', placeholder: 'BlogPostsChart', @@ -118,13 +123,13 @@ public function handle(): int //Create Directory For Selected Panel in Module If Not Exists if(count($widgetDirectories) < 1 && count($widgetNamespaces) < 1){ $modulePath = module_path($moduleName); - if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()))){ - File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId())); + if(!File::exists($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()))){ + File::makeDirectory($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId())); } - if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Widgets')){ - File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Widgets'); + if(!File::exists($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Widgets')){ + File::makeDirectory($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Widgets'); } - $widgetDirectories[] = $modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Widgets'; + $widgetDirectories[] = $modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Widgets'; $widgetNamespaces[] = $module->appNamespace().'\\Filament\\'.Str::studly($panel->getId()).'\\Widgets'; } @@ -144,20 +149,20 @@ public function handle(): int //Create Directory For Selected Panel in Module If Not Exists if(count($resourceDirectories) < 1 && count($resourceNamespaces) < 1){ $modulePath = module_path($moduleName); - if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()))){ - File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId())); + if(!File::exists($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()))){ + File::makeDirectory($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId())); } - if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource')){ - File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource'); + if(!File::exists($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Resource')){ + File::makeDirectory($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Resource'); } - if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource)){ - File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource); + if(!File::exists($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource)){ + File::makeDirectory($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource); } - if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Widgets')){ - File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Widgets'); + if(!File::exists($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Widgets')){ + File::makeDirectory($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Widgets'); } - $resourceDirectories[] = $modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Widgets'; + $resourceDirectories[] = $modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Widgets'; $resourceNamespaces[] = $module->appNamespace().'\\Filament\\'.Str::studly($panel->getId()).'\\Resource\\'.$resource.'\\Widgets'; } diff --git a/src/FilamentPluginsPlugin.php b/src/FilamentPluginsPlugin.php index 72f931e..ae3d1e2 100644 --- a/src/FilamentPluginsPlugin.php +++ b/src/FilamentPluginsPlugin.php @@ -34,7 +34,8 @@ public function register(Panel $panel): void foreach ($plugins as $plugin){ if($plugin->type === 'plugin' && in_array($plugin->module_name, $this->modules)){ $module = Module::find($plugin->module_name); - if($module->isEnabled()){ + $dir = File::directories($module->getPath()); + if($module->isEnabled() && !in_array($module->getPath() . DIRECTORY_SEPARATOR . 'src', $dir)){ $checkIfThereIsDirectoryForThisPanel = File::exists($module->appPath('Filament' . DIRECTORY_SEPARATOR . Str::studly($panel->getId()))); if($checkIfThereIsDirectoryForThisPanel && $this->discoverCurrentPanelOnly){ $panel->discoverPages( @@ -92,7 +93,6 @@ public function register(Panel $panel): void ); } } - } } } diff --git a/src/FilamentPluginsServiceProvider.php b/src/FilamentPluginsServiceProvider.php index a53f34b..d710857 100644 --- a/src/FilamentPluginsServiceProvider.php +++ b/src/FilamentPluginsServiceProvider.php @@ -3,6 +3,7 @@ namespace TomatoPHP\FilamentPlugins; use Illuminate\Foundation\Application; +use Illuminate\Support\Facades\File; use Illuminate\Support\ServiceProvider; use Nwidart\Modules\Laravel\Module; @@ -19,6 +20,7 @@ public function register(): void \TomatoPHP\FilamentPlugins\Console\FilamentWidgetGenerate::class, \TomatoPHP\FilamentPlugins\Console\FilamentPluginsGenerate::class, \TomatoPHP\FilamentPlugins\Console\FilamentPluginsModel::class, + \TomatoPHP\FilamentPlugins\Console\FilamentPublishModule::class, ]); //Register Config file @@ -56,6 +58,7 @@ public function register(): void $this->loadRoutesFrom(__DIR__.'/../routes/web.php'); $this->registerModuleMacros(); + } public function boot(): void @@ -83,10 +86,21 @@ protected function registerModuleMacros(): void $relativeNamespace = trim($relativeNamespace, '\\'); $relativeNamespace = $relativeNamespace; - return $this->namespace($relativeNamespace); + $dir = File::directories($this->getPath()); + $appPath = $this->namespace($relativeNamespace); + if(in_array($this->getPath() . '/src', $dir)){ + $info = json_decode(File::get($this->getPath() . '/module.json')); + $appPath = str($this->namespace($relativeNamespace))->replace('Modules', str($info->providers[0])->explode('\\')->first())->toString(); + } + + return $appPath; }); Module::macro('appPath', function (string $relativePath = '') { + $dir = File::directories($this->getPath()); $appPath = $this->getExtraPath('app'); + if(in_array($this->getPath() . '/src', $dir)){ + $appPath = $this->getPath() . '/src'; + } return $appPath . ($relativePath ? DIRECTORY_SEPARATOR . $relativePath : ''); }); @@ -120,6 +134,7 @@ protected function registerModuleMacros(): void return $appPath . ($relativePath ? DIRECTORY_SEPARATOR . $relativePath : ''); }); + } } diff --git a/src/Models/Plugin.php b/src/Models/Plugin.php index 0003ba4..73d03c2 100644 --- a/src/Models/Plugin.php +++ b/src/Models/Plugin.php @@ -8,6 +8,7 @@ use Nwidart\Modules\Facades\Module; use Sushi\Sushi; use TomatoPHP\FilamentPlugins\Facades\FilamentPlugins; +use function Pest\Laravel\json; class Plugin extends Model { @@ -28,76 +29,38 @@ class Plugin extends Model 'type' => 'string', ]; + public function getRows() { - $getPlugins = []; - if(File::exists(base_path('Modules'))){ - $getPlugins = collect(File::directories(base_path('Modules'))); - $getPlugins = $getPlugins->filter(function ($item) { - $json = json_decode(File::get($item . "/module.json")); - if (isset($json->type) && $json->type === 'plugin'){ - return true; - } - else { - return false; + $plugins = collect(Module::all()) + ->filter(function ($module){ + if(File::exists($module->getPath())){ + $info = json_decode(File::get($module->getPath() . "/module.json")); + if(isset($info->title)){ + return true; + } } - })->transform(callback: static function($item){ - $info = json_decode(File::get($item . "/module.json")); + + return false; + }) + ->map(function ($module){ + $info = json_decode(File::get($module->getPath() . "/module.json")); return [ "module_name" => $info->name, "name" => json_encode($info->title), "description" => json_encode($info->description), - "color" => $info->color, - "placeholder" => $info->placeholder, - "version" => $info->version, - "type" => $info->type, - "icon" => $info->icon, + "color" => $info->color??null, + "placeholder" => $info->placeholder??null, + "version" => $info->version??null, + "type" => $info->type??null, + "icon" => $info->icon??null, "github" => isset($info->github)?$info->github:null, "docs" => isset($info->docs)?$info->docs:null, "active" => Module::find($info->name)->isEnabled(), - "providers" => json_encode($info->providers) + "providers" => isset($info->providers)?json_encode($info->providers):null, ]; - }); - } - - $providersPlugins = []; - if(config('filament-plugins.scan')){ - $getVendorPathes = File::directories(base_path('vendor')); - foreach ($getVendorPathes as $item){ - $checkInsideDir = File::directories($item); - foreach ($checkInsideDir as $dir){ - $getDirFiles = File::files($dir); - foreach ($getDirFiles as $file){ - if (str($file->getFilename())->contains('filament-plugin.json')){ - $info = json_decode($file->getContents()); - $providersPlugins[] = [ - "module_name" => $info->name, - "name" => json_encode($info->title), - "description" => json_encode($info->description), - "color" => $info->color, - "type" => $info->type, - "placeholder" => $info->placeholder, - "version" => $info->version, - "icon" => $info->icon, - "github" => isset($info->github)?$info->github:null, - "docs" => isset($info->docs)?$info->docs:null, - "active" => false, - "providers" => json_encode($info->providers) - ]; - } - } - } - } - } - - if(is_array($getPlugins)){ - $values = array_values($getPlugins); - } - else { - $values = array_values($getPlugins->toArray()); - } - + })->toArray(); - return array_merge($values, array_values($providersPlugins)); + return array_values($plugins); } } diff --git a/src/Pages/Plugins.php b/src/Pages/Plugins.php index 27d937a..ad5bb88 100644 --- a/src/Pages/Plugins.php +++ b/src/Pages/Plugins.php @@ -24,6 +24,7 @@ use TomatoPHP\FilamentIcons\Components\IconPicker; use TomatoPHP\FilamentPlugins\Models\Plugin; use TomatoPHP\FilamentPlugins\Services\PluginGenerator; +use TomatoPHP\FilamentPlugins\Services\PublishPackage; class Plugins extends Page implements HasTable { @@ -39,7 +40,7 @@ public function getTitle(): string return trans('filament-plugins::messages.plugins.title'); } - public static function getNavigationLabel(): string + public static function getNavigationLabel(): string { return trans('filament-plugins::messages.plugins.title'); } @@ -76,8 +77,8 @@ public function disableAction(): Action $module?->disable(); Notification::make() - ->title(trans('filament-plugins::messages.plugins.notificationss.disabled.title')) - ->body(trans('filament-plugins::messages.plugins.notificationss.disabled.body')) + ->title(trans('filament-plugins::messages.plugins.notifications.disabled.title')) + ->body(trans('filament-plugins::messages.plugins.notifications.disabled.body')) ->success() ->send(); @@ -88,6 +89,15 @@ public function disableAction(): Action public function deleteAction(): Action { return Action::make('delete') + ->visible(function (array $arguments) { + $module = Module::find($arguments['item']['module_name']); + if(str($module->getPath())->contains('vendor')){ + return false; + } + else { + return true; + } + }) ->iconButton() ->icon('heroicon-s-trash') ->color('danger') @@ -98,8 +108,8 @@ public function deleteAction(): Action $module?->delete(); Notification::make() - ->title(trans('filament-plugins::messages.plugins.notificationss.deleted.title')) - ->body(trans('filament-plugins::messages.plugins.notificationss.deleted.body')) + ->title(trans('filament-plugins::messages.plugins.notifications.deleted.title')) + ->body(trans('filament-plugins::messages.plugins.notifications.deleted.body')) ->success() ->send(); @@ -107,7 +117,6 @@ public function deleteAction(): Action }); } - public function activeAction(): Action { return Action::make('active') @@ -119,8 +128,8 @@ public function activeAction(): Action ->action(function (array $arguments) { if(!class_exists(json_decode($arguments['item']['providers'])[0])){ Notification::make() - ->title(trans('filament-plugins::messages.plugins.notificationss.autoload.title')) - ->body(trans('filament-plugins::messages.plugins.notificationss.autoload.body')) + ->title(trans('filament-plugins::messages.plugins.notifications.autoload.title')) + ->body(trans('filament-plugins::messages.plugins.notifications.autoload.body')) ->danger() ->send(); return; @@ -129,8 +138,8 @@ public function activeAction(): Action $module?->enable(); Notification::make() - ->title(trans('filament-plugins::messages.plugins.notificationss.enabled.title')) - ->body(trans('filament-plugins::messages.plugins.notificationss.enabled.body')) + ->title(trans('filament-plugins::messages.plugins.notifications.enabled.title')) + ->body(trans('filament-plugins::messages.plugins.notifications.enabled.body')) ->success() ->send(); @@ -195,8 +204,8 @@ public function importPlugin(array $data) $zip->close(); Notification::make() - ->title(trans('filament-plugins::messages.plugins.notificationss.import.title')) - ->body(trans('filament-plugins::messages.plugins.notificationss.import.body')) + ->title(trans('filament-plugins::messages.plugins.notifications.import.title')) + ->body(trans('filament-plugins::messages.plugins.notifications.import.body')) ->success() ->send(); diff --git a/src/Resources/TableResource.php b/src/Resources/TableResource.php index e5168c3..7d6e0b7 100644 --- a/src/Resources/TableResource.php +++ b/src/Resources/TableResource.php @@ -161,7 +161,14 @@ public static function table(Table $table): Table return; } - $checkIfModelExists = File::exists(module_path($record->module, '/app/Models/' . Str::ucfirst(Str::singular(Str::camel($record->name))) . '.php')); + $module = Module::find($record->module); + $appPath = 'app'; + $moduleDir = File::directories($module->getPath()); + if(in_array($module->getPath() .'/src', $moduleDir)){ + $appPath = 'src'; + } + + $checkIfModelExists = File::exists(module_path($record->module, '/'.$appPath.'/Models/' . Str::ucfirst(Str::singular(Str::camel($record->name))) . '.php')); if((!$checkIfModelExists) && in_array($data['type'], ['resource', 'page', 'widget'])){ diff --git a/src/Services/Concerns/GenerateFolders.php b/src/Services/Concerns/GenerateFolders.php index a1448ad..cb9d2c7 100644 --- a/src/Services/Concerns/GenerateFolders.php +++ b/src/Services/Concerns/GenerateFolders.php @@ -4,36 +4,30 @@ use Illuminate\Support\Facades\File; use Illuminate\Support\Str; +use Nwidart\Modules\Facades\Module; trait GenerateFolders { private function generateFolders(): void { - if($this->moduleName){ - $folders = [ - module_path($this->moduleName) ."/app/Http/Controllers/", - module_path($this->moduleName) ."/app/resources/", - module_path($this->moduleName) ."/app/Http/Requests/", - module_path($this->moduleName) ."/app/Http/Requests/{$this->modelName}", - module_path($this->moduleName) ."/app/Models/", - module_path($this->moduleName) . "/resources/views/" . str_replace('_', '-', $this->tableName), - module_path($this->moduleName) . "/routes", - module_path($this->moduleName)."/app/Tables", - module_path($this->moduleName)."/app/Forms", - ]; + $module = Module::find($this->moduleName); + $appPath = 'app'; + $moduleDir = File::directories($module->getPath()); + if(in_array($module->getPath() .'/src', $moduleDir)){ + $appPath = 'src'; } - else { + + if($this->moduleName){ $folders = [ - app_path("Http/Controllers") . "/Admin", - app_path("Http/Resources"), - app_path("Http/Requests"), - app_path("Http/Requests/Admin"), - app_path("Http/Requests/Admin/{$this->modelName}"), - app_path("Http/Resources") . "/Admin", - resource_path("views") . '/admin', - resource_path("views") . '/admin/' . str_replace('_', '-', $this->tableName), - base_path("routes"), - app_path("Tables") +// module_path($this->moduleName) .'/'.$appPath . "/Http/Controllers/", +// module_path($this->moduleName) .'/'.$appPath . "/resources/", +// module_path($this->moduleName) .'/'.$appPath . "/Http/Requests/", +// module_path($this->moduleName) .'/'.$appPath . "/Http/Requests/{$this->modelName}", + module_path($this->moduleName) .'/'.$appPath . "/Models/", +// module_path($this->moduleName) . "/resources/views/" . str_replace('_', '-', $this->tableName), +// module_path($this->moduleName) . "/routes", +// module_path($this->moduleName).'/'.$appPath . "/Tables", +// module_path($this->moduleName).'/'.$appPath . "/Forms", ]; } diff --git a/src/Services/Concerns/GenerateModel.php b/src/Services/Concerns/GenerateModel.php index 6de14c8..d42ccca 100644 --- a/src/Services/Concerns/GenerateModel.php +++ b/src/Services/Concerns/GenerateModel.php @@ -3,6 +3,8 @@ namespace TomatoPHP\FilamentPlugins\Services\Concerns; use Illuminate\Support\Facades\Artisan; +use Illuminate\Support\Facades\File; +use Nwidart\Modules\Facades\Module; trait GenerateModel { @@ -15,18 +17,29 @@ public function generateModel() $modelName = $this->modelName; $filePath = ""; + $module = Module::find($this->moduleName); + $appPath = 'app'; + $moduleDir = File::directories($module->getPath()); + if(in_array($module->getPath() .'/src', $moduleDir)){ + $appPath = 'src'; + } + if($this->moduleName){ - if(file_exists(module_path($this->moduleName) . '/app/Models/'. $this->modelName . '.php')){ + if(file_exists(module_path($this->moduleName) . '/'.$appPath . '/Models/'. $this->modelName . '.php')){ $exists = true; } - $namespace = "Modules\\{$this->moduleName}\\Models"; - $filePath = module_path($this->moduleName) . '/app/Models/'. $this->modelName . '.php'; - } - else if(file_exists(app_path("Models/{$this->modelName}.php"))){ - $exists = true; - $namespace = "App\\Models"; - $filePath = app_path("Models/{$this->modelName}.php"); + if($appPath === 'src'){ + $info = json_decode(File::get(module_path($this->moduleName) . '/module.json')); + $namespaceStart = str($info->providers[0])->explode('\\')->first(); + + $namespace = $namespaceStart . "\\{$this->moduleName}\\Models"; + } + else { + $namespace = "Modules\\{$this->moduleName}\\Models"; + } + + $filePath = module_path($this->moduleName) . '/'.$appPath . '/Models/'. $this->modelName . '.php'; } if(!$exists){ @@ -44,7 +57,7 @@ public function generateModel() "methods" => $this->getMethods(), ], [ - $this->moduleName ? module_path($this->moduleName) . '/app/Models/' : app_path("Models") + $this->moduleName ? module_path($this->moduleName) . '/'.$appPath . '/Models/' : app_path("Models") ] );