私は Symfony 2.7 WebApp に取り組んでいます。私が作成したバンドルの 1 つには、ユーザー関連のものを提供するサービスが含まれていますuserHasPurchases()
。
問題は、 a を含めるとTwig Extesion
別のサービスが壊れることです:
AppShopサービス
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 Extension
、AppShopService
そのメソッドuserHasPurchases
は機能しなくなりました。問題は、 のコンストラクターが を返すようになったため、もうAppShopService
設定されていないことです。user
$tokenStorage->getToken()
null
これはどのように可能ですか?を含む以外は何も変更していませんTwig Extension
。Twig Extension
すべてからを削除するとすぐに、services.yml
再び正しく機能します。
私の唯一の推測は、Twig Extension
セキュリティの前に作成が行われるということです。しかし、なぜ?
ここで何が間違っているのでしょうか?