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

162 行
5.1KB

  1. <?php
  2. namespace Lc\SovBundle\Twig;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  5. use Lc\SovBundle\Translation\TranslatorAdmin;
  6. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\HttpKernel\KernelInterface;
  10. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. use Twig\Extension\AbstractExtension;
  13. use Twig\TwigFilter;
  14. use Twig\TwigFunction;
  15. class TwigExtension extends AbstractExtension
  16. {
  17. protected $em;
  18. protected $kernel;
  19. protected $parameterBag;
  20. protected $cacheManager;
  21. protected $requestStack;
  22. protected $router;
  23. protected $translator;
  24. protected $translatorAdmin;
  25. public function __construct(
  26. KernelInterface $kernel,
  27. ParameterBagInterface $parameterBag,
  28. CacheManager $cacheManager,
  29. EntityManagerInterface $entityManager,
  30. RequestStack $requestStack,
  31. UrlGeneratorInterface $router,
  32. TranslatorInterface $translator,
  33. TranslatorAdmin $translatorAdmin
  34. ) {
  35. $this->kernel = $kernel;
  36. $this->parameterBag = $parameterBag;
  37. $this->cacheManager = $cacheManager;
  38. $this->em = $entityManager;
  39. $this->requestStack = $requestStack;
  40. $this->router = $router;
  41. $this->translator = $translator;
  42. $this->translatorAdmin = $translatorAdmin;
  43. }
  44. public function getFunctions()
  45. {
  46. return array(
  47. new TwigFunction('lc_liip', [$this, 'lcLiip']),
  48. new TwigFunction('homepage_route', [$this, 'homepageRoute']),
  49. new TwigFunction('page_by_dev_alias', [$this, 'getPageByDevAlias']),
  50. new TwigFunction('translated_urls', [$this, 'getTranslatedUrls'])
  51. );
  52. }
  53. public function getFilters()
  54. {
  55. return [
  56. new TwigFilter('lc_cache', [$this, 'lcCache']),
  57. new TwigFilter('lc_trans_admin_field', [$this, 'lcTransAdminField']),
  58. new TwigFilter('lc_trans_admin_help', [$this, 'lcTransAdminHelp']),
  59. new TwigFilter('lc_trans_admin_panel', [$this, 'lcTransAdminPanel']),
  60. new TwigFilter('lc_trans_admin_menu', [$this, 'lcTransAdminMenu']),
  61. new TwigFilter('lc_trans_admin_title', [$this, 'lcTransAdminTitle']),
  62. new TwigFilter('lc_trans_admin_action', [$this, 'lcTransAdminAction']),
  63. ];
  64. }
  65. public function lcCache($file)
  66. {
  67. $cacheTime = filemtime($this->kernel->getProjectDir() . '/public' . $file);
  68. if ($cacheTime) {
  69. return $file . '?c=' . $cacheTime;
  70. } else {
  71. return $file . "?c=0";
  72. }
  73. }
  74. public function lcTransAdminField($fieldName, $entityClass)
  75. {
  76. return $this->translatorAdmin->transField($fieldName, $entityClass) ;
  77. }
  78. public function lcTransAdminHelp($fieldName, $entityClass)
  79. {
  80. return $this->translatorAdmin->transHelp($fieldName, $entityClass) ;
  81. }
  82. public function lcTransAdminPanel($panelName, $entityClass)
  83. {
  84. return $this->translatorAdmin->transPanel($panelName, $entityClass) ;
  85. }
  86. public function lcTransAdminMenu($menuName)
  87. {
  88. return $this->translatorAdmin->transMenu($menuName);;
  89. }
  90. public function lcTransAdminAction($actionName)
  91. {
  92. return $this->translatorAdmin->transAction($actionName);;
  93. }
  94. public function lcTransAdminTitle($pageName, $entityClass = null, $params = [])
  95. {
  96. return $this->translatorAdmin->transTitle($pageName, $entityClass, $params) ;
  97. }
  98. public function lcLiip($path, $thumb = 'tile', $default = 'default.jpg')
  99. {
  100. if (substr($path, 0, 1) === '/') {
  101. $path = substr($path, 1);
  102. }
  103. if ($path) {
  104. $fileManagerFolder = substr($this->getFileManagerFolder(), 1);
  105. if (strpos($path, $fileManagerFolder) === false) {
  106. $path = $fileManagerFolder . '/' . $path;
  107. }
  108. if (file_exists($path)) {
  109. return $this->cacheManager->getBrowserPath($path, $thumb);
  110. }
  111. }
  112. return $this->cacheManager->getBrowserPath($this->getFileManagerFolder() . '/' . $default, $thumb);
  113. }
  114. function getTranslatedUrls()
  115. {
  116. $ret = array();
  117. $langs = $this->parameterBag->get('app.locales');
  118. $currentRoute = $this->requestStack->getCurrentRequest()->get('_route');
  119. $params = array_merge((array)$this->requestStack->getCurrentRequest()->get('_route_params'), $_GET);
  120. if ($currentRoute) {
  121. foreach ($langs as $lg) {
  122. $ret[$lg] = $this->router->generate($currentRoute, array_merge($params, array('_locale' => $lg)));
  123. }
  124. }
  125. return $ret;
  126. }
  127. public function getFileManagerFolder()
  128. {
  129. return $this->parameterBag->get('app.path_uploads');
  130. }
  131. public function homepageRoute()
  132. {
  133. return $this->parameterBag->get('lc_sov.homepage_route');
  134. }
  135. }