Невозможно переопределить FOSOAuthServerBundle tokenAction - PullRequest
1 голос
/ 13 мая 2019

Я использую следующие пакеты:

    "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.*"

Я пытаюсь переопределить метод tokenAction пакета FOSOAuthServerBundle, но я застреваю при ошибке:

"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"

Я пробовал несколько разных подходов (autowire, auto-инъекция), но я продолжаю возвращаться к ошибке, описанной выше.Похоже, что "использовать OAuth2 \ OAuth2;"ссылка правильно помещена в пространство имен в TokenController комплекта, но когда я пытаюсь переопределить ее, она не может правильно проанализировать расположения класса OAuth2, и я не уверен, какой шаблон использовать в классе или в services.yaml, чтобы указать его в правильном расположении.

Вот мой services.yaml

services:

    ...

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

И мой пользовательский класс TokenController

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

}

А если я попытаюсь сделать очевидное и добавить строку

use OAuth2\OAuth2;

на свой пользовательский TokenController я получаю ту же ошибку.

1 Ответ

0 голосов
/ 14 мая 2019

Получается, что ответом было использование декораторов

В моем services.yaml

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

Любой мой пользовательский класс для переопределения 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();
        }
    }

}
...