symfonyservicedependency-injectionoauththephpleague

Inject repository into phpleague/oauth-server:authServer


I'm currently trying to set up the phpleague/oauth-server in a symfony 3.3 project. For that reason i want to specify the AuthorizationServer as service to be able to load it from the container (an not set up the whole thing everywhere its used).

To set the AuthorizationServer as service I need to inject multiple repositories as arguments.

This is the service definition for the 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"

The current definition of the repositories looks like this:

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

I tried many ways of defining the repositories as services but everytime i get the same error:

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

This is how the ClientRepository looks like:

<?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.
    }
}

Here are some other ways i tried to implement it:

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

How to configure dependency injection for repository class in symfony 3

Neither of them worked. Has any one of you an idea why the service definition of my repository is not accepted as valid input for the AuthorizationServer?

Yours, FMK


Solution

  • I had tried to extend the EntityRepository but forgot to define the repository in the Entities. With the repositories extending the entityRepository and the ORM\Entity definition in the Entities it seems to work!