You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TwigExtension.php 6.0KB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace Lc\SovBundle\Twig;
  3. use App\Repository\ReminderRepository;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lc\SovBundle\Component\DateComponent;
  6. use Lc\SovBundle\Component\MetaComponent;
  7. use Lc\SovBundle\Component\StringComponent;
  8. use Lc\SovBundle\Form\Newsletter\NewsletterType;
  9. use Lc\SovBundle\Model\File\FileInterface;
  10. use Lc\SovBundle\Repository\Reminder\ReminderStore;
  11. use Lc\SovBundle\Translation\TranslatorAdmin;
  12. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  13. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  14. use Symfony\Component\Form\FormFactoryInterface;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\HttpKernel\KernelInterface;
  17. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  18. use Symfony\Component\Security\Core\Security;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. use Twig\Extension\AbstractExtension;
  21. use Twig\TwigFilter;
  22. use Twig\TwigFunction;
  23. class TwigExtension extends AbstractExtension
  24. {
  25. protected EntityManagerInterface $em;
  26. protected KernelInterface $kernel;
  27. protected ParameterBagInterface $parameterBag;
  28. protected CacheManager $cacheManager;
  29. protected RequestStack $requestStack;
  30. protected UrlGeneratorInterface $router;
  31. protected TranslatorInterface $translator;
  32. protected TranslatorAdmin $translatorAdmin;
  33. protected ReminderStore $reminderStore;
  34. protected Security $security;
  35. protected FormFactoryInterface $formFactory;
  36. protected StringComponent $stringComponent;
  37. protected MetaComponent $metaComponent;
  38. protected DateComponent $dateComponent;
  39. public function __construct(
  40. KernelInterface $kernel,
  41. ParameterBagInterface $parameterBag,
  42. CacheManager $cacheManager,
  43. EntityManagerInterface $entityManager,
  44. RequestStack $requestStack,
  45. UrlGeneratorInterface $router,
  46. TranslatorInterface $translator,
  47. TranslatorAdmin $translatorAdmin,
  48. ReminderStore $reminderStore,
  49. Security $security,
  50. FormFactoryInterface $formFactory,
  51. StringComponent $stringComponent,
  52. MetaComponent $metaComponent,
  53. DateComponent $dateComponent
  54. ) {
  55. $this->kernel = $kernel;
  56. $this->parameterBag = $parameterBag;
  57. $this->cacheManager = $cacheManager;
  58. $this->em = $entityManager;
  59. $this->requestStack = $requestStack;
  60. $this->router = $router;
  61. $this->translator = $translator;
  62. $this->translatorAdmin = $translatorAdmin;
  63. $this->reminderStore = $reminderStore;
  64. $this->security = $security;
  65. $this->formFactory = $formFactory;
  66. $this->stringComponent = $stringComponent;
  67. $this->metaComponent = $metaComponent;
  68. $this->dateComponent = $dateComponent;
  69. }
  70. public function getFunctions()
  71. {
  72. return [
  73. new TwigFunction('sov_liip', [$this, 'liip']),
  74. new TwigFunction('liip', [$this, 'liip']),
  75. new TwigFunction('sov_get_by_devalias', [$this, 'getByDevAlias']),
  76. new TwigFunction('sov_parameter', [$this, 'getParameter']),
  77. new TwigFunction('sov_homepage_route', [$this, 'getHomepageRoute']),
  78. new TwigFunction('lc_format_price', [$this, 'formatPrice']),
  79. new TwigFunction('die', [$this, 'die']),
  80. new TwigFunction('get_form_newsletter', [$this, 'getFormNewsletter']),
  81. new TwigFunction('sov_limit_text', [$this, 'limitText']),
  82. new TwigFunction('day_by_number', [$this, 'getDayByNumber']),
  83. ];
  84. }
  85. public function die()
  86. {
  87. die();
  88. }
  89. public function getFilters()
  90. {
  91. return [
  92. new TwigFilter('uc_first', [$this, 'ucFirst']),
  93. new TwigFilter('format_price', [$this, 'formatPrice']),
  94. new TwigFilter('sov_cache', [$this, 'sovCache']),
  95. new TwigFilter('slugify', [$this, 'slugify']),
  96. ];
  97. }
  98. public function sovCache($file)
  99. {
  100. $cacheTime = filemtime($this->kernel->getProjectDir() . '/public' . $file);
  101. if ($cacheTime) {
  102. return $file . '?c=' . $cacheTime;
  103. } else {
  104. return $file . "?c=0";
  105. }
  106. }
  107. public function formatPrice($price, $unbreakableSpace = true)
  108. {
  109. $price = number_format($price, 2, ',', ' ');
  110. $price .= $unbreakableSpace ? '&nbsp;' : ' ';
  111. $price .= '€';
  112. return $price;
  113. }
  114. public function ucFirst($string)
  115. {
  116. return ucfirst($string);
  117. }
  118. public function liip($path, $thumb = 'tile', $default = 'default.jpg')
  119. {
  120. if (substr($path, 0, 1) === '/') {
  121. $path = substr($path, 1);
  122. }
  123. if ($path) {
  124. $fileManagerFolder = substr($this->getFileManagerFolder(), 1);
  125. if (strpos($path, $fileManagerFolder) === false) {
  126. $path = $fileManagerFolder . '/' . $path;
  127. }
  128. if (file_exists($path)) {
  129. return $this->cacheManager->getBrowserPath($path, $thumb);
  130. }
  131. }
  132. return $this->cacheManager->getBrowserPath($this->getFileManagerFolder() . '/' . $default, $thumb);
  133. }
  134. public function getFileManagerFolder()
  135. {
  136. return $this->parameterBag->get('app.path_uploads');
  137. }
  138. public function getHomepageRoute()
  139. {
  140. return $this->parameterBag->get('lc_sov.homepage_route');
  141. }
  142. public function getParameter($name)
  143. {
  144. return $this->parameterBag->get($name);
  145. }
  146. public function getFormNewsletter()
  147. {
  148. $form = $this->formFactory->create(NewsletterType::class);
  149. return $form->createView();
  150. }
  151. public function limitText(string $text, int $limit)
  152. {
  153. return $this->stringComponent->limitText($text, $limit);
  154. }
  155. public function getDayByNumber($day): string
  156. {
  157. return $this->dateComponent->getDayByNumber($day);
  158. }
  159. public function slugify($string)
  160. {
  161. return $this->stringComponent->slugify($string) ;
  162. }
  163. }