Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

112 lines
3.7KB

  1. <?php
  2. namespace Lc\SovBundle\Twig;
  3. use App\Entity\Page;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  6. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\HttpKernel\KernelInterface;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Twig\Extension\AbstractExtension;
  11. use Twig\TwigFilter;
  12. use Twig\TwigFunction;
  13. class TwigExtension extends AbstractExtension
  14. {
  15. protected $em;
  16. protected $kernel;
  17. protected $parameterBag;
  18. protected $cacheManager;
  19. protected $requestStack;
  20. protected $router;
  21. public function __construct(KernelInterface $kernel, ParameterBagInterface $parameterBag, CacheManager $cacheManager, EntityManagerInterface $entityManager, RequestStack $requestStack,UrlGeneratorInterface $router)
  22. {
  23. $this->kernel = $kernel;
  24. $this->parameterBag = $parameterBag;
  25. $this->cacheManager = $cacheManager;
  26. $this->em = $entityManager;
  27. $this->requestStack = $requestStack;
  28. $this->router = $router;
  29. }
  30. public function getFunctions()
  31. {
  32. return array(
  33. new TwigFunction('lc_liip', [$this, 'lcLiip']),
  34. new TwigFunction('page_by_dev_alias', [$this, 'getPageByDevAlias']),
  35. new TwigFunction('translated_urls', [$this, 'getTranslatedUrls'])
  36. );
  37. }
  38. public function getFilters()
  39. {
  40. return [
  41. new TwigFilter('lc_cache', [$this, 'lcCache']),
  42. ];
  43. }
  44. public function lcCache($file)
  45. {
  46. $cacheTime = filemtime($this->kernel->getProjectDir() . '/public' . $file);
  47. if ($cacheTime) {
  48. return $file . '?c=' . $cacheTime;
  49. } else {
  50. return $file . "?c=0";
  51. }
  52. }
  53. public function lcLiip($path, $thumb = 'tile', $default = 'default.jpg')
  54. {
  55. if (substr($path, 0, 1) === '/') $path = substr($path, 1);
  56. if ($path) {
  57. $fileManagerFolder = substr($this->getFileManagerFolder(), 1) ;
  58. if (strpos($path, $fileManagerFolder) === false) {
  59. $path = $fileManagerFolder . '/' . $path;
  60. }
  61. if (file_exists($path)) {
  62. return $this->cacheManager->getBrowserPath($path, $thumb);
  63. }
  64. }
  65. return $this->cacheManager->getBrowserPath($this->getFileManagerFolder() . '/' . $default, $thumb);
  66. }
  67. function getTranslatedUrls()
  68. {
  69. $ret = array();
  70. $langs = $this->parameterBag->get('app.locales');
  71. $currentRoute = $this->requestStack->getCurrentRequest()->get('_route');
  72. $params = array_merge((array)$this->requestStack->getCurrentRequest()->get('_route_params'), $_GET);
  73. if ($currentRoute) {
  74. foreach($langs as $lg) {
  75. $ret[$lg] = $this->router->generate($currentRoute, array_merge($params, array('_locale'=>$lg)));
  76. }
  77. }
  78. return $ret;
  79. }
  80. public function getFileManagerFolder()
  81. {
  82. return $this->parameterBag->get('app.path_uploads');
  83. }
  84. public function getPageByDevAlias($devAlias)
  85. {
  86. return $this->em->getRepository(Page::class)->findOneByDevAlias($devAlias);
  87. }
  88. }