From bb26016cd99d678a32f12fec9f4547faeb35c5ba Mon Sep 17 00:00:00 2001 From: Bizley Date: Wed, 5 May 2021 14:35:26 +0200 Subject: [PATCH] Allow Yii-style configs --- infection.json.dist | 2 +- src/Jwt.php | 35 +++++++++++++++++++++++++-------- tests/ConstraintsConfigTest.php | 8 ++++++++ tests/stubs/YiiConstraint.php | 18 +++++++++++++++++ 4 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 tests/stubs/YiiConstraint.php diff --git a/infection.json.dist b/infection.json.dist index 88b5a0b..0bcf1b3 100644 --- a/infection.json.dist +++ b/infection.json.dist @@ -14,7 +14,7 @@ "@default": true, "MethodCallRemoval": { "ignore": [ - "bizley\\jwt\\Jwt::init::184", + "bizley\\jwt\\Jwt::init::187", "bizley\\jwt\\JwtHttpBearerAuth::init::68" ] } diff --git a/src/Jwt.php b/src/Jwt.php index 0f23ab4..a12c482 100644 --- a/src/Jwt.php +++ b/src/Jwt.php @@ -20,10 +20,12 @@ use yii\base\InvalidConfigException; use yii\di\Instance; +use function array_keys; use function count; use function in_array; use function is_array; use function is_string; +use function reset; use function strpos; /** @@ -104,15 +106,16 @@ class Jwt extends Component /** * @var string|Signer|null Signer ID or Signer instance to be used for signing/verifying. - * See $signers for available values. In case it's not set, no algorithm will be used, which may be handy if you want - * to do some testing but it's NOT recommended for production environments. + * See $signers for available values. In case it's not set, no algorithm will be used, which may be handy if you + * want to do some testing but it's NOT recommended for production environments. * @since 3.0.0 */ public $signer; /** - * @var array> Default signers configuration. When instantiated it will use selected array to spread into - * `Yii::createObject($type, array $params = [])` method so the first array element is $type, and the second is $params. + * @var array> Default signers configuration. When instantiated it will use selected array to + * spread into `Yii::createObject($type, array $params = [])` method so the first array element is $type, and + * the second is $params. * Since 3.0.0 configuration is done using arrays. * @since 2.0.0 */ @@ -165,8 +168,8 @@ class Jwt extends Component public $decoder; /** - * @var array>|Validation\Constraint[]|Closure|null List of constraints that will be used to validate against or - * an anonymous function that can be resolved as such list. The signature of the function should be + * @var array>|Validation\Constraint[]|Closure|null List of constraints that will be used to validate + * against or an anonymous function that can be resolved as such list. The signature of the function should be * `function(\bizley\jwt\Jwt $jwt)` where $jwt will be an instance of this component. * For the constraints you can use instances of Lcobucci\JWT\Validation\Constraint or configuration arrays to be * resolved as such. @@ -211,6 +214,22 @@ public function init(): void } } + /** + * @param array $config + * @return object + * @throws InvalidConfigException + */ + private function buildObjectFromArray(array $config): object + { + $keys = array_keys($config); + if (is_string(reset($keys))) { + // most probably Yii-style config + return Yii::createObject($config); + } + + return Yii::createObject(...$config); + } + /** * @throws InvalidConfigException * @since 3.0.0 @@ -380,7 +399,7 @@ private function prepareSigner($signer): Signer } /** @var Signer $signerInstance */ - $signerInstance = Yii::createObject(...$this->signers[$signer]); + $signerInstance = $this->buildObjectFromArray($this->signers[$signer]); return $signerInstance; } @@ -404,7 +423,7 @@ private function prepareValidationConstraints(): array $constraints[] = $constraint; } else { /** @var Validation\Constraint $constraintInstance */ - $constraintInstance = Yii::createObject(...$constraint); + $constraintInstance = $this->buildObjectFromArray($constraint); $constraints[] = $constraintInstance; } } diff --git a/tests/ConstraintsConfigTest.php b/tests/ConstraintsConfigTest.php index a4ed302..8881cb7 100644 --- a/tests/ConstraintsConfigTest.php +++ b/tests/ConstraintsConfigTest.php @@ -5,6 +5,7 @@ namespace bizley\tests; use bizley\jwt\Jwt; +use bizley\tests\stubs\YiiConstraint; use Closure; use Lcobucci\JWT\Token; use Lcobucci\JWT\Validation\Constraint; @@ -53,6 +54,13 @@ public function testArrayConfigWithArray(): void self::assertTrue($jwt->validate($this->getToken($jwt))); } + public function testArrayConfigWithYiiArray(): void + { + $jwt = $this->getJwt([['class' => YiiConstraint::class, 'test' => 'yii']]); + + self::assertTrue($jwt->validate($this->getToken($jwt))); + } + public function testArrayConfigWithClosure(): void { $jwt = $this->getJwt(static function (Jwt $jwt) { diff --git a/tests/stubs/YiiConstraint.php b/tests/stubs/YiiConstraint.php new file mode 100644 index 0000000..53db7cc --- /dev/null +++ b/tests/stubs/YiiConstraint.php @@ -0,0 +1,18 @@ +