Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

110 lines
3.3KB

  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. ];
  60. }
  61. public function getFilters()
  62. {
  63. return [];
  64. }
  65. public function liip($path, $thumb = 'tile', $default = 'default.jpg')
  66. {
  67. if (substr($path, 0, 1) === '/') {
  68. $path = substr($path, 1);
  69. }
  70. if ($path) {
  71. $fileManagerFolder = substr($this->getFileManagerFolder(), 1);
  72. if (strpos($path, $fileManagerFolder) === false) {
  73. $path = $fileManagerFolder . '/' . $path;
  74. }
  75. if (file_exists($path)) {
  76. return $this->cacheManager->getBrowserPath($path, $thumb);
  77. }
  78. }
  79. return $this->cacheManager->getBrowserPath($this->getFileManagerFolder() . '/' . $default, $thumb);
  80. }
  81. public function getFileManagerFolder()
  82. {
  83. return $this->parameterBag->get('app.path_uploads');
  84. }
  85. public function getHomepageRoute()
  86. {
  87. return $this->parameterBag->get('lc_sov.homepage_route');
  88. }
  89. public function getParameter($name)
  90. {
  91. return $this->parameterBag->get($name) ;
  92. }
  93. }