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

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.         // TODO ISSUE WITH THE METHOD
  39.         $agency $this->getAgency();
  40.         //dd($this->session->has('operatorId'), $agency);
  41.         if ($agency['agencyTwigFlux']) {
  42.             $folder $agency['agencyAssetsFolder'] . "/Flux";
  43.             return $folder;
  44.         } elseif($this->session->has('operatorId')) {
  45.             // Returns the custom default twig if exists
  46.             // return "aviaturb2t/Flux";
  47.             return "aviatur/Flux";
  48.         } else {
  49.             return "default/Flux";
  50.         }
  51.     }
  52.     public function assetStyle() {
  53.         $agency $this->getAgency();
  54.         $folder $agency['agencyAssetsFolder'];
  55.         $twigFlux $agency['agencyTwigFlux'];
  56.         return $folder "_assets";
  57.     }
  58.     public function assetFlux() {
  59.         $agency $this->getAgency();
  60.         if ($agency['agencyTwigFlux']) {
  61.             $folder $agency['agencyAssetsFolder'];
  62.             return $folder "_assets";
  63.         } else {
  64.             return "default_assets";
  65.         }
  66.     }
  67.     public function getAgency() {
  68.         // TODO SG GET THE AGENCY ALWAYS IN AVIATUR
  69.         $session $this->session;
  70.         if (!$session->has('agencyTwigFlux')) {
  71.             $em $this->managerRegistry->getManager();
  72.             if ($session->has('agencyId')) {
  73.                 $agency $em->getRepository(\Aviatur\AgencyBundle\Entity\Agency::class)->find($session->get('agencyId'));
  74.             } else {
  75.                 $protocol 'domain';
  76.                 if ($this->request !== null) {
  77.                     $domain $this->request->getHost();
  78.                     if ($this->request->isSecure()) {
  79.                         $protocol 'domainsecure';
  80.                     }
  81.                 } else {
  82.                     $domain 'www.aviatur.com';
  83.                     if ($this->env === 'dev'){
  84.                         $domain 'aviatursym.com';
  85.                     }
  86.                 }
  87.                 $agency $em->getRepository(\Aviatur\AgencyBundle\Entity\Agency::class)->findOneBy(
  88.                         [$protocol => [$domainstr_replace($this->wwwPrefix''$domain)]]
  89.                 );
  90.                 // solucion momentanea aval
  91.                 if(!isset($agency)) {
  92.                     $domain 'www.viajestuplus.com.co';
  93.                     $agency $em->getRepository(\Aviatur\AgencyBundle\Entity\Agency::class)->findOneBy(
  94.                         [$protocol => [$domainstr_replace($this->wwwPrefix''$domain)]]);
  95.                 }
  96.             }
  97.             $session->set('agencyTwigFlux'$agency->getTwigFlux());
  98.             $session->set('agencyAssetsFolder'$agency->getAssetsFolder());
  99.         }
  100.         return ['agencyTwigFlux' => $session->get('agencyTwigFlux'), 'agencyAssetsFolder' => $session->get('agencyAssetsFolder')];
  101.     }
  102.     public function absoluteBaseUrl() {
  103.         $protocol 'domain';
  104.         if ($this->request !== null) {
  105.             $domain $this->request->getHost();
  106.             if ($this->request->isSecure()) {
  107.                 $protocol 'domainsecure';
  108.             }
  109.         } elseif ($this->session->has('domain')) {
  110.             $domain $this->session->get('domain');
  111.         } else {
  112.             $domain 'aviatur.com';
  113.             if ($this->env === 'dev'){
  114.                 $domain 'aviatursym.com';
  115.             }
  116.         }
  117.         if($protocol == 'domainsecure') {
  118.             return 'https://' $domain;
  119.         }
  120.         return 'https://' $domain;
  121.     }
  122.     public function absoluteAssetsUrl($option null) {
  123.         if ($option == 'common') {
  124.             return $this->absoluteBaseUrl() . '/version/' $this->assetsPackages->getVersion($this->absoluteBaseUrl()) . '/assets/common_assets/';
  125.         }
  126.         return $this->absoluteBaseUrl() . '/version/' $this->assetsPackages->getVersion($this->absoluteBaseUrl()) . '/assets/' $this->assetStyle() . '/';
  127.     }
  128.     public function twigExists($customTwig$customDefaultTwig null) {
  129.         [$bundle$agency$path] = array_pad(explode('/'$customTwig3), 3null);
  130.         $aviaturTwig implode('/' , [$bundle"aviatur"$path]);
  131.         if ($bundle != "@AviaturTwig") {
  132.             return $customTwig;
  133.         }
  134.         if ($this->filesystemLoader->exists($customTwig)) {
  135.             // Returns the agency twig if it exists
  136.             return $customTwig;
  137.         } elseif($this->session->has('operatorId') && $this->filesystemLoader->exists($aviaturTwig)){
  138.             // $this->filesystemLoader->exists($aviaturTwig)
  139.             // Returns FRONT Agency inherited from default
  140.             return $aviaturTwig;
  141.         } elseif ($customDefaultTwig != null && $this->filesystemLoader->exists($customDefaultTwig)) {
  142.             // Returns the custom default twig if exists
  143.             return $customDefaultTwig;
  144.         } else {
  145.             // Builds the default twig path and validates if exists
  146.             $defaultTwig implode('/' , [$bundle"default"$path]);
  147.             if ($this->filesystemLoader->exists($defaultTwig)) {
  148.                 return $defaultTwig;
  149.             }
  150.         }
  151.         return $customTwig;
  152.     }
  153.     public function pathWithLocale($path$pathArray = [], $newLocale null) {
  154.         if ($newLocale == null) {
  155.             $request $this->requestStack->getCurrentRequest();
  156.             $locale $request->getLocale();
  157.         } else {
  158.             $locale $newLocale;
  159.         }
  160.         if ($locale == 'en' || $locale == 'fr') {
  161.             $pathArray['_locale'] = $locale;
  162.             return $this->router->generate($path '_locale'$pathArray);
  163.         } else {
  164.             return $this->router->generate($path$pathArray);
  165.         }
  166.     }
  167. }