1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
<?php
namespace Rain\Tpl;
require_once __DIR__ . '/IPlugin.php';
/**
* Basic plugin implementation.
* - It allows to define hooks as property.
* - Options can be passed in constructor.
* When a setter set_{optionname}() exists it is used to store the option value.
* Otherwise \InvalidArgumentException is thrown.
*/
class Plugin implements IPlugin
{
/**
* This should be an array containing:
* - a key/value pair where key is hook name and value is implementing method,
* - a value only when hook has same name as method.
*
* @var array
*/
protected $hooks = array();
public function __construct($options = array())
{
$this->setOptions($options);
}
/**
* Returns a list of hooks that are implemented by the plugin.
* This should be an array containing:
* - a key/value pair where key is hook name and value is implementing method,
* - a value only when hook has same name as method.
*/
public function declareHooks() {
return $this->hooks;
}
/**
* Sets plugin options.
*
* @var array
*/
public function setOptions($options) {
foreach ((array) $options as $key => $val) {
$this->setOption($key, $val);
}
return $this;
}
/**
* Sets plugin option.
*
* @param string $name
* @param mixed $value
* @throws \InvalidArgumentException Wrong option name or value
* @return Plugin
*/
public function setOption($name, $value) {
$method = 'set' . ucfirst($name);
if (!\method_exists($this, $method)) {
throw new \InvalidArgumentException('Key "' . $name . '" is not a valid settings option' );
}
$this->{$method}($value);
return $this;
}
}
|