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

109 行
3.7KB

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