Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

182 linhas
5.9KB

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