返回介绍

Extending Extensions

发布于 2024-06-22 20:04:58 字数 5507 浏览 0 评论 0 收藏 0

Flarum extensions aren't just for adding features to core: extensions can extend other extensions!

提示

To learn how to make your extension extensible, see the relevant documentation

Dependencies

If your extension relies on another extension, you'll want to ensure that:

  • The other extension is installed and enabled before yours can be.
  • The other extension can't be disabled while yours is enabled.
  • The other extension is booted before your extension.

Flarum makes this very easy: just add the other extension to your extension's composer.json's require section.

For example, if you were building a new theme for the Flarum Tags extension, your composer.json would look like this:

{
  // ...
  "require": {
    "flarum/core": "^0.1.0-beta.15",  // Since all extensions need to require core.
    "flarum/tags": "^0.1.0-beta.15"  // This tells Flarum to treat tags as a dependency of your extension.
  },
  // ...
}

Optional Dependencies

Sometimes, extension A might want to extend extension B only if extension B is enabled. In this case, we call B an "Optional Dependency" of A. For instance, a drafts extension might want to add support for saving private discussion drafts, but only if the private discussion extension is enabled.

The first step here is detecting whether extension B is enabled. In the frontend, this is easy: if extension B does anything in the frontend, its extension ID will appear as a key in the flarum.extensions global object. For instance:

if ('some-extension-id' in flarum.extensions) {
    // do something
}

In the backend, you'll need to inject an instance of Flarum\Extension\ExtensionManager, and use its isEnabled() method. For instance:

<?php

use Flarum\Extension\ExtensionManager;

class SomeClass {
    public function __construct(ExtensionManager $extensions)
    {
        $this->extensions = $extensions;
    }

    public function someMethod()
    {
        if ($this->extensions->isEnabled('some-extension-id')) {
            // do something.
        }
    }
}

Generally, if your extension has optional dependencies, you'll want it to be booted after said optional dependencies. You can also do this by specifying composer package names (NOT flarum extension IDs) in an array for the extra.flarum-extension.optional-dependencies key of your composer.json.

For instance:

{
  // ...
  "extra": {
    "flarum-extension": {
      "optional-dependencies": [
        "flarum/tags"
      ]
    }
  },
  // ...
}

Importing from Extensions

In the backend, you can import the classes you need via regular PHP use statements:

<?php

use Flarum\Tags\Tag;

class SomeClass
{
    public function someMethod()
    {
        return new Tag();  // This is not the correct way to instantiate models, it's just here for example of importing.
    }
}

Note that if you're importing from an optional dependency which might not be installed, you'll need to check that the class in question exists via the class_exists function.

In the frontend, you can only import things that have been explicitly exported. However, first you'll need to configure your extension's webpack to allow these imports:

webpack.config.js

module.exports = require('flarum-webpack-config')({
    // Provide the extension IDs of all extensions from which your extension will be importing.
    // Do this for both full and optional dependencies.
    useExtensions: ['flarum-tags']
});

Once this is done, you can import with:

const allThingsExportedBySomeExtension = require('@flarum-tags');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文