FrontendTwigExtension.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\Security\Core\Security;
  15. use Twig\Extension\AbstractExtension;
  16. use Twig\TwigFilter;
  17. use Twig\TwigFunction;
  18. class FrontendTwigExtension extends AbstractExtension
  19. {
  20. protected $em;
  21. protected $security;
  22. protected $merchantUtils;
  23. protected $formFactory;
  24. protected $requestStack;
  25. protected $productCategoryRepository;
  26. protected $merchantRepository;
  27. protected $productFamilyRepository;
  28. protected $liipCacheHelper;
  29. protected $parameterBag;
  30. protected $kernel;
  31. public function __construct(EntityManagerInterface $em, Security $security, MerchantUtilsInterface $merchantUtils,
  32. FormFactoryInterface $formFactory, RequestStack $requestStack, ParameterBagInterface $parameterBag, KernelInterface $kernel)
  33. {
  34. $this->em = $em;
  35. $this->security = $security;
  36. $this->merchantUtils = $merchantUtils;
  37. $this->formFactory = $formFactory;
  38. $this->requestStack = $requestStack;
  39. $this->productCategoryRepository = $this->em->getRepository($this->em->getClassMetadata(ProductCategoryInterface::class)->getName());
  40. $this->merchantRepository = $this->em->getRepository($this->em->getClassMetadata(MerchantInterface::class)->getName());
  41. $this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetadata(ProductFamilyInterface::class)->getName());
  42. $this->parameterBag = $parameterBag;
  43. $this->kernel = $kernel;
  44. }
  45. public function getFunctions()
  46. {
  47. return array(
  48. new TwigFunction('get_product_categories', [$this, 'getProductCategories']),
  49. new TwigFunction('get_form_newsletter', [$this, 'getFormNewsletter']),
  50. new TwigFunction('get_merchants', [$this, 'getMerchants']),
  51. new TwigFunction('get_file_manager_folder', [$this, 'getFileManagerFolder']),
  52. );
  53. }
  54. public function getFilters()
  55. {
  56. return [
  57. new TwigFilter('format_price', [$this, 'formatPrice']),
  58. new TwigFilter('lc_cache', [$this, 'lcCache']),
  59. ];
  60. }
  61. public function lcCache($file)
  62. {
  63. $cacheTime = filemtime($this->kernel->getProjectDir() . '/public' . $file);
  64. if ($cacheTime) {
  65. return $file . '?c=' . $cacheTime;
  66. } else {
  67. return $file . "?c=0";
  68. }
  69. }
  70. public function getProductCategories()
  71. {
  72. $categories = $this->productCategoryRepository->findAllParents(false);
  73. return $categories;
  74. }
  75. public function getFormNewsletter()
  76. {
  77. $form = $this->formFactory->create(NewsletterType::class);
  78. return $form->createView();
  79. }
  80. public function formatPrice($price)
  81. {
  82. $price = number_format($price, 2, ',', ' ');
  83. $price = $price . '&nbsp;€';
  84. return $price;
  85. }
  86. public function getMerchants()
  87. {
  88. return $this->merchantRepository->findAll();
  89. }
  90. }