Skip to content

Instantly share code, notes, and snippets.

@shameerc
Created December 11, 2010 08:54
Show Gist options
  • Save shameerc/737265 to your computer and use it in GitHub Desktop.
Save shameerc/737265 to your computer and use it in GitHub Desktop.
Simple plugin system using Reflection api
<?php
/**
* Application class
*
* @author Shameer
*/
class Application {
private $plugins = array();
public function __construct() {
// Constructor
}
public function run()
{
//simply start a new instance of plugin to use in this example
$newsLetter = new Newsletter();
//Load in the plugin reflection classes
$plugins = $this->bindPlugins();
//Build the final menu
$menu = $this->buildMenu();
print_r($menu);
}
public function bindPlugins()
{
//get all instantiated classes
$classes = get_declared_classes();
//check which all classes implements the basic Plugin interface
foreach($classes as $class) {
$reflectClass = new ReflectionClass($class) ;
if($reflectClass->implementsInterface('Plugin')) {
//We are actually storing the instance reflection class itself
//Not the class name or its object
$this->plugins[] = $reflectClass;
}
}
return $this->plugins;
}
public function buildMenu()
{
//Get the basic menus
$menu = $this->getMainMenu();
$result = $this->runEvent('adminMenu', $menu);
if(is_array($result)) {
$menu = array_merge($menu, $result);
}
return $menu;
}
function runEvent($method, $args) {
$return = array();
foreach($this->plugins as $plugin) {
if($plugin->hasMethod($method)) {
$reflectMethod = $plugin->getMethod($method);
//If the method is static we dont need the class instance
if($reflectMethod->isStatic()) {
$list = $reflectMethod->invoke($args);
}
else {
//Create a new instance of the class
$instance = $plugin->newInstance();
$list = $reflectMethod->invoke($instance,$args);
}
$return = array_merge($return, $list);
}
}
return $return;
}
public function getMainMenu()
{
return array('Articles','News','Blog');
}
}
?>
<?php
/**
* Index file
*
* @author Shameer
*/
error_reporting(E_ALL);
require'Autoload.php';
$app = new Application();
$app->run();
?>
<?php
/**
* Newsletter plugin
*
* @author Shameer
*/
class Newsletter implements Plugin{
public function init()
{
echo "Newsletter Plugin init";
}
public function adminMenu()
{
return array('Newsletter settings');
}
}
?>
<?php
/**
* Plugin interface. All plugin must implement this interface
*
* @author Shameer
*/
interface Plugin {
function init();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment