Skip to main content
The PluginRegistry class manages plugin registration and resolution. Plugins extend Laravel Modular’s functionality and are resolved as singletons from the service container.

Class Reference

InterNACHI\Modular\PluginRegistry

Constructor

public function __construct(
    protected Container $container,
)
container
Container
required
The Laravel service container instance

Static Methods

register()

Statically register one or more plugins with the registry.
public static function register(string ...$class): void
class
class-string<Plugin>...
required
One or more fully qualified plugin class names to register
Example:
use InterNACHI\Modular\PluginRegistry;
use App\Plugins\CustomGenerator;
use App\Plugins\ModuleAnalyzer;

// Register a single plugin
PluginRegistry::register(CustomGenerator::class);

// Register multiple plugins
PluginRegistry::register(
    CustomGenerator::class,
    ModuleAnalyzer::class
);
This is a convenience method that resolves the registry from the container and calls the add() method.

Instance Methods

add()

Add one or more plugins to the registry.
public function add(string ...$class): static
class
class-string<Plugin>...
required
One or more fully qualified plugin class names to add
return
static
Returns the registry instance for method chaining
Example:
use InterNACHI\Modular\PluginRegistry;
use App\Plugins\CustomGenerator;
use App\Plugins\ModuleAnalyzer;

$registry = app(PluginRegistry::class);

$registry
    ->add(CustomGenerator::class)
    ->add(ModuleAnalyzer::class);

// Or add multiple at once
$registry->add(
    CustomGenerator::class,
    ModuleAnalyzer::class
);
Plugins are stored using their class name as a key. Adding the same plugin multiple times has no effect.

get()

Resolve and retrieve a plugin instance from the container.
public function get(string $plugin, array $parameters = []): Plugin
plugin
class-string<Plugin>
required
The fully qualified class name of the plugin to retrieve
parameters
array
Optional constructor parameters to pass when resolving the plugin
return
Plugin
The resolved plugin instance
throws
InvalidArgumentException
Thrown when attempting to get a plugin that hasn’t been registered
Example:
use InterNACHI\Modular\PluginRegistry;
use App\Plugins\CustomGenerator;

$registry = app(PluginRegistry::class);

// Get a plugin
$generator = $registry->get(CustomGenerator::class);

// Get a plugin with constructor parameters
$generator = $registry->get(CustomGenerator::class, [
    'option' => 'value',
]);
Plugins are resolved as singletons. If you pass new parameters to an already-resolved plugin, the instance is recreated with the new parameters and stored in the container.

all()

Get all registered plugin class names.
public function all(): array
return
class-string<Plugin>[]
An array of registered plugin class names
Example:
use InterNACHI\Modular\PluginRegistry;

$registry = app(PluginRegistry::class);

$plugins = $registry->all();

foreach ($plugins as $pluginClass) {
    echo "Registered plugin: {$pluginClass}\n";
}

Usage Examples

Registering Plugins in a Service Provider

use Illuminate\Support\ServiceProvider;
use InterNACHI\Modular\PluginRegistry;
use App\Plugins\CustomGenerator;
use App\Plugins\ModuleAnalyzer;

class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        PluginRegistry::register(
            CustomGenerator::class,
            ModuleAnalyzer::class
        );
    }
}

Creating a Custom Plugin

use InterNACHI\Modular\Plugins\Plugin;
use Illuminate\Console\Command;

class CustomGenerator extends Plugin
{
    public function boot(): void
    {
        // Plugin initialization logic
    }
    
    public function commands(): array
    {
        return [
            // Return custom commands
        ];
    }
}

Resolving and Using Plugins

use InterNACHI\Modular\PluginRegistry;
use App\Plugins\ModuleAnalyzer;

$registry = app(PluginRegistry::class);

// Resolve the plugin
$analyzer = $registry->get(ModuleAnalyzer::class);

// Use the plugin
$results = $analyzer->analyzeModule('blog');

Checking Registered Plugins

use InterNACHI\Modular\PluginRegistry;
use App\Plugins\CustomGenerator;

$registry = app(PluginRegistry::class);

// Check if a plugin is registered
$plugins = $registry->all();

if (in_array(CustomGenerator::class, $plugins)) {
    $generator = $registry->get(CustomGenerator::class);
    // Use the plugin
}

Error Handling

use InterNACHI\Modular\PluginRegistry;
use App\Plugins\CustomGenerator;
use InvalidArgumentException;

$registry = app(PluginRegistry::class);

try {
    $plugin = $registry->get(CustomGenerator::class);
} catch (InvalidArgumentException $e) {
    // Plugin not registered
    logger()->error("Plugin not found: {$e->getMessage()}");
    
    // Register it first
    $registry->add(CustomGenerator::class);
    $plugin = $registry->get(CustomGenerator::class);
}

Using Constructor Parameters

use InterNACHI\Modular\PluginRegistry;
use App\Plugins\ConfigurablePlugin;

$registry = app(PluginRegistry::class);

// First resolution with parameters
$plugin = $registry->get(ConfigurablePlugin::class, [
    'mode' => 'development',
]);

// Subsequent resolution with different parameters
// This will recreate the plugin instance
$plugin = $registry->get(ConfigurablePlugin::class, [
    'mode' => 'production',
]);

See Also

Build docs developers (and LLMs) love