vendor/boldr/shop-bundle/src/Twig/Extension.php line 50

Open in your IDE?
  1. <?php
  2. namespace Boldr\Shop\ShopBundle\Twig;
  3. use Twig\Extension\AbstractExtension;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. use Boldr\Shop\ShopBundle\ShopContext;
  6. use Boldr\Shop\ShopBundle\CurrencyFormatter;
  7. use Boldr\Shop\ShopBundle\Presenter\Order\{ OrderPresenter, OrderView };
  8. use Boldr\Shop\ShopBundle\Presenter\Currency\{ CurrencyPresenter, CurrencyView };
  9. use Boldr\Shop\ShopBundle\Presenter\Customer\{ CustomerPresenter, CustomerView };
  10. use Symfony\Contracts\Service\ServiceSubscriberInterface;
  11. use Psr\Container\ContainerInterface;
  12. use Twig\{ TwigFunction, TwigFilter };
  13. class Extension extends AbstractExtension implements ServiceSubscriberInterface
  14. {
  15. private ContainerInterface $container;
  16. public function __construct(ContainerInterface $container)
  17. {
  18. $this->container = $container;
  19. }
  20. public static function getSubscribedServices()
  21. {
  22. return [
  23. RequestStack::class,
  24. ShopContext::class,
  25. CurrencyFormatter::class,
  26. OrderPresenter::class,
  27. CurrencyPresenter::class,
  28. CustomerPresenter::class,
  29. ];
  30. }
  31. public function getFunctions()
  32. {
  33. return [
  34. new TwigFunction('boldr_shop_get_cart', [$this, 'createCartView']),
  35. new TwigFunction('boldr_shop_get_customer', [$this, 'createCustomerView']),
  36. new TwigFunction('boldr_shop_get_currencies', [$this, 'getCurrencies']),
  37. new TwigFunction('boldr_shop_get_currency', [$this, 'getCurrency']),
  38. new TwigFunction('boldr_shop_format_currency', [$this->container->get(CurrencyFormatter::class), 'format']),
  39. ];
  40. }
  41. public function createCartView(): OrderView
  42. {
  43. $locale = $this->container->get(RequestStack::class)->getCurrentRequest()->getLocale();
  44. $currentOrder = $this->container->get(ShopContext::class)->getCurrentOrder();
  45. return $this->container->get(OrderPresenter::class)->createOrderView($currentOrder, $locale);
  46. }
  47. public function createCustomerView(): ?CustomerView
  48. {
  49. $customer = $this->container->get(ShopContext::class)->getCustomer();
  50. if ($customer === null)
  51. return null;
  52. $locale = $this->container->get(RequestStack::class)->getCurrentRequest()->getLocale();
  53. return $this->container->get(CustomerPresenter::class)->createCustomerView($customer, $locale);
  54. }
  55. /**
  56. * @return CurrencyView[]
  57. */
  58. public function getCurrencies(): array
  59. {
  60. $currencyPresenter = $this->container->get(CurrencyPresenter::class);
  61. return array_map(
  62. fn($currency) => $currencyPresenter->createCurrencyView($currency),
  63. array_keys($this->container->get(ShopContext::class)->getCurrencies())
  64. );
  65. }
  66. public function getCurrency(): CurrencyView
  67. {
  68. return $this->container->get(CurrencyPresenter::class)->createCurrencyView($this->container->get(ShopContext::class)->getCurrency());
  69. }
  70. }