Make a plugin script

I am working on a script and I need to make it plagible. Now the syntax I came up with and which should work for me is to use its classes. For example, to create a new plugin that will be launched when it reaches a certain point (hook), you will declare a new class. I'm not sure how the hook would be specified in this syntax, so I'm looking for suggestions.

Syntax Example:

<?php
class ScriptPlugin
{
    function runPlugin() {} // would be run when the time has to come to execute this plugin
}
?>

Also, if this syntax wouldn't work, it would be great if you guys could give me a good example of the syntax.

+5
source share
3 answers

The Observational Pattern comes to mind. Plugins will be registered and will be notified when the hook is called.

, - PHP. , . .

Observer , , . , , , Hooks, / , . .

:

  • .
  • Hooks.
  • , Hooks.
  • ( ) , Registerable.

:

<?php
Namespace Addon;

class Hooks
{
    private $hooks = array();
    private $arguments;
    private $name;
    private $return;
    public function __call($name, array $arguments)
    {
        $name = (string) $name;
        $this->name = $name;
        $this->arguments = $arguments;
        $this->return  = NULL;
        foreach($this->getHooks($name) as $hook)
        {
            $this->return = call_user_func($hook, $this);
        }
        return $this->return;
    }
    public function getHooks($name)
    {
        return isset($this->hooks[$name]) ? $this->hooks[$name] : array();
    }
    public function getArguments()
    {
        return $this->arguments;
    }
    public function getName()
    {
        return $this->name;
    }
    public function getReturn()
    {
        return $this->return;
    }
    public function setReturn($return)
    {
        $this->return = $return;
    }
    public function attach($name, $callback)
    {
        $this->hooks[(string) $name][] = $callback;
    }
    public function register(Registerable $plugin)
    {
        $plugin->register($this);
    }
}

interface Registerable
{
    public function register(Hooks $hooks);
}

class MyPlugin implements Registerable
{
    public function register(Hooks $hooks)
    {
        $hooks->attach('postPublished', array($this, 'postPublished'));
        $hooks->attach('postDisplayFilter', array($this, 'filterToUpper'));
    }
    public function postPublished()
    {
        echo "MyPlugin: postPublished.\n";
    }
    public function filterToUpper(Hooks $context)
    {
        list($post) = $context->getArguments();
        return strtoupper($post);
    }
}

$hooks = new Hooks();

$plugin = new MyPlugin();
$hooks->register($plugin);  

$hooks->postPublished();

echo $hooks->postDisplayFilter("Some post text\n");

, , , . , , , - . :

$hooks->attach('hookName', function() {echo "Hook was called\n";});

, , , register , docblock

class MyNewPlugin extends PluginSuper
{
    /**
     * @hook postPublished
     */
    public function justAnotherFunction() {}

    public hookPostPublished() {}
}

Reflection, . .

+3

, .

abstract class Plugin {
    abstract function yourHook();
}

.

class SomePlugin extends Plugin {
    function yourHook() {
        echo 'yourHook() Called!';
    }
}

, , - , $plugins. : fooobar.com/questions/34246/...

foreach (glob("classes/*.php") as $filename)
{
    include $filename;
}

( )

, , registerPlugin():

function registerPlugin($classname) {
    $plugins[] = new $classname();
}

( ):

registerPlugin('SomePlugin');

, $plugins . - :

foreach ($plugins as $plugin) {
    $plugin->yourHook();
}

, interfaces . , .

0

Let's say the plugin looks like:

class NewsPlugin extends Plugin
{
  function onCreate($title)
  {
    # Do some stuff
  }
}

Then, when you create the news, you can simply call onCreate in all registered plugins.

0
source

All Articles