1

I am using the following packages:

    "friendsofsymfony/oauth-server-bundle": "^1.6",
    "friendsofsymfony/rest-bundle": "^2.5",
    "friendsofsymfony/user-bundle": "^2.1",
    "symfony/framework-bundle": "4.2.*",
    "symfony/http-foundation": "4.2.*",
    "symfony/http-kernel": "4.2.*"

I am attempting to override the tokenAction method of the FOSOAuthServerBundle package, but I am getting stuck on the error:

"Cannot autowire service App\Controller\TokenController argument $server of method FOS\OAuthServerBundle\Controller\TokenController::__construct() references class OAuth2\OAuth2; but no such service exists. You should maybe alias this class to the existing fos_oauth_server.server service"

I have tried several different approaches (autowire, auto-injection), but I keep coming full circle back to the error described above. It seems that the "use OAuth2\OAuth2;" reference is properly namespaced in the bundle's TokenController, but when I try to override it cannot parse the OAuth2 class locations properly, and I am not sure what pattern to use either in the class or in the services.yaml to point it to the right location.

Here is my services.yaml

services:

    ...

    App\Controller\TokenController\:
        resource: '../src/Controller/TokenController.php'
        arguments: ['@fos_oauth_server.server']

And my custom TokenController class

?php

namespace App\Controller;

use FOS\OAuthServerBundle\Controller\TokenController as BaseController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class TokenController extends BaseController
{

    /**
     * @param Request $request
     *
     * @return Response
     */
    public function tokenAction(Request $request)
    {
        $token = parent::tokenAction($request);

        // my custom code here 

        return $token;
    }

}

And if I try to do the obvious and add the line

use OAuth2\OAuth2;

to my custom TokenController I get the same error.

1 Answers1

0

Turns out the answer was to use decorators

In my services.yaml

    App\Controller\OAuth\OAuthTokenController:
        decorates: FOS\OAuthServerBundle\Controller\TokenController
        arguments: ['@fos_oauth_server.server']

Any my custom class to override the TokenController

namespace App\Controller\OAuth;

use FOS\OAuthServerBundle\Controller\TokenController as BaseController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class OAuthTokenController extends BaseController
{

    /**
     * @param Request $request
     *
     * @return Response
     */
    public function tokenAction(Request $request)
    {
        try {
            $token = $this->server->grantAccessToken($request);

            // custom code here

            return $token;
        } catch (OAuth2ServerException $e) {
            return $e->getHttpResponse();
        }
    }

}