Created
December 4, 2019 12:40
-
-
Save kobus1998/5e4eeda399e822904d2aa9cc848261cb to your computer and use it in GitHub Desktop.
service provider
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
<?php | |
interface PluginServiceProvider | |
{ | |
public function register(Plugin $plugin): void; | |
} | |
class Plugin | |
{ | |
protected $methods = []; | |
public function addPlugin(PluginServiceProvider $provider) | |
{ | |
$provider->register($this); | |
return $this; | |
} | |
public function addMethod($name, $call) | |
{ | |
$this->methods[$name] = $call; | |
} | |
public function __call($method, $params = []) | |
{ | |
if (!isset($this->methods[$method])) { | |
throw new \Exception('method doesnt exist'); | |
} | |
return call_user_func($this->methods[$method], ...$params); | |
} | |
} | |
class SomeServiceProvider implements PluginServiceProvider | |
{ | |
public function register(Plugin $plugin): void | |
{ | |
$plugin->addMethod('do', [$this, 'doAAA']); | |
} | |
public function doAAA() { | |
echo "AAAA"; | |
} | |
} | |
$plugin = new Plugin(); | |
$plugin->addPlugin(new SomeServiceProvider()); | |
$plugin->do(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment