Skip to content

Commit

Permalink
optimizes (#652)
Browse files Browse the repository at this point in the history
* variable name shortening.

* Add callable.

* class name

* 🐛 开放平台 ServerGuard 覆盖基类 ServerGuard

* extensible
  • Loading branch information
mingyoung authored and overtrue committed Apr 16, 2017
1 parent e52472d commit 19170b9
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 131 deletions.
10 changes: 5 additions & 5 deletions src/Foundation/ServiceProviders/OpenPlatformServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
use EasyWeChat\OpenPlatform\AccessToken;
use EasyWeChat\OpenPlatform\Api\BaseApi;
use EasyWeChat\OpenPlatform\Api\PreAuthorization;
use EasyWeChat\OpenPlatform\Authorization;
use EasyWeChat\OpenPlatform\Authorizer;
use EasyWeChat\OpenPlatform\AuthorizerAccessToken;
use EasyWeChat\OpenPlatform\EventHandlers;
use EasyWeChat\OpenPlatform\Guard;
Expand Down Expand Up @@ -112,8 +112,8 @@ public function register(Container $pimple)
);
};

$pimple['open_platform.authorization'] = function ($pimple) {
return new Authorization(
$pimple['open_platform.authorizer'] = function ($pimple) {
return new Authorizer(
$pimple['open_platform.api'],
$pimple['config']['open_platform']['app_id'],
$pimple['cache']
Expand All @@ -123,7 +123,7 @@ public function register(Container $pimple)
$pimple['open_platform.authorizer_access_token'] = function ($pimple) {
return new AuthorizerAccessToken(
$pimple['config']['open_platform']['app_id'],
$pimple['open_platform.authorization']
$pimple['open_platform.authorizer']
);
};

Expand Down Expand Up @@ -151,7 +151,7 @@ public function register(Container $pimple)
$scopes = $pimple['config']->get('open_platform.oauth.scopes', []);
$socialite = (new Socialite([
'wechat_open' => [
'client_id' => $pimple['open_platform.authorization']->getAuthorizerAppId(),
'client_id' => $pimple['open_platform.authorizer']->getAppId(),
'client_secret' => [
$pimple['open_platform.access_token']->getAppId(),
$pimple['open_platform.access_token']->getToken(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/

/**
* Authorization.php.
* Authorizer.php.
*
* Part of Overtrue\WeChat.
*
Expand All @@ -31,7 +31,7 @@
use EasyWeChat\Core\Exception;
use EasyWeChat\OpenPlatform\Api\BaseApi;

class Authorization
class Authorizer
{
const CACHE_KEY_ACCESS_TOKEN = 'easywechat.open_platform.authorizer_access_token';
const CACHE_KEY_REFRESH_TOKEN = 'easywechat.open_platform.authorizer_refresh_token';
Expand All @@ -51,30 +51,30 @@ class Authorization
protected $api;

/**
* Open Platform App Id, aka, Component App Id.
* Authorizer AppId.
*
* @var string
*/
protected $appId;

/**
* Authorizer App Id.
* OpenPlatform AppId.
*
* @var string
*/
protected $authorizerAppId;
protected $openPlatformAppId;

/**
* Authorization Constructor.
* Authorizer Constructor.
*
* @param \EasyWeChat\OpenPlatform\Api\BaseApi $api
* @param string $appId
* @param string $openPlatformAppId OpenPlatform AppId
* @param \Doctrine\Common\Cache\Cache $cache
*/
public function __construct(BaseApi $api, $appId, Cache $cache)
public function __construct(BaseApi $api, $openPlatformAppId, Cache $cache)
{
$this->api = $api;
$this->appId = $appId;
$this->openPlatformAppId = $openPlatformAppId;
$this->cache = $cache;
}

Expand All @@ -91,13 +91,13 @@ public function getApi()
/**
* Sets the authorizer app id.
*
* @param string $authorizerAppId
* @param string $appId
*
* @return $this
*/
public function setAuthorizerAppId($authorizerAppId)
public function setAppId($appId)
{
$this->authorizerAppId = $authorizerAppId;
$this->appId = $appId;

return $this;
}
Expand All @@ -109,49 +109,54 @@ public function setAuthorizerAppId($authorizerAppId)
*
* @throws \EasyWeChat\Core\Exception
*/
public function getAuthorizerAppId()
public function getAppId()
{
if (!$this->authorizerAppId) {
if (!$this->appId) {
throw new Exception(
'Authorizer App Id is not present, you may not make the authorization yet.'
'Authorizer App Id is not present, you may not make the authorizer yet.'
);
}

return $this->authorizerAppId;
return $this->appId;
}

/**
* Saves the authorizer access token in cache.
*
* @param string $token
* @param int $expires
*
* @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise
* @return $this
*/
public function setAuthorizerAccessToken($token, $expires = 7200)
public function setAccessToken($token, $expires = 7200)
{
return $this->cache->save($this->getAuthorizerAccessTokenKey(), $token, $expires);
$this->cache->save($this->getAccessTokenCacheKey(), $token, $expires);

return $this;
}

/**
* Gets the authorizer access token.
*
* @return string
*/
public function getAuthorizerAccessToken()
public function getAccessToken()
{
return $this->cache->fetch($this->getAuthorizerAccessTokenKey());
return $this->cache->fetch($this->getAccessTokenCacheKey());
}

/**
* Saves the authorizer refresh token in cache.
*
* @param string $refreshToken
*
* @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise
* @return $this
*/
public function setAuthorizerRefreshToken($refreshToken)
public function setRefreshToken($refreshToken)
{
return $this->cache->save($this->getAuthorizerRefreshTokenKey(), $refreshToken);
$this->cache->save($this->getRefreshTokenCacheKey(), $refreshToken);

return $this;
}

/**
Expand All @@ -161,56 +166,34 @@ public function setAuthorizerRefreshToken($refreshToken)
*
* @throws \EasyWeChat\Core\Exception when refresh token is not present
*/
public function getAuthorizerRefreshToken()
public function getRefreshToken()
{
if ($token = $this->cache->fetch($this->getAuthorizerRefreshTokenKey())) {
if ($token = $this->cache->fetch($this->getRefreshTokenCacheKey())) {
return $token;
}

throw new Exception(
'Authorizer Refresh Token is not present, you may not make the authorization yet.'
'Authorizer Refresh Token is not present, you may not make the authorizer yet.'
);
}

/**
* Removes the authorizer access token from cache.
*
* @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise.
* Deleting a non-existing entry is considered successful
*/
public function removeAuthorizerAccessToken()
{
return $this->cache->delete($this->getAuthorizerAccessTokenKey());
}

/**
* Removes the authorizer refresh token from cache.
*
* @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise.
* Deleting a non-existing entry is considered successful
*/
public function removeAuthorizerRefreshToken()
{
return $this->cache->delete($this->getAuthorizerRefreshTokenKey());
}

/**
* Gets the authorizer access token cache key.
*
* @return string
*/
public function getAuthorizerAccessTokenKey()
public function getAccessTokenCacheKey()
{
return self::CACHE_KEY_ACCESS_TOKEN.$this->appId.$this->getAuthorizerAppId();
return self::CACHE_KEY_ACCESS_TOKEN.$this->appId.$this->getAppId();
}

/**
* Gets the authorizer refresh token cache key.
*
* @return string
*/
public function getAuthorizerRefreshTokenKey()
public function getRefreshTokenCacheKey()
{
return self::CACHE_KEY_REFRESH_TOKEN.$this->appId.$this->getAuthorizerAppId();
return self::CACHE_KEY_REFRESH_TOKEN.$this->appId.$this->getAppId();
}
}
27 changes: 13 additions & 14 deletions src/OpenPlatform/AuthorizerAccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,28 @@
*
* AuthorizerAccessToken is responsible for the access token of the authorizer,
* the complexity is that this access token also requires the refresh token
* of the authorizer which is acquired by the open platform authorization
* process.
* of the authorizer which is acquired by the open platform authorizer process.
*
* This completely overrides the original AccessToken.
*/
class AuthorizerAccessToken extends BaseAccessToken
{
/**
* @var \EasyWeChat\OpenPlatform\Authorization
* @var \EasyWeChat\OpenPlatform\Authorizer
*/
protected $authorization;
protected $authorizer;

/**
* AuthorizerAccessToken constructor.
*
* @param string $appId
* @param \EasyWeChat\OpenPlatform\Authorization $authorization
* @param string $appId
* @param \EasyWeChat\OpenPlatform\Authorizer $authorizer
*/
public function __construct($appId, Authorization $authorization)
public function __construct($appId, Authorizer $authorizer)
{
parent::__construct($appId, null);

$this->authorization = $authorization;
$this->authorizer = $authorizer;
}

/**
Expand All @@ -70,7 +69,7 @@ public function __construct($appId, Authorization $authorization)
*/
public function getToken($forceRefresh = false)
{
$cached = $this->authorization->getAuthorizerAccessToken();
$cached = $this->authorizer->getAccessToken();

if ($forceRefresh || empty($cached)) {
return $this->refreshToken();
Expand All @@ -86,13 +85,13 @@ public function getToken($forceRefresh = false)
*/
protected function refreshToken()
{
$token = $this->authorization->getApi()
$token = $this->authorizer->getApi()
->getAuthorizerToken(
$this->authorization->getAuthorizerAppId(),
$this->authorization->getAuthorizerRefreshToken()
$this->authorizer->getAppId(),
$this->authorizer->getRefreshToken()
);

$this->authorization->setAuthorizerAccessToken($token['authorizer_access_token'], $token['expires_in'] - 1500);
$this->authorizer->setAccessToken($token['authorizer_access_token'], $token['expires_in'] - 1500);

return $token['authorizer_access_token'];
}
Expand All @@ -104,6 +103,6 @@ protected function refreshToken()
*/
public function getAppId()
{
return $this->authorization->getAuthorizerAppId();
return $this->authorizer->getAppId();
}
}
4 changes: 2 additions & 2 deletions src/OpenPlatform/Guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ protected function handleEventMessage(array $message)
Log::notice("No existing handler for '{$infoType}'.");
}

if ($customHandler = $this->getMessageHandler()) {
$customHandler($message);
if ($messageHandler = $this->getMessageHandler()) {
call_user_func_array($messageHandler, [$message]);
}
}
}
17 changes: 9 additions & 8 deletions src/OpenPlatform/OpenPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,16 @@ class OpenPlatform
*/
public function createAuthorizerApplication($appId, $refreshToken)
{
$this->fetch('authorization')
->setAuthorizerAppId($appId)
->setAuthorizerRefreshToken($refreshToken);
$this->fetch('authorizer', function ($authorizer) use ($appId, $refreshToken) {
$authorizer->setAppId($appId);
$authorizer->setRefreshToken($refreshToken);
});

$application = $this->fetch('app');
$application['access_token'] = $this->fetch('authorizer_access_token');
$application['oauth'] = $this->fetch('oauth');

return $application;
return $this->fetch('app', function ($app) {
$app['access_token'] = $this->fetch('authorizer_access_token');
$app['oauth'] = $this->fetch('oauth');
$app['server'] = $this->fetch('server');
});
}

/**
Expand Down
13 changes: 10 additions & 3 deletions src/Support/Traits/PrefixedContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,20 @@ public function __construct(Container $container)
/**
* Fetches from pimple container.
*
* @param string $key
* @param string $key
* @param callable|null $callable
*
* @return mixed
*/
public function fetch($key)
public function fetch($key, callable $callable = null)
{
return $this->$key;
$instance = $this->$key;

if (!is_null($callable)) {
$callable($instance);
}

return $instance;
}

/**
Expand Down
Loading

2 comments on commit 19170b9

@reatang
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

大量优化命名啊

@mingyoung
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@reatang 是啊

Please sign in to comment.