-
-
Save tleilax/fe23e52fb2582ea6dc12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
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