您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TwigExtension.php 6.8KB

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