FrontendTwigExtension.php 4.1KB

4 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Lc\ShopBundle\Twig;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\ShopBundle\Context\MerchantInterface;
  5. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  6. use Lc\ShopBundle\Context\ProductCategoryInterface;
  7. use Lc\ShopBundle\Context\ProductFamilyInterface;
  8. use Lc\ShopBundle\Form\Frontend\NewsletterType;
  9. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  10. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  11. use Symfony\Component\Form\FormFactoryInterface;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpKernel\KernelInterface;
  14. use Symfony\Component\Routing\RouterInterface;
  15. use Symfony\Component\Security\Core\Security;
  16. use Twig\Extension\AbstractExtension;
  17. use Twig\TwigFilter;
  18. use Twig\TwigFunction;
  19. class FrontendTwigExtension extends AbstractExtension
  20. {
  21. protected $em;
  22. protected $security;
  23. protected $merchantUtils;
  24. protected $formFactory;
  25. protected $requestStack;
  26. protected $productCategoryRepository;
  27. protected $merchantRepository;
  28. protected $productFamilyRepository;
  29. protected $liipCacheHelper;
  30. protected $parameterBag;
  31. protected $kernel;
  32. protected $router ;
  33. public function __construct(EntityManagerInterface $em, Security $security, MerchantUtilsInterface $merchantUtils,
  34. FormFactoryInterface $formFactory, RequestStack $requestStack, ParameterBagInterface $parameterBag,
  35. KernelInterface $kernel, RouterInterface $router)
  36. {
  37. $this->em = $em;
  38. $this->security = $security;
  39. $this->merchantUtils = $merchantUtils;
  40. $this->formFactory = $formFactory;
  41. $this->requestStack = $requestStack;
  42. $this->productCategoryRepository = $this->em->getRepository($this->em->getClassMetadata(ProductCategoryInterface::class)->getName());
  43. $this->merchantRepository = $this->em->getRepository($this->em->getClassMetadata(MerchantInterface::class)->getName());
  44. $this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetadata(ProductFamilyInterface::class)->getName());
  45. $this->parameterBag = $parameterBag;
  46. $this->kernel = $kernel;
  47. $this->router = $router ;
  48. }
  49. public function getFunctions()
  50. {
  51. return array(
  52. new TwigFunction('get_product_categories', [$this, 'getProductCategories']),
  53. new TwigFunction('get_form_newsletter', [$this, 'getFormNewsletter']),
  54. new TwigFunction('get_merchants', [$this, 'getMerchants']),
  55. new TwigFunction('get_file_manager_folder', [$this, 'getFileManagerFolder']),
  56. );
  57. }
  58. public function getFilters()
  59. {
  60. return [
  61. new TwigFilter('format_price', [$this, 'formatPrice']),
  62. new TwigFilter('lc_cache', [$this, 'lcCache']),
  63. ];
  64. }
  65. public function lcCache($file)
  66. {
  67. $cacheTime = filemtime($this->kernel->getProjectDir() . '/public' . $file);
  68. if ($cacheTime) {
  69. return $file . '?c=' . $cacheTime;
  70. } else {
  71. return $file . "?c=0";
  72. }
  73. }
  74. public function getProductCategories()
  75. {
  76. $categories = $this->productCategoryRepository->findAllParents(false);
  77. return $categories;
  78. }
  79. public function getFormNewsletter()
  80. {
  81. $form = $this->formFactory->create(NewsletterType::class);
  82. return $form->createView();
  83. }
  84. public function formatPrice($price)
  85. {
  86. $price = number_format($price, 2, ',', ' ');
  87. $price = $price . '&nbsp;€';
  88. return $price;
  89. }
  90. public function getMerchants()
  91. {
  92. return $this->merchantRepository->findAll();
  93. }
  94. }