Skip to content

Instantly share code, notes, and snippets.

@tleilax
Forked from luniki/pluginengine.php
Created July 8, 2014 08:57
Show Gist options
  • Save tleilax/fe23e52fb2582ea6dc12 to your computer and use it in GitHub Desktop.
Save tleilax/fe23e52fb2582ea6dc12 to your computer and use it in GitHub Desktop.
<?
interface SomeRole {
public function foo();
public static function bar();
}
class A extends Plugin {}
class B extends A {
public function foo() {}
}
class C extends B {
public static function bar() {}
}
class D extends B {
public static function bar($param1, $param2) {}
}
class PluginManager {
function getPlugins($role)
{
$pluginClasses = range("A", "D");
return array_filter($pluginClasses, function ($plugin) use ($role) { return $plugin::does($role); });
}
}
$pm = new PluginManager();
var_dump($pm->getPlugins("SomeRole"));
class Plugin {
public static function does($role) {
// loadRole($role);
$klass = get_called_class();
$rc = new ReflectionClass($role);
$methods = $rc->getMethods();
foreach ($methods as $interfaceMethod) {
// check public methods only
if (!$interfaceMethod->isPublic()) {
continue;
}
$equals = function ($m1, $m2) {
return $m1->isStatic() === $m2->isStatic()
&& $m1->getNumberOfParameters() === $m2->getNumberOfParameters();
};
try {
$myMethod = new ReflectionMethod($klass, $interfaceMethod->name);
if (!$equals($interfaceMethod, $myMethod)) {
return false;
}
} catch (ReflectionException $re) {
return false;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment