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.

TwigExtension.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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('homepage_route', [$this, 'homepageRoute']),
  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 homepageRoute(){
  85. return $this->parameterBag->get('lc_sov.homepage_route');
  86. }
  87. }