<?php
namespace Aviatur\TwigBundle\Services;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Asset\Packages;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\RouterInterface;
use Twig\Loader\FilesystemLoader;
class TwigFolder {
protected $request;
private \Symfony\Component\HttpFoundation\RequestStack $requestStack;
private \Symfony\Component\HttpFoundation\Session\SessionInterface $session;
private \Symfony\Component\Routing\RouterInterface $router;
private \Doctrine\Persistence\ManagerRegistry $managerRegistry;
private \Symfony\Component\Asset\Packages $assetsPackages;
private \Twig\Loader\FilesystemLoader $filesystemLoader;
private $env;
private string $wwwPrefix = 'www.';
public function __construct(RequestStack $requestStack, SessionInterface $session, RouterInterface $router, ManagerRegistry $managerRegistry, Packages $assetsPackages, FilesystemLoader $filesystemLoader, $env) {
$this->requestStack = $requestStack;
$this->session = $session;
$this->router = $router;
$this->managerRegistry = $managerRegistry;
$this->assetsPackages = $assetsPackages;
$this->filesystemLoader = $filesystemLoader;
$this->env = $env;
}
public function setRequest(RequestStack $request_stack) {
$this->request = $request_stack->getCurrentRequest();
}
public function twigStyle() {
$agency = $this->getAgency();
$folder = $agency['agencyAssetsFolder'];
$twigFlux = $agency['agencyTwigFlux'];
return $folder . "/Custom";
}
public function twigFlux() {
$agency = $this->getAgency();
if ($agency['agencyTwigFlux']) {
$folder = $agency['agencyAssetsFolder'] . "/Flux";
return $folder;
} else {
return "default/Flux";
}
}
public function assetStyle() {
$agency = $this->getAgency();
$folder = $agency['agencyAssetsFolder'];
$twigFlux = $agency['agencyTwigFlux'];
return $folder . "_assets";
}
public function assetFlux() {
$agency = $this->getAgency();
if ($agency['agencyTwigFlux']) {
$folder = $agency['agencyAssetsFolder'];
return $folder . "_assets";
} else {
return "default_assets";
}
}
public function getAgency() {
$session = $this->session;
if (!$session->has('agencyTwigFlux')) {
$em = $this->managerRegistry->getManager();
if ($session->has('agencyId')) {
$agency = $em->getRepository(\Aviatur\AgencyBundle\Entity\Agency::class)->find($session->get('agencyId'));
} else {
$protocol = 'domain';
if ($this->request !== null) {
$domain = $this->request->getHost();
if ($this->request->isSecure()) {
$protocol = 'domainsecure';
}
} else {
$domain = 'www.aviatur.com';
if ($this->env === 'dev'){
$domain = 'aviatursym.com';
}
}
$agency = $em->getRepository(\Aviatur\AgencyBundle\Entity\Agency::class)->findOneBy(
[$protocol => [$domain, str_replace($this->wwwPrefix, '', $domain)]]
);
// solucion momentanea aval
if(!isset($agency)) {
$domain = 'www.viajestuplus.com.co';
$agency = $em->getRepository(\Aviatur\AgencyBundle\Entity\Agency::class)->findOneBy(
[$protocol => [$domain, str_replace($this->wwwPrefix, '', $domain)]]);
}
}
$session->set('agencyTwigFlux', $agency->getTwigFlux());
$session->set('agencyAssetsFolder', $agency->getAssetsFolder());
}
return ['agencyTwigFlux' => $session->get('agencyTwigFlux'), 'agencyAssetsFolder' => $session->get('agencyAssetsFolder')];
}
public function absoluteBaseUrl() {
$protocol = 'domain';
if ($this->request !== null) {
$domain = $this->request->getHost();
if ($this->request->isSecure()) {
$protocol = 'domainsecure';
}
} elseif ($this->session->has('domain')) {
$domain = $this->session->get('domain');
} else {
$domain = 'aviatur.com';
if ($this->env === 'dev'){
$domain = 'aviatursym.com';
}
}
if($protocol == 'domainsecure') {
return 'https://' . $domain;
}
return 'https://' . $domain;
}
public function absoluteAssetsUrl($option = null) {
if ($option == 'common') {
return $this->absoluteBaseUrl() . '/version/' . $this->assetsPackages->getVersion($this->absoluteBaseUrl()) . '/assets/common_assets/';
}
return $this->absoluteBaseUrl() . '/version/' . $this->assetsPackages->getVersion($this->absoluteBaseUrl()) . '/assets/' . $this->assetStyle() . '/';
}
public function twigExists($customTwig, $defaultTwig = null) {
if ($this->filesystemLoader->exists($customTwig)) {
return $customTwig;
} elseif ($defaultTwig != null && $this->filesystemLoader->exists($defaultTwig)) {
return $defaultTwig;
} else {
$explodedTwig = explode('/', $customTwig);
if (isset($explodedTwig[1])) {
$explodedTwig[1] = 'default';
}
$implodedTwig = implode('/', $explodedTwig);
if ($this->filesystemLoader->exists($implodedTwig)) {
return $implodedTwig;
}
}
}
public function pathWithLocale($path, $pathArray = [], $newLocale = null) {
if ($newLocale == null) {
$request = $this->requestStack->getCurrentRequest();
$locale = $request->getLocale();
} else {
$locale = $newLocale;
}
if ($locale == 'en' || $locale == 'fr') {
$pathArray['_locale'] = $locale;
return $this->router->generate($path . '_locale', $pathArray);
} else {
return $this->router->generate($path, $pathArray);
}
}
}