5

我正在开发一个 Symfony 2.7 WebApp。我创建的其中一个捆绑包包括一项服务,该服务提供一些与用户相关的内容,例如userHasPurchases().

问题是,包括Twig Extesion中断另一个服务:

应用商店服务

namespace AppShopBundle\Service;

use AppBundle\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
...

class AppShopService {
    protected $user;

    public function __construct(TokenStorageInterface $tokenStorage, ...) {
        $this->user = $tokenStorage->getToken() ? $tokenStorage->getToken()->getUser() : null;
        ...
    }

    public function userHasPurchases(User $user) {
        $user = $user ? $user : $this->user;
        $result = $user...
        return result;
    }
}

AppShopBundle\Resources\config\services.yml

services:
    app_shop.service:
        class: AppShopBundle\Service\AppShopService
        arguments: 
            - "@security.token_storage"
            - ...

到目前为止一切正常:AppShopServices使用当前用户创建userHasPurchases()并按预期工作。

现在我添加了一个Twig 扩展,以便能够userHasPurchases()在我的模板中使用:

树枝扩展

namespace AppShopBundle\Twig;

use AppShopBundle\Service\AppShopService;

class AppShopExtension extends \Twig_Extension {
    private $shopService;

    public function __construct(AppShopService $shopService) {
        $this->shopService = $shopService;
    }

    public function getName() {
        return 'app_shop_bundle_extension';
    }

    public function getFunctions() {
        $functions = array();

        $functions[] = new \Twig_SimpleFunction('userHasPurchases', array(
                $this,
                'userHasPurchases'
            ));

        return $functions;
    }

    public function userHasPurchases($user) {
        return $this->shopService->userHasPurchases($user);
    }
}

在 AppShopBundle\Resources\config\services.yml 中包含扩展

services:
    app_shop.service:
        class: AppShopBundle\Service\AppShopService
        arguments: 
            - "@security.token_storage"
            - ...

    app_shop.twig_extension:
        class: AppShopBundle\Twig\AppShopExtension
        arguments: 
          - "@app_shop.service"
        tags:
          - { name: twig.extension }

包含 ,之后Twig ExtensionAppShopService它的方法userHasPurchases不再起作用。问题是, 的构造函数AppShopService不再设置user,因为$tokenStorage->getToken()现在返回null

这怎么可能?除了包括Twig Extension. 一旦我Twig Extensionservices.yml所有东西中删除它,它就会再次正常工作。

我唯一的猜测是,它的创建Twig Extension是在任何安全性之前完成的。但为什么?

知道这里可能有什么问题吗?

4

1 回答 1

3

不要在构造函数中与 tokenStorage 交互,而只能在userHasPurchases方法中。

namespace AppShopBundle\Service;

use AppBundle\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
...

class AppShopService {
    protected $tokenStorage;

    public function __construct(TokenStorageInterface $tokenStorage, ...) {
        $this->tokenStorage = $tokenStorage;
    }

    public function userHasPurchases(User $user) {
        $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
        $result = $user...
        return result;
    }
}

希望这有帮助

于 2016-04-21T13:00:25.983 回答