echo-fusion / pluginmanager
A flexible PHP package for managing and integrating plugins, allowing dynamic loading and registration based on application environments. Enhance the modularity and extensibility of your applications with ease.
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.0
- psr/container: ^2.0
Requires (Dev)
- echo-fusion/codestandard: ^1.0
- friendsofphp/php-cs-fixer: dev-master
- phpunit/phpunit: ^9.5
- vimeo/psalm: 5.x-dev
README
The PluginManager is a versatile package for managing and integrating plugins into your PHP applications. It allows you to dynamically load and register plugins based on the environment, making your application modular and extensible.
Installation
Install the package via Composer:
composer require echo-fusion/pluginmanager
Requirements
The following versions of PHP are supported by this version.
- PHP 8.1
- PHP 8.2
- PHP 8.3
Usage
Here’s how to use the PluginManager to set up and run:
- Instantiate the PluginManager with an array of plugins, specifying the environments in which they should be loaded:
use EchoFusion\PluginManager\PluginManager; use Psr\Container\ContainerInterface; $createPluginManager = function(ContainerInterface $container) { $plugins = [ MyPlugin::class, AnotherPlugin::class, ]; return new PluginManager($container, $plugins); }
- Register the plugins based on the current environment:
// Usage $container = // Your container implementation here $pluginManager = $createPluginManager($container); try { // Register plugins for a specific environment (e.g., 'dev') $pluginManager->register('dev'); } catch (PluginManagerException $e) { // Handle any exceptions related to plugin management echo 'Error registering plugins: ' . $e->getMessage(); } catch (Throwable $e) { // General fallback for other types of exceptions echo 'An unexpected error occurred: ' . $e->getMessage(); }
- Ensure that your plugins implement the PluginInterface:
<?php declare(strict_types=1); namespace YourNamespace\Plugins; use EchoFusion\PluginManager\PluginInterface; use EchoFusion\PluginManager\Environment; class ExamplePlugin implements PluginInterface { private $service; public function register(): void { // Initialize the service $this->service = $this->initializeService(); // Set up configurations $this->configureSettings(); // Register event listeners $this->registerEventListeners(); echo "ExamplePlugin has been registered successfully.\n"; } public function getSupportedEnvironments(): array { return ['dev','prod']; } //... }
Testing
Testing includes PHPUnit and PHPStan (Level 7).
$ composer test
Credits
Developed and maintained by Amir Shadanfar.
Connect on LinkedIn.
License
The MIT License (MIT). Please see License File for more information.