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

TwigExtension.php 3.9KB

3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace Lc\SovBundle\Twig;
  3. use App\Repository\ReminderRepository;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lc\SovBundle\Repository\Reminder\ReminderStore;
  6. use Lc\SovBundle\Translation\TranslatorAdmin;
  7. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  8. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\HttpKernel\KernelInterface;
  11. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  12. use Symfony\Component\Security\Core\Security;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. use Twig\Extension\AbstractExtension;
  15. use Twig\TwigFilter;
  16. use Twig\TwigFunction;
  17. class TwigExtension extends AbstractExtension
  18. {
  19. protected $em;
  20. protected $kernel;
  21. protected $parameterBag;
  22. protected $cacheManager;
  23. protected $requestStack;
  24. protected $router;
  25. protected $translator;
  26. protected $translatorAdmin;
  27. protected $reminderStore;
  28. protected $security;
  29. public function __construct(
  30. KernelInterface $kernel,
  31. ParameterBagInterface $parameterBag,
  32. CacheManager $cacheManager,
  33. EntityManagerInterface $entityManager,
  34. RequestStack $requestStack,
  35. UrlGeneratorInterface $router,
  36. TranslatorInterface $translator,
  37. TranslatorAdmin $translatorAdmin,
  38. ReminderStore $reminderStore,
  39. Security $security
  40. ) {
  41. $this->kernel = $kernel;
  42. $this->parameterBag = $parameterBag;
  43. $this->cacheManager = $cacheManager;
  44. $this->em = $entityManager;
  45. $this->requestStack = $requestStack;
  46. $this->router = $router;
  47. $this->translator = $translator;
  48. $this->translatorAdmin = $translatorAdmin;
  49. $this->reminderStore = $reminderStore;
  50. $this->security = $security;
  51. }
  52. public function getFunctions()
  53. {
  54. return [
  55. new TwigFunction('sov_liip', [$this, 'liip']),
  56. new TwigFunction('sov_get_by_devalias', [$this, 'getByDevAlias']),
  57. new TwigFunction('sov_parameter', [$this, 'getParameter']),
  58. new TwigFunction('sov_homepage_route', [$this, 'getHomepageRoute']),
  59. new TwigFunction('lc_format_price', [$this, 'formatPrice']),
  60. new TwigFunction('die', [$this, 'die']),
  61. ];
  62. }
  63. public function die()
  64. {
  65. die();
  66. }
  67. public function getFilters()
  68. {
  69. return [
  70. new TwigFilter('uc_first', [$this, 'ucFirst']),
  71. new TwigFilter('format_price', [$this, 'formatPrice']),
  72. ];
  73. }
  74. public function formatPrice($price, $unbreakableSpace = true)
  75. {
  76. $price = number_format($price, 2, ',', ' ');
  77. $price .= $unbreakableSpace ? '&nbsp;' : ' ';
  78. $price .= '€';
  79. return $price;
  80. }
  81. public function ucFirst($string)
  82. {
  83. return ucfirst($string);
  84. }
  85. public function liip($path, $thumb = 'tile', $default = 'default.jpg')
  86. {
  87. if (substr($path, 0, 1) === '/') {
  88. $path = substr($path, 1);
  89. }
  90. if ($path) {
  91. $fileManagerFolder = substr($this->getFileManagerFolder(), 1);
  92. if (strpos($path, $fileManagerFolder) === false) {
  93. $path = $fileManagerFolder.'/'.$path;
  94. }
  95. if (file_exists($path)) {
  96. return $this->cacheManager->getBrowserPath($path, $thumb);
  97. }
  98. }
  99. return $this->cacheManager->getBrowserPath($this->getFileManagerFolder().'/'.$default, $thumb);
  100. }
  101. public function getFileManagerFolder()
  102. {
  103. return $this->parameterBag->get('app.path_uploads');
  104. }
  105. public function getHomepageRoute()
  106. {
  107. return $this->parameterBag->get('lc_sov.homepage_route');
  108. }
  109. public function getParameter($name)
  110. {
  111. return $this->parameterBag->get($name);
  112. }
  113. }