Skip to content

Commit

Permalink
大量代码修型及可能的bug修正: (#242)
Browse files Browse the repository at this point in the history
1. 遵循`RFC6749`规范,引入命名空间常量`RFC6749_ABNF_*`定义,从代码层面即可直观感受各`Providers`对规范的遵循度;
2. 在抽象`User`类,引入命名空间常量`ABNF_*`定义,直观感受对各厂商的授权信息抽象;
3. 在工厂抽象类中,引入命名空间常量`ABNF_*`定义,直观感受本LIB对`RFC6749`及厂商实现的抽象;
4. 调优`Psr\Http\Message\ResponseInterface::getBody()`获取返回值可能引发的流偏移而拿不到返回文本的潜在问题;
5. 使用相对路径的命名空间导入功能,对代码做了一点点修型;
6. 扔掉了下游包抛送的异常注释,代码注释仅专注于本LIB抛送的异常定义;
7. 支付宝&钉钉的`scope`拼接修型;
8. BC: 大小写`Exceptions\Feishu`调整为`Exceptions\FeiShu`;
9. BC: 部分抛送的异常调整为恰当类型;
  • Loading branch information
TheNorthMemory committed Jul 7, 2022
1 parent c5daae0 commit be554a1
Show file tree
Hide file tree
Showing 36 changed files with 977 additions and 1,065 deletions.
19 changes: 10 additions & 9 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Overtrue\Socialite;

use ArrayAccess;
use JsonSerializable;

class Config implements ArrayAccess, \JsonSerializable
class Config implements ArrayAccess, JsonSerializable
{
protected array $config;

Expand All @@ -24,8 +25,8 @@ public function get(string $key, mixed $default = null): mixed
return $config[$key];
}

foreach (explode('.', $key) as $segment) {
if (!is_array($config) || !array_key_exists($segment, $config)) {
foreach (\explode('.', $key) as $segment) {
if (!\is_array($config) || !\array_key_exists($segment, $config)) {
return $default;
}
$config = $config[$segment];
Expand All @@ -36,18 +37,18 @@ public function get(string $key, mixed $default = null): mixed

public function set(string $key, $value): mixed
{
$keys = explode('.', $key);
$keys = \explode('.', $key);
$config = &$this->config;

while (count($keys) > 1) {
$key = array_shift($keys);
if (!isset($config[$key]) || !is_array($config[$key])) {
while (\count($keys) > 1) {
$key = \array_shift($keys);
if (!isset($config[$key]) || !\is_array($config[$key])) {
$config[$key] = [];
}
$config = &$config[$key];
}

$config[array_shift($keys)] = $value;
$config[\array_shift($keys)] = $value;

return $config;
}
Expand All @@ -59,7 +60,7 @@ public function has(string $key): bool

public function offsetExists(mixed $offset): bool
{
return array_key_exists($offset, $this->config);
return \array_key_exists($offset, $this->config);
}

public function offsetGet(mixed $offset): mixed
Expand Down
10 changes: 5 additions & 5 deletions src/Contracts/FactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Overtrue\Socialite\Contracts;

const ABNF_APP_ID = 'app_id';
const ABNF_APP_SECRET = 'app_secret';
const ABNF_OPEN_ID = 'open_id';
const ABNF_TOKEN = 'token';

interface FactoryInterface
{
/**
* @param string $driver
*
* @return \Overtrue\Socialite\Contracts\ProviderInterface
*/
public function create(string $driver): ProviderInterface;
}
46 changes: 44 additions & 2 deletions src/Contracts/ProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,45 @@

namespace Overtrue\Socialite\Contracts;

/** @see https://datatracker.ietf.org/doc/html/rfc6749#appendix-A.1 */
const RFC6749_ABNF_CLIENT_ID = 'client_id';
/** @see https://datatracker.ietf.org/doc/html/rfc6749#appendix-A.2 */
const RFC6749_ABNF_CLIENT_SECRET = 'client_secret';
/** @see https://datatracker.ietf.org/doc/html/rfc6749#appendix-A.3 */
const RFC6749_ABNF_RESPONSE_TYPE = 'response_type';
/** @see https://datatracker.ietf.org/doc/html/rfc6749#appendix-A.4 */
const RFC6749_ABNF_SCOPE = 'scope';
/** @see https://datatracker.ietf.org/doc/html/rfc6749#appendix-A.5 */
const RFC6749_ABNF_STATE = 'state';
/** @see https://datatracker.ietf.org/doc/html/rfc6749#appendix-A.6 */
const RFC6749_ABNF_REDIRECT_URI = 'redirect_uri';
/** @see https://datatracker.ietf.org/doc/html/rfc6749#appendix-A.7 */
const RFC6749_ABNF_ERROR = 'error';
/** @see https://datatracker.ietf.org/doc/html/rfc6749#appendix-A.8 */
const RFC6749_ABNF_ERROR_DESCRIPTION = 'error_description';
/** @see https://datatracker.ietf.org/doc/html/rfc6749#appendix-A.9 */
const RFC6749_ABNF_ERROR_URI = 'error_uri';
/** @see https://datatracker.ietf.org/doc/html/rfc6749#appendix-A.10 */
const RFC6749_ABNF_GRANT_TYPE = 'grant_type';
/** @see https://datatracker.ietf.org/doc/html/rfc6749#appendix-A.11 */
const RFC6749_ABNF_CODE = 'code';
/** @see https://datatracker.ietf.org/doc/html/rfc6749#appendix-A.12 */
const RFC6749_ABNF_ACCESS_TOKEN = 'access_token';
/** @see https://datatracker.ietf.org/doc/html/rfc6749#appendix-A.13 */
const RFC6749_ABNF_TOKEN_TYPE = 'token_type';
/** @see https://datatracker.ietf.org/doc/html/rfc6749#appendix-A.14 */
const RFC6749_ABNF_EXPIRES_IN = 'expires_in';
/** @see https://datatracker.ietf.org/doc/html/rfc6749#appendix-A.15 */
const RFC6749_ABNF_USERNAME = 'username';
/** @see https://datatracker.ietf.org/doc/html/rfc6749#appendix-A.16 */
const RFC6749_ABNF_PASSWORD = 'password';
/** @see https://datatracker.ietf.org/doc/html/rfc6749#appendix-A.17 */
const RFC6749_ABNF_REFRESH_TOKEN = 'refresh_token';
/** @see https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.3 */
const RFC6749_ABNF_AUTHORATION_CODE = 'authorization_code';
/** @see https://datatracker.ietf.org/doc/html/rfc6749#section-4.4.2 */
const RFC6749_ABNF_CLIENT_CREDENTIALS = 'client_credentials';

interface ProviderInterface
{
public function redirect(?string $redirectUrl = null): string;
Expand All @@ -10,7 +49,10 @@ public function userFromCode(string $code): UserInterface;

public function userFromToken(string $token): UserInterface;

public function withState(string $state): ProviderInterface;
public function withState(string $state): self;

public function scopes(array $scopes): ProviderInterface;
/**
* @param string[] $scopes
*/
public function scopes(array $scopes): self;
}
20 changes: 20 additions & 0 deletions src/Contracts/UserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

namespace Overtrue\Socialite\Contracts;

const ABNF_ID = 'id';
const ABNF_NAME = 'name';
const ABNF_NICKNAME = 'nickname';
const ABNF_EMAIL = 'email';
const ABNF_AVATAR = 'avatar';

interface UserInterface
{
public function getId(): mixed;
Expand All @@ -19,4 +25,18 @@ public function getAccessToken(): ?string;
public function getRefreshToken(): ?string;

public function getExpiresIn(): ?int;

public function getProvider(): ProviderInterface;

public function setRefreshToken(?string $refreshToken): self;

public function setExpiresIn(int $expiresIn): self;

public function setTokenResponse(array $response): self;

public function setProvider(ProviderInterface $provider): self;

public function setRaw(array $user): self;

public function setAccessToken(string $token): self;
}
9 changes: 9 additions & 0 deletions src/Exceptions/FeiShu/InvalidTicketException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Overtrue\Socialite\Exceptions\FeiShu;

use Overtrue\Socialite\Exceptions;

class InvalidTicketException extends Exceptions\Exception
{
}
9 changes: 0 additions & 9 deletions src/Exceptions/Feishu/InvalidTicketException.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Exceptions/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Overtrue\Socialite\Exceptions;

class InvalidArgumentException extends Exception
class InvalidArgumentException extends \InvalidArgumentException
{
//
}

0 comments on commit be554a1

Please sign in to comment.