Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created December 4, 2019 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kobus1998/5e4eeda399e822904d2aa9cc848261cb to your computer and use it in GitHub Desktop.
Save kobus1998/5e4eeda399e822904d2aa9cc848261cb to your computer and use it in GitHub Desktop.
service provider
<?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