pluginhost: add some type hints

This commit is contained in:
Andrew Dolgov 2021-02-08 21:38:03 +03:00
parent 7eb860af61
commit d91eae9c7e
1 changed files with 31 additions and 31 deletions

View File

@ -77,7 +77,7 @@ class PluginHost {
const KIND_SYSTEM = 2; const KIND_SYSTEM = 2;
const KIND_USER = 3; const KIND_USER = 3;
static function object_to_domain($plugin) { static function object_to_domain(Plugin $plugin) {
return strtolower(get_class($plugin)); return strtolower(get_class($plugin));
} }
@ -97,7 +97,7 @@ class PluginHost {
return self::$instance; return self::$instance;
} }
private function register_plugin($name, $plugin) { private function register_plugin(string $name, Plugin $plugin) {
//array_push($this->plugins, $plugin); //array_push($this->plugins, $plugin);
$this->plugins[$name] = $plugin; $this->plugins[$name] = $plugin;
} }
@ -129,11 +129,11 @@ class PluginHost {
return $this->plugins; return $this->plugins;
} }
function get_plugin($name) { function get_plugin(string $name) {
return $this->plugins[strtolower($name)] ?? null; return $this->plugins[strtolower($name)] ?? null;
} }
function run_hooks($hook, ...$args) { function run_hooks(string $hook, ...$args) {
$method = strtolower($hook); $method = strtolower($hook);
foreach ($this->get_hooks($hook) as $plugin) { foreach ($this->get_hooks($hook) as $plugin) {
@ -149,7 +149,7 @@ class PluginHost {
} }
} }
function run_hooks_until($hook, $check, ...$args) { function run_hooks_until(string $hook, $check, ...$args) {
$method = strtolower($hook); $method = strtolower($hook);
foreach ($this->get_hooks($hook) as $plugin) { foreach ($this->get_hooks($hook) as $plugin) {
@ -169,7 +169,7 @@ class PluginHost {
return false; return false;
} }
function run_hooks_callback($hook, $callback, ...$args) { function run_hooks_callback(string $hook, Closure $callback, ...$args) {
$method = strtolower($hook); $method = strtolower($hook);
foreach ($this->get_hooks($hook) as $plugin) { foreach ($this->get_hooks($hook) as $plugin) {
@ -186,7 +186,7 @@ class PluginHost {
} }
} }
function chain_hooks_callback($hook, $callback, &...$args) { function chain_hooks_callback(string $hook, Closure $callback, &...$args) {
$method = strtolower($hook); $method = strtolower($hook);
foreach ($this->get_hooks($hook) as $plugin) { foreach ($this->get_hooks($hook) as $plugin) {
@ -203,7 +203,7 @@ class PluginHost {
} }
} }
function add_hook($type, $sender, $priority = 50) { function add_hook(string $type, Plugin $sender, int $priority = 50) {
$priority = (int) $priority; $priority = (int) $priority;
if (!method_exists($sender, strtolower($type))) { if (!method_exists($sender, strtolower($type))) {
@ -227,7 +227,7 @@ class PluginHost {
ksort($this->hooks[$type]); ksort($this->hooks[$type]);
} }
function del_hook($type, $sender) { function del_hook(string $type, Plugin $sender) {
if (is_array($this->hooks[$type])) { if (is_array($this->hooks[$type])) {
foreach (array_keys($this->hooks[$type]) as $prio) { foreach (array_keys($this->hooks[$type]) as $prio) {
$key = array_search($sender, $this->hooks[$type][$prio]); $key = array_search($sender, $this->hooks[$type][$prio]);
@ -239,7 +239,7 @@ class PluginHost {
} }
} }
function get_hooks($type) { function get_hooks(string $type) {
if (isset($this->hooks[$type])) { if (isset($this->hooks[$type])) {
$tmp = []; $tmp = [];
@ -252,7 +252,7 @@ class PluginHost {
return []; return [];
} }
} }
function load_all($kind, $owner_uid = false, $skip_init = false) { function load_all(int $kind, int $owner_uid = null, bool $skip_init = false) {
$plugins = array_merge(glob("plugins/*"), glob("plugins.local/*")); $plugins = array_merge(glob("plugins/*"), glob("plugins.local/*"));
$plugins = array_filter($plugins, "is_dir"); $plugins = array_filter($plugins, "is_dir");
@ -263,7 +263,7 @@ class PluginHost {
$this->load(join(",", $plugins), $kind, $owner_uid, $skip_init); $this->load(join(",", $plugins), $kind, $owner_uid, $skip_init);
} }
function load($classlist, $kind, $owner_uid = false, $skip_init = false) { function load(string $classlist, int $kind, int $owner_uid = null, bool $skip_init = false) {
$plugins = explode(",", $classlist); $plugins = explode(",", $classlist);
$this->owner_uid = (int) $owner_uid; $this->owner_uid = (int) $owner_uid;
@ -360,14 +360,14 @@ class PluginHost {
$this->load_data(); $this->load_data();
} }
function is_system($plugin) { function is_system(Plugin $plugin) {
$about = $plugin->about(); $about = $plugin->about();
return $about[3] ?? false; return $about[3] ?? false;
} }
// only system plugins are allowed to modify routing // only system plugins are allowed to modify routing
function add_handler($handler, $method, $sender) { function add_handler(string $handler, $method, Plugin $sender) {
$handler = str_replace("-", "_", strtolower($handler)); $handler = str_replace("-", "_", strtolower($handler));
$method = strtolower($method); $method = strtolower($method);
@ -380,7 +380,7 @@ class PluginHost {
} }
} }
function del_handler($handler, $method, $sender) { function del_handler(string $handler, $method, Plugin $sender) {
$handler = str_replace("-", "_", strtolower($handler)); $handler = str_replace("-", "_", strtolower($handler));
$method = strtolower($method); $method = strtolower($method);
@ -404,7 +404,7 @@ class PluginHost {
return false; return false;
} }
function add_command($command, $description, $sender, $suffix = "", $arghelp = "") { function add_command(string $command, string $description, Plugin $sender, string $suffix = "", string $arghelp = "") {
$command = str_replace("-", "_", strtolower($command)); $command = str_replace("-", "_", strtolower($command));
$this->commands[$command] = array("description" => $description, $this->commands[$command] = array("description" => $description,
@ -413,7 +413,7 @@ class PluginHost {
"class" => $sender); "class" => $sender);
} }
function del_command($command) { function del_command(string $command) {
$command = "-" . strtolower($command); $command = "-" . strtolower($command);
unset($this->commands[$command]); unset($this->commands[$command]);
@ -433,7 +433,7 @@ class PluginHost {
return $this->commands; return $this->commands;
} }
function run_commands($args) { function run_commands(array $args) {
foreach ($this->get_commands() as $command => $data) { foreach ($this->get_commands() as $command => $data) {
if (isset($args[$command])) { if (isset($args[$command])) {
$command = str_replace("-", "", $command); $command = str_replace("-", "", $command);
@ -456,7 +456,7 @@ class PluginHost {
} }
} }
private function save_data($plugin) { private function save_data(string $plugin) {
if ($this->owner_uid) { if ($this->owner_uid) {
if (!$this->pdo_data) if (!$this->pdo_data)
@ -489,7 +489,7 @@ class PluginHost {
} }
} }
function set($sender, $name, $value, $sync = true) { function set(Plugin $sender, string $name, $value, bool $sync = true) {
$idx = get_class($sender); $idx = get_class($sender);
if (!isset($this->storage[$idx])) if (!isset($this->storage[$idx]))
@ -500,7 +500,7 @@ class PluginHost {
if ($sync) $this->save_data(get_class($sender)); if ($sync) $this->save_data(get_class($sender));
} }
function get($sender, $name, $default_value = false) { function get(Plugin $sender, string $name, $default_value = false) {
$idx = get_class($sender); $idx = get_class($sender);
$this->load_data(); $this->load_data();
@ -518,7 +518,7 @@ class PluginHost {
return $this->storage[$idx] ?? []; return $this->storage[$idx] ?? [];
} }
function clear_data($sender) { function clear_data(Plugin $sender) {
if ($this->owner_uid) { if ($this->owner_uid) {
$idx = get_class($sender); $idx = get_class($sender);
@ -533,7 +533,7 @@ class PluginHost {
// Plugin feed functions are *EXPERIMENTAL*! // Plugin feed functions are *EXPERIMENTAL*!
// cat_id: only -1 is supported (Special) // cat_id: only -1 is supported (Special)
function add_feed($cat_id, $title, $icon, $sender) { function add_feed(int $cat_id, $title, $icon, Plugin $sender) {
if (!$this->feeds[$cat_id]) $this->feeds[$cat_id] = array(); if (!$this->feeds[$cat_id]) $this->feeds[$cat_id] = array();
$id = count($this->feeds[$cat_id]); $id = count($this->feeds[$cat_id]);
@ -544,7 +544,7 @@ class PluginHost {
return $id; return $id;
} }
function get_feeds($cat_id) { function get_feeds(int $cat_id) {
return $this->feeds[$cat_id] ?? []; return $this->feeds[$cat_id] ?? [];
} }
@ -559,25 +559,25 @@ class PluginHost {
} }
} }
static function pfeed_to_feed_id($label) { static function pfeed_to_feed_id($pfeed) {
return PLUGIN_FEED_BASE_INDEX - 1 - abs($label); return PLUGIN_FEED_BASE_INDEX - 1 - abs($pfeed);
} }
static function feed_to_pfeed_id($feed) { static function feed_to_pfeed_id($feed) {
return PLUGIN_FEED_BASE_INDEX - 1 + abs($feed); return PLUGIN_FEED_BASE_INDEX - 1 + abs($feed);
} }
function add_api_method($name, $sender) { function add_api_method(string $name, Plugin $sender) {
if ($this->is_system($sender)) { if ($this->is_system($sender)) {
$this->api_methods[strtolower($name)] = $sender; $this->api_methods[strtolower($name)] = $sender;
} }
} }
function get_api_method($name) { function get_api_method(string $name) {
return $this->api_methods[$name]; return $this->api_methods[$name];
} }
function add_filter_action($sender, $action_name, $action_desc) { function add_filter_action(Plugin $sender, string $action_name, string $action_desc) {
$sender_class = get_class($sender); $sender_class = get_class($sender);
if (!isset($this->plugin_actions[$sender_class])) if (!isset($this->plugin_actions[$sender_class]))
@ -596,7 +596,7 @@ class PluginHost {
} }
// handled by classes/pluginhandler.php, requires valid session // handled by classes/pluginhandler.php, requires valid session
function get_method_url($sender, $method, $params) { function get_method_url(Plugin $sender, string $method, $params) {
return get_self_url_prefix() . "/backend.php?" . return get_self_url_prefix() . "/backend.php?" .
http_build_query( http_build_query(
array_merge( array_merge(
@ -609,7 +609,7 @@ class PluginHost {
} }
// WARNING: endpoint in public.php, exposed to unauthenticated users // WARNING: endpoint in public.php, exposed to unauthenticated users
function get_public_method_url($sender, $method, $params) { function get_public_method_url(Plugin $sender, string $method, $params) {
if ($sender->is_public_method($method)) { if ($sender->is_public_method($method)) {
return get_self_url_prefix() . "/public.php?" . return get_self_url_prefix() . "/public.php?" .
http_build_query( http_build_query(