Symfony Mercure(捆绑包)Cookiegenerator.php 问题

问题描述 投票:0回答:1

我尝试修复 Cookiegenerator.php 文件以使用我的 JWT 密钥生成令牌,但我有一些错误:

            <?php

            declare(strict_types=1);

            namespace App\Services\Mercure;

            use App\Entity\Channel;
            use Lcobucci\JWT\Token\Builder;
            use Lcobucci\JWT\Signer\Hmac\Sha256;
            use Lcobucci\JWT\Signer\Key;

            class CookieGenerator
            {
                private string $key;

                public function __construct(string $key)
                {
                    $this->key = $key;
                }

                public function __invoke(Channel $channel): string
                {
                    $signer = new Sha256();
                    return (new Builder())
                        ->withClaim('mercure', ['subscribe' => [sprintf('http://astrochat.com/channel/%s', $channel->getId())]])
                        ->getToken($signer, new Key($this->key))
                        ->__toString()
                    ;
                }
            }

错误:

预计有 2 个参数。找到 0。(L 24) 未定义的方法“__toString”。 (L 27)

我尝试重做该文件并将一些参数传递给 Builder 但无事可做...基本上该文件使用 use Lcobucci\JWT\Builder;我以为我明白它不再使用所以我使用 use Lcobucci\JWT\Token\Builder;有人会有什么想法吗?非常感谢

php symfony jwt token mercure
1个回答
0
投票

正如上面评论中所讨论的,token类不再实现

Stringable
接口,并且也没有
__toString()
方法(从PHP 8.0开始将隐式实现它),因此对魔术字符串的调用方法:

 ->__toString()

现在需要更新为:

 ->toString()

此外,

Builder
实例现在需要2个参数(docs),一个编码器和一个格式化程序,例如:

$tokenBuilder = (new Builder(new \Lcobucci\JWT\Encoding\JoseEncoder(), \Lcobucci\JWT\Encoding\ChainedFormatter::default()));
© www.soinside.com 2019 - 2024. All rights reserved.