From 7ac298ffd53efd3553830c38498517cd7870e17f Mon Sep 17 00:00:00 2001 From: Boy132 Date: Fri, 4 Jul 2025 10:15:59 +0200 Subject: [PATCH] automatically discover providers & commands --- app/Console/Commands/Plugin/Plugin.stub | 7 ------- app/Contracts/Plugins/HasPluginCommands.php | 11 ----------- app/Models/Plugin.php | 14 ++++---------- app/Services/Helpers/PluginService.php | 4 ++-- 4 files changed, 6 insertions(+), 30 deletions(-) delete mode 100644 app/Contracts/Plugins/HasPluginCommands.php diff --git a/app/Console/Commands/Plugin/Plugin.stub b/app/Console/Commands/Plugin/Plugin.stub index 2597398bf..79ce24959 100644 --- a/app/Console/Commands/Plugin/Plugin.stub +++ b/app/Console/Commands/Plugin/Plugin.stub @@ -35,11 +35,4 @@ class $class$ implements Plugin return $plugin; } - - public static function getProviders(): array - { - return [ - \$namespace$\Providers\$class$Provider::class, - ]; - } } diff --git a/app/Contracts/Plugins/HasPluginCommands.php b/app/Contracts/Plugins/HasPluginCommands.php deleted file mode 100644 index 7b702ae61..000000000 --- a/app/Contracts/Plugins/HasPluginCommands.php +++ /dev/null @@ -1,11 +0,0 @@ -fullClass(); - if (class_exists($class) && method_exists($class, 'getProviders')) { - return ($class)::getProviders(); - } + $providers = File::allFiles(plugin_path($this->id, 'src', 'Providers')); - return []; + return array_map(fn ($provider) => $this->namespace . '\\Providers\\' . str($provider->getRelativePathname())->remove('.php', false), $providers); } /** @@ -275,11 +272,8 @@ class Plugin extends Model implements HasPluginSettings */ public function getCommands(): array { - $class = $this->fullClass(); - if (class_exists($class) && method_exists($class, 'getCommands')) { - return ($class)::getCommands(); - } + $providers = File::allFiles(plugin_path($this->id, 'src', 'Console', 'Commands')); - return []; + return array_map(fn ($provider) => $this->namespace . '\\Console\\Commands\\' . str($provider->getRelativePathname())->remove('.php', false), $providers); } } diff --git a/app/Services/Helpers/PluginService.php b/app/Services/Helpers/PluginService.php index b9f48591d..11be8bd81 100644 --- a/app/Services/Helpers/PluginService.php +++ b/app/Services/Helpers/PluginService.php @@ -62,7 +62,7 @@ class PluginService // Register service providers foreach ($plugin->getProviders() as $provider) { if (!class_exists($provider) || !is_subclass_of($provider, ServiceProvider::class)) { - throw new Exception('Provider class "' . $provider . '" not found'); + continue; } $this->app->register($provider); @@ -71,7 +71,7 @@ class PluginService // Resolve artisan commands foreach ($plugin->getCommands() as $command) { if (!class_exists($command) || !is_subclass_of($command, Command::class)) { - throw new Exception('Command class "' . $command . '" not found'); + continue; } ConsoleApplication::starting(function ($artisan) use ($command) {