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

开放平台大幅重构并且添加测试 #606

Merged
merged 10 commits into from
Mar 8, 2017
Merged
85 changes: 69 additions & 16 deletions src/Foundation/ServiceProviders/OpenPlatformServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,36 @@
/**
* OpenPlatformServiceProvider.php.
*
* This file is part of the wechat.
* Part of Overtrue\WeChat.
*
* (c) mingyoung <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
* @author mingyoung <[email protected]>
* @author lixiao <[email protected]>
* @copyright 2016
*
* @see https://github.com/overtrue
* @see http://overtrue.me
*/

namespace EasyWeChat\Foundation\ServiceProviders;

use EasyWeChat\Encryption\Encryptor;
use EasyWeChat\OpenPlatform\Authorization;
use EasyWeChat\OpenPlatform\AuthorizerToken;
use EasyWeChat\OpenPlatform\AccessToken;
use EasyWeChat\OpenPlatform\Components\Authorizer;
use EasyWeChat\OpenPlatform\EventHandlers\Authorized;
use EasyWeChat\OpenPlatform\EventHandlers\UpdateAuthorized;
use EasyWeChat\OpenPlatform\EventHandlers\Unauthorized;
use EasyWeChat\OpenPlatform\EventHandlers\ComponentVerifyTicket;
use EasyWeChat\OpenPlatform\Guard;
use EasyWeChat\OpenPlatform\OpenPlatform;
use EasyWeChat\OpenPlatform\VerifyTicket;
use Pimple\Container;
use Pimple\ServiceProviderInterface;

/**
* Class OpenPlatformServiceProvider.
*/
class OpenPlatformServiceProvider implements ServiceProviderInterface
{
/**
Expand All @@ -45,23 +54,23 @@ class OpenPlatformServiceProvider implements ServiceProviderInterface
*/
public function register(Container $pimple)
{
$pimple['component_verify_ticket'] = function ($pimple) {
$pimple['open_platform.verify_ticket'] = function ($pimple) {
return new VerifyTicket(
$pimple['config']['open_platform'],
$pimple['config']['open_platform']['app_id'],
$pimple['cache']
);
};

$pimple['open_platform_access_token'] = function ($pimple) {
$pimple['open_platform.access_token'] = function ($pimple) {
return new AccessToken(
$pimple['config']['open_platform']['app_id'],
$pimple['config']['open_platform']['secret'],
$pimple['component_verify_ticket'],
$pimple['open_platform.verify_ticket'],
$pimple['cache']
);
};

$pimple['open_platform_encryptor'] = function ($pimple) {
$pimple['open_platform.encryptor'] = function ($pimple) {
return new Encryptor(
$pimple['config']['open_platform']['app_id'],
$pimple['config']['open_platform']['token'],
Expand All @@ -70,17 +79,61 @@ public function register(Container $pimple)
};

$pimple['open_platform'] = function ($pimple) {
$server = new Guard($pimple['config']['open_platform']['token'], $pimple['component_verify_ticket']);
$server = new Guard(
$pimple['config']['open_platform']['token']
);

$server->debug($pimple['config']['debug']);

$server->setEncryptor($pimple['open_platform_encryptor']);
$server->setEncryptor($pimple['open_platform.encryptor']);
$server->setContainer($pimple);

return new OpenPlatform(
$platform = new OpenPlatform(
$server,
$pimple['open_platform_access_token'],
$pimple['open_platform.access_token'],
$pimple['config']['open_platform']
);

$platform->setContainer($pimple);

return $platform;
};

$pimple['open_platform.authorizer'] = function ($pimple) {
return new Authorizer(
$pimple['open_platform.access_token'],
$pimple['config']['open_platform']
);
};

$pimple['open_platform.authorization'] = function($pimple) {
return new Authorization(
$pimple['open_platform.authorizer'],
$pimple['config']['open_platform']['app_id'],
$pimple['cache']
);
};

$pimple['open_platform.authorizer_token'] = function ($pimple) {
return new AuthorizerToken(
$pimple['config']['open_platform']['app_id'],
$pimple['open_platform.authorization']
);
};

// Authorization events handlers.
$pimple['open_platform.handlers.component_verify_ticket'] = function($pimple) {
return new ComponentVerifyTicket($pimple['open_platform.verify_ticket']);
};
$pimple['open_platform.handlers.authorized'] = function($pimple) {
return new Authorized($pimple['open_platform.authorization']);
};
$pimple['open_platform.handlers.updateauthorized'] = function($pimple) {
return new UpdateAuthorized($pimple['open_platform.authorization']);
};
$pimple['open_platform.handlers.unauthorized'] = function($pimple) {
return new Unauthorized($pimple['open_platform.authorization']);
};
}

}
2 changes: 1 addition & 1 deletion src/OpenPlatform/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class AccessToken extends WechatAccessToken
/**
* {@inheritdoc}.
*/
protected $prefix = 'easywechat.common.component_access_token.';
protected $prefix = 'easywechat.open_platform.component_access_token.';

/**
* AccessToken constructor.
Expand Down
Loading