How to making extension support for your PHP project using “plugin-system” library

Isa Eken
2 min readJul 31, 2021

At the end of this tutorial, we will have a plugin supported web page.

First Step

First, there is the “composer”. If you don’t know what a composer is, please research and practice it first.

You need add to “plugin-system” library to your project.

composer require isaeken/plugin-system

The Base Of Project

Suppose we have a system with the following file tree.

`
|- /app
|- payment_callback.php
|- /plugins
|- ExamplePlugin.php
|- TestingPlugin.php
|- /vendor
|- /public
|- index.php

firstly we need add `PluginSystem` class to bootstrap file.

<?php
// /public/index.php
use IsaEken\PluginSystem\Interfaces\PluginInterface;
use IsaEken\PluginSystem\PluginSystem;
// include the autoloader.
require_once __DIR__ . '/../vendor/autoload.php';
// create a instance
$pluginSystem = new PluginSystem(__DIR__ . '/../plugins');
// load all plugins in plugins directory
$pluginSystem->autoload();

// call in all loaded and enabled plugins
$pluginSystem->execute('init');
$routes[] = '/';
// ...

Creating a Plugin

<?php
// /plugins/ExamplePlugin.php
namespace YourNamespace;use IsaEken\PluginSystem\Plugin;class YourPlugin extends Plugin {
public string $name = 'plugin-name';
public string $author = 'Your Name';
public function init() {
// ...
}
public function payment() {
// ...
}
}

return YourPlugin::class;

If you add namespace you need to return it at the end of the plugin.

Disabling/Enabling a Plugin

<?php$pluginSystem->plugins()->each(function (PluginInterface $plugin) {
if ($plugin->isEnabled() && $plugin->name == 'example-plugin') {
// disable the plugin
$plugin->disable();
} else {
$plugin->enable();
}
});

Calling A Method In All Plugins

<?php
// /app/payment_callback.php
// ... bootstrap your application ...$pluginSystem->plugins()->each(function (PluginInterface $plugin) {
if ($plugin->isEnabled() && $plugin->category == 'payment') {
// execute method with arguments in current plugin.
$plugin->execute('callback', 'cc_number', 'customer_name', 'price');
}
});

That Is All! Make Awesome Projects!

More info & Documentation: plugin-system

--

--