src/Aviatur/TwigBundle/Services/TwigFolder.php line 107

Open in your IDE?
  1. <?php
  2. namespace Aviatur\TwigBundle\Services;
  3. use Doctrine\Persistence\ManagerRegistry;
  4. use Symfony\Component\Asset\Packages;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  7. use Symfony\Component\Routing\RouterInterface;
  8. use Twig\Loader\FilesystemLoader;
  9. class TwigFolder {
  10.     protected $request;
  11.     private \Symfony\Component\HttpFoundation\RequestStack $requestStack;
  12.     private \Symfony\Component\HttpFoundation\Session\SessionInterface $session;
  13.     private \Symfony\Component\Routing\RouterInterface $router;
  14.     private \Doctrine\Persistence\ManagerRegistry $managerRegistry;
  15.     private \Symfony\Component\Asset\Packages $assetsPackages;
  16.     private \Twig\Loader\FilesystemLoader $filesystemLoader;
  17.     private $env;
  18.     private string $wwwPrefix 'www.';
  19.     public function __construct(RequestStack $requestStackSessionInterface $sessionRouterInterface $routerManagerRegistry $managerRegistryPackages $assetsPackagesFilesystemLoader $filesystemLoader$env) {
  20.         $this->requestStack $requestStack;
  21.         $this->session $session;
  22.         $this->router $router;
  23.         $this->managerRegistry $managerRegistry;
  24.         $this->assetsPackages $assetsPackages;
  25.         $this->filesystemLoader $filesystemLoader;
  26.         $this->env $env;
  27.     }
  28.     public function setRequest(RequestStack $request_stack) {
  29.         $this->request $request_stack->getCurrentRequest();
  30.     }
  31.     public function twigStyle() {
  32.         $agency $this->getAgency();
  33.         $folder $agency['agencyAssetsFolder'];
  34.         $twigFlux $agency['agencyTwigFlux'];
  35.         return $folder "/Custom";
  36.     }
  37.     public function twigFlux() {
  38.         $agency $this->getAgency();
  39.         if ($agency['agencyTwigFlux']) {
  40.             $folder $agency['agencyAssetsFolder'] . "/Flux";
  41.             return $folder;
  42.         } else {
  43.             return "default/Flux";
  44.         }
  45.     }
  46.     public function assetStyle() {
  47.         $agency $this->getAgency();
  48.         $folder $agency['agencyAssetsFolder'];
  49.         $twigFlux $agency['agencyTwigFlux'];
  50.         return $folder "_assets";
  51.     }
  52.     public function assetFlux() {
  53.         $agency $this->getAgency();
  54.         if ($agency['agencyTwigFlux']) {
  55.             $folder $agency['agencyAssetsFolder'];
  56.             return $folder "_assets";
  57.         } else {
  58.             return "default_assets";
  59.         }
  60.     }
  61.     public function getAgency() {
  62.         $session $this->session;
  63.         if (!$session->has('agencyTwigFlux')) {
  64.             $em $this->managerRegistry->getManager();
  65.             if ($session->has('agencyId')) {
  66.                 $agency $em->getRepository(\Aviatur\AgencyBundle\Entity\Agency::class)->find($session->get('agencyId'));
  67.             } else {
  68.                 $protocol 'domain';
  69.                 if ($this->request !== null) {
  70.                     $domain $this->request->getHost();
  71.                     if ($this->request->isSecure()) {
  72.                         $protocol 'domainsecure';
  73.                     }
  74.                 } else {
  75.                     $domain 'www.aviatur.com';
  76.                     if ($this->env === 'dev'){
  77.                         $domain 'aviatursym.com';
  78.                     }
  79.                 }
  80.                 $agency $em->getRepository(\Aviatur\AgencyBundle\Entity\Agency::class)->findOneBy(
  81.                         [$protocol => [$domainstr_replace($this->wwwPrefix''$domain)]]
  82.                 );
  83.                 // solucion momentanea aval
  84.                 if(!isset($agency)) {
  85.                     $domain 'www.viajestuplus.com.co';
  86.                     $agency $em->getRepository(\Aviatur\AgencyBundle\Entity\Agency::class)->findOneBy(
  87.                         [$protocol => [$domainstr_replace($this->wwwPrefix''$domain)]]);
  88.                 }
  89.             }
  90.             $session->set('agencyTwigFlux'$agency->getTwigFlux());
  91.             $session->set('agencyAssetsFolder'$agency->getAssetsFolder());
  92.         }
  93.         return ['agencyTwigFlux' => $session->get('agencyTwigFlux'), 'agencyAssetsFolder' => $session->get('agencyAssetsFolder')];
  94.     }
  95.     public function absoluteBaseUrl() {
  96.         $protocol 'domain';
  97.         if ($this->request !== null) {
  98.             $domain $this->request->getHost();
  99.             if ($this->request->isSecure()) {
  100.                 $protocol 'domainsecure';
  101.             }
  102.         } elseif ($this->session->has('domain')) {
  103.             $domain $this->session->get('domain');
  104.         } else {
  105.             $domain 'aviatur.com';
  106.             if ($this->env === 'dev'){
  107.                 $domain 'aviatursym.com';
  108.             }
  109.         }
  110.         if($protocol == 'domainsecure') {
  111.             return 'https://' $domain;
  112.         }
  113.         return 'https://' $domain;
  114.     }
  115.     public function absoluteAssetsUrl($option null) {
  116.         if ($option == 'common') {
  117.             return $this->absoluteBaseUrl() . '/version/' $this->assetsPackages->getVersion($this->absoluteBaseUrl()) . '/assets/common_assets/';
  118.         }
  119.         return $this->absoluteBaseUrl() . '/version/' $this->assetsPackages->getVersion($this->absoluteBaseUrl()) . '/assets/' $this->assetStyle() . '/';
  120.     }
  121.     public function twigExists($customTwig$defaultTwig null) {
  122.         if ($this->filesystemLoader->exists($customTwig)) {
  123.             return $customTwig;
  124.         } elseif ($defaultTwig != null && $this->filesystemLoader->exists($defaultTwig)) {
  125.             return $defaultTwig;
  126.         } else {
  127.             $explodedTwig explode('/'$customTwig);
  128.             if (isset($explodedTwig[1])) {
  129.                 $explodedTwig[1] = 'default';
  130.             }
  131.             $implodedTwig implode('/'$explodedTwig);
  132.             if ($this->filesystemLoader->exists($implodedTwig)) {
  133.                 return $implodedTwig;
  134.             }
  135.         }
  136.     }
  137.     public function pathWithLocale($path$pathArray = [], $newLocale null) {
  138.         if ($newLocale == null) {
  139.             $request $this->requestStack->getCurrentRequest();
  140.             $locale $request->getLocale();
  141.         } else {
  142.             $locale $newLocale;
  143.         }
  144.         if ($locale == 'en' || $locale == 'fr') {
  145.             $pathArray['_locale'] = $locale;
  146.             return $this->router->generate($path '_locale'$pathArray);
  147.         } else {
  148.             return $this->router->generate($path$pathArray);
  149.         }
  150.     }
  151. }