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.

195 linhas
6.2KB

  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. new TwigFunction('user_current', [$this, 'getUserCurrent']),
  84. ];
  85. }
  86. public function die()
  87. {
  88. die();
  89. }
  90. public function getFilters()
  91. {
  92. return [
  93. new TwigFilter('uc_first', [$this, 'ucFirst']),
  94. new TwigFilter('format_price', [$this, 'formatPrice']),
  95. new TwigFilter('sov_cache', [$this, 'sovCache']),
  96. new TwigFilter('slugify', [$this, 'slugify']),
  97. ];
  98. }
  99. public function sovCache($file)
  100. {
  101. $cacheTime = filemtime($this->kernel->getProjectDir() . '/public' . $file);
  102. if ($cacheTime) {
  103. return $file . '?c=' . $cacheTime;
  104. } else {
  105. return $file . "?c=0";
  106. }
  107. }
  108. public function formatPrice($price, $unbreakableSpace = true)
  109. {
  110. $price = number_format($price, 2, ',', ' ');
  111. $price .= $unbreakableSpace ? '&nbsp;' : ' ';
  112. $price .= '€';
  113. return $price;
  114. }
  115. public function ucFirst($string)
  116. {
  117. return ucfirst($string);
  118. }
  119. public function liip($path, $thumb = 'tile', $default = 'default.jpg')
  120. {
  121. if (substr($path, 0, 1) === '/') {
  122. $path = substr($path, 1);
  123. }
  124. if ($path) {
  125. $fileManagerFolder = substr($this->getFileManagerFolder(), 1);
  126. if (strpos($path, $fileManagerFolder) === false) {
  127. $path = $fileManagerFolder . '/' . $path;
  128. }
  129. if (file_exists($path)) {
  130. return $this->cacheManager->getBrowserPath($path, $thumb);
  131. }
  132. }
  133. return $this->cacheManager->getBrowserPath($this->getFileManagerFolder() . '/' . $default, $thumb);
  134. }
  135. public function getFileManagerFolder()
  136. {
  137. return $this->parameterBag->get('app.path_uploads');
  138. }
  139. public function getHomepageRoute()
  140. {
  141. return $this->parameterBag->get('lc_sov.homepage_route');
  142. }
  143. public function getParameter($name)
  144. {
  145. return $this->parameterBag->get($name);
  146. }
  147. public function getFormNewsletter()
  148. {
  149. $form = $this->formFactory->create(NewsletterType::class);
  150. return $form->createView();
  151. }
  152. public function limitText(string $text, int $limit)
  153. {
  154. return $this->stringComponent->limitText($text, $limit);
  155. }
  156. public function getDayByNumber($day): string
  157. {
  158. return $this->dateComponent->getDayByNumber($day);
  159. }
  160. public function slugify($string)
  161. {
  162. return $this->stringComponent->slugify($string) ;
  163. }
  164. public function getUserCurrent()
  165. {
  166. return $this->security->getUser();
  167. }
  168. }