diff options
| author | wn_ <invalid@email.com> | 2024-12-15 16:09:40 +0000 |
|---|---|---|
| committer | wn_ <invalid@email.com> | 2024-12-15 16:09:42 +0000 |
| commit | cfbbb9d714d7b5d59d3fb3e326b80f6cc88fbe23 (patch) | |
| tree | e73043c079304f0a318e484ff3c28c8cba51cf12 /classes | |
| parent | 62a6191f04e37cc9b40fcf3e713563ed51722e95 (diff) | |
Clean up some virtual feed stuff in PluginHost.
Among other things, this makes 'PluginHost->add_feed()' return false if the feed was not added.
Diffstat (limited to 'classes')
| -rw-r--r-- | classes/PluginHost.php | 57 |
1 files changed, 32 insertions, 25 deletions
diff --git a/classes/PluginHost.php b/classes/PluginHost.php index 97d0c45c7..bccf2ecc8 100644 --- a/classes/PluginHost.php +++ b/classes/PluginHost.php @@ -23,8 +23,8 @@ class PluginHost { /** @var array<string, array<string, mixed>> plugin name -> (potential profile array) -> key -> value */ private array $storage = []; - /** @var array<int, array<int, array{'id': int, 'title': string, 'sender': Plugin, 'icon': string}>> */ - private array $feeds = []; + /** @var array<int, array{'id': int, 'title': string, 'sender': Plugin, 'icon': string}> */ + private array $special_feeds = []; /** @var array<string, Plugin> API method name, Plugin sender */ private array $api_methods = []; @@ -759,44 +759,51 @@ class PluginHost { } } - // Plugin feed functions are *EXPERIMENTAL*! - - // cat_id: only -1 (Feeds::CATEGORY_SPECIAL) is supported (Special) - function add_feed(int $cat_id, string $title, string $icon, Plugin $sender): int { - - if (empty($this->feeds[$cat_id])) - $this->feeds[$cat_id] = []; + /** + * Add a special (plugin-provided) feed + * + * @param int $cat_id only -1 (Feeds::CATEGORY_SPECIAL) is supported + * @return false|positive-int false if $cat_id was not -1 (Feeds::CATEGORY_SPECIAL), + * otherwise a positive integer ID that might change between executions + */ + function add_feed(int $cat_id, string $title, string $icon, Plugin $sender): false|int { + if ($cat_id !== Feeds::CATEGORY_SPECIAL) + return false; - $id = count($this->feeds[$cat_id]); + $id = count($this->special_feeds); - array_push($this->feeds[$cat_id], - ['id' => $id, 'title' => $title, 'sender' => $sender, 'icon' => $icon]); + $this->special_feeds[] = [ + 'id' => $id, + 'title' => $title, + 'sender' => $sender, + 'icon' => $icon, + ]; return $id; } /** + * Get special (plugin-provided) feeds + * + * @param int $cat_id only -1 (Feeds::CATEGORY_SPECIAL) is supported * @return array<int, array{'id': int, 'title': string, 'sender': Plugin, 'icon': string}> */ function get_feeds(int $cat_id) { - return $this->feeds[$cat_id] ?? []; + return $cat_id === Feeds::CATEGORY_SPECIAL ? $this->special_feeds : []; } /** - * convert feed_id (e.g. -129) to pfeed_id first - * @return (Plugin&IVirtualFeed)|null + * Get the Plugin handling a specific virtual feed. + * + * Convert feed_id (e.g. -129) to pfeed_id first. + * + * @return (Plugin&IVirtualFeed)|null a Plugin that implements IVirtualFeed, otherwise null */ function get_feed_handler(int $pfeed_id): ?Plugin { - foreach ($this->feeds as $cat) { - foreach ($cat as $feed) { - if ($feed['id'] == $pfeed_id) { - if (implements_interface($feed['sender'], 'IVirtualFeed')) { - /** @var Plugin&IVirtualFeed $feed['sender'] */ - return $feed['sender']; - } else { - return null; - } - } + foreach ($this->special_feeds as $feed) { + if ($feed['id'] == $pfeed_id) { + /** @var Plugin&IVirtualFeed $feed['sender'] */ + return implements_interface($feed['sender'], 'IVirtualFeed') ? $feed['sender'] : null; } } return null; |