automatically discover providers & commands

This commit is contained in:
Boy132 2025-07-04 10:15:59 +02:00
parent 4db0702970
commit 7ac298ffd5
4 changed files with 6 additions and 30 deletions

View File

@ -35,11 +35,4 @@ class $class$ implements Plugin
return $plugin;
}
public static function getProviders(): array
{
return [
\$namespace$\Providers\$class$Provider::class,
];
}
}

View File

@ -1,11 +0,0 @@
<?php
namespace App\Contracts\Plugins;
interface HasPluginCommands
{
/**
* @return string[]
*/
public function getCommands(): array;
}

View File

@ -262,12 +262,9 @@ class Plugin extends Model implements HasPluginSettings
*/
public function getProviders(): array
{
$class = $this->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);
}
}

View File

@ -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) {