Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

111 lines
3.3KB

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