Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚧 Auto discover extensions. #1027

Merged
merged 5 commits into from
Dec 8, 2017
Merged

🚧 Auto discover extensions. #1027

merged 5 commits into from
Dec 8, 2017

Conversation

mingyoung
Copy link
Collaborator

@mingyoung mingyoung commented Nov 14, 2017

满足以下条件的 composer 包即可自动注入 observer,EasyWeChat 无需另外配置

相关包: easywechat-composer/easywechat-composer
示例包:open-platform-testcase


composer.json 内容:

type 需指定为 easywechat-extension

{
    "name": "foo/bar",
    "type": "easywechat-extension",
    "autoload": {
        "psr-4": {
            "Acme\\": ""
        }
    },
    "extra": {
        "observers": [
            "Acme\\ExampleHandler",
            "Acme\\OtherHandler"
        ]
    }
}

一个完整的 ExampleHandler 如下:

<?php

namespace Acme;

use EasyWeChat\Kernel\Contracts\EventHandlerInterface;
use EasyWeChat\OfficialAccount\Application as OfficialAccount;
use EasyWeChat\MiniProgram\Application as MiniProgram;
use EasyWeChat\Kernel\Messages\Message;

class ExampleHandler implements EventHandlerInterface
{
    public function handle(array $payload = [])
    {
        return 'hello world!';
    }

    // 指定注入的应用(可选)
    public static function getAccessor()
    {
        return OfficialAccount::class;
        // 或多个应用
        // return [OfficialAccount::class, MiniProgram::class,];
    }

    // 注入条件(可选)
    public static function onCondition()
    {
        return Message::IMAGE;
        // or 
        // return Message::IMAGE | Message::LOCATION;
    }
}

禁用某个 observer

EasyWeChat::officialAccount(
    // 配置加入以下内容:
    'disable_observers' => [
        '*', // 传入 * 则禁用所有
        'Acme\\ExampleHandler',
    ],
);

@mingyoung mingyoung requested a review from overtrue November 15, 2017 06:47
@overtrue overtrue merged commit b341d2b into master Dec 8, 2017
@overtrue overtrue deleted the composer-plugin branch December 8, 2017 06:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants