【发布时间】:2017-11-10 15:29:07
【问题描述】:
我目前正在尝试在 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/
How to configure dependency injection for repository class in symfony 3
他们都没有工作。你们有人知道为什么我的存储库的服务定义不被接受为AuthorizationServer 的有效输入吗?
你的, FMK
【问题讨论】:
-
看起来您没有将 Client 实体正确映射到存储库。因此使用了默认的 EntityRepository。您的客户端存储库将需要扩展 Doctrine EntityRepository 才能工作。
-
我曾尝试扩展 EntityRepository,但忘记在实体中定义存储库。随着存储库扩展 entityRepository 和实体中的 ORM\Entity 定义,它似乎可以工作!
标签: symfony service dependency-injection oauth thephpleague