将存储库注入 phpleague/oauth-server:authServer

Inject repository into phpleague/oauth-server:authServer

我目前正在尝试在 symfony 3.3 项目中设置 phpleague/oauth-server。出于这个原因,我想将 AuthorizationServer 指定为服务,以便能够从容器中加载它(而不是在它使用的任何地方设置整个东西)。

要将 AuthorizationServer 设置为服务,我需要注入多个存储库作为参数。

这是 AuthorizationServer 的服务定义:

app.oauth2.authorization_server:
        class: League\OAuth2\Server\AuthorizationServer
        arguments: ["@app.oauth2.client_repository", "@app.oauth2.access_token_repository", "@app.oauth2.scope_repository", "%oauth_private_key%", "%oauth_encryption_key%"]
        configurator: "app.oauth2.authorization_server.configurator:configure"

存储库的当前定义如下所示:

app.oauth2.client_repository:
    class: Appbundle\Repository\OAuth2\ClientRepository
    factory: 'doctrine.orm.entity_manager:getRepository'
    arguments: [AppBundle\Entity\OAuth2\Client]
...

我尝试了多种将存储库定义为服务的方法,但每次我都会遇到同样的错误:

Type error: Argument 1 passed to League\OAuth2\Server\AuthorizationServer::__construct() must be an instance of League\OAuth2\Server\Repositories\ClientRepositoryInterface, instance of Doctrine\ORM\EntityRepository given

这是 ClientRepository 的样子:

<?php

namespace AppBundle\Repository\OAuth2;

use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;

class ClientRepository implements ClientRepositoryInterface
{
    /**
     * Get a client.
     *
     * @param string $clientIdentifier The client's identifier
     * @param string $grantType The grant type used
     * @param null|string $clientSecret The client's secret (if sent)
     * @param bool $mustValidateSecret If true the client must attempt to validate the secret if the client
     *                                        is confidential
     *
     * @return ClientEntityInterface
     */
    public function getClientEntity($clientIdentifier, $grantType, $clientSecret = null, $mustValidateSecret = true)
    {
        // TODO: Implement getClientEntity() method.
    }
}

以下是我尝试实现的其他一些方法:

https://matthiasnoback.nl/2014/05/inject-a-repository-instead-of-an-entity-manager/

他们都没有工作。你们中有人知道为什么我的存储库的服务定义不被接受为 AuthorizationServer 的有效输入吗?

你的, 调频

我曾尝试扩展 EntityRepository 但忘记在实体中定义存储库。随着存储库扩展 entityRepository 和实体中的 ORM\Entity 定义,它似乎可以工作!