src/Aviatur/TwigBundle/Twig/Extension/FileExtension.php line 148

Open in your IDE?
  1. <?php
  2. namespace Aviatur\TwigBundle\Twig\Extension;
  3. use Aviatur\GeneralBundle\Services\AviaturChangeCoin;
  4. use Aviatur\GeneralBundle\Services\AviaturErrorHandler;
  5. use Aviatur\TwigBundle\Services\TwigFolder;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  8. use Symfony\Component\Routing\RouterInterface;
  9. class FileExtension extends \Twig_Extension {
  10.     private \Symfony\Component\HttpFoundation\RequestStack $requestStack;
  11.     private \Symfony\Component\HttpFoundation\Session\SessionInterface $session;
  12.     private \Symfony\Component\Routing\RouterInterface $router;
  13.     private \Aviatur\GeneralBundle\Services\AviaturChangeCoin $aviaturChangeCoin;
  14.     private \Aviatur\GeneralBundle\Services\AviaturErrorHandler $errorHandler;
  15.     private \Aviatur\TwigBundle\Services\TwigFolder $twigFolder;
  16.     private $env;
  17.     public function __construct(RequestStack $requestStackSessionInterface $sessionRouterInterface $routerAviaturChangeCoin $aviaturChangeCoinAviaturErrorHandler $errorHandlerTwigFolder $twigFolder$env) {
  18.         $this->requestStack $requestStack;
  19.         $this->session $session;
  20.         $this->router $router;
  21.         $this->aviaturChangeCoin $aviaturChangeCoin;
  22.         $this->errorHandler $errorHandler;
  23.         $this->twigFolder $twigFolder;
  24.         $this->env $env;
  25.     }
  26.     /**
  27.      * Return the functions registered as twig extensions
  28.      *
  29.      * @return array
  30.      */
  31.     public function getFunctions() {
  32.         return [
  33.             new \Twig_SimpleFunction('twig_exists', [
  34.                 $this,
  35.                 'twigExists'
  36.             ]),
  37.             new \Twig_SimpleFunction('path_with_locale', [
  38.                 $this,
  39.                 'pathWithLocale'
  40.             ]),
  41.             new \Twig_SimpleFunction('file_exists', [
  42.                 $this,
  43.                 'file_exists'
  44.             ]),
  45.             new \Twig_SimpleFunction('image_exists', [
  46.                 $this,
  47.                 'image_exists'
  48.             ]),
  49.             new \Twig_SimpleFunction('change_coin', [
  50.                 $this,
  51.                 'changeCoin'
  52.             ]),
  53.             new \Twig_SimpleFunction('change_currency', [
  54.                 $this,
  55.                 'changeCurrency'
  56.             ])
  57.         ];
  58.     }
  59.     public function twigExists($customTwig$defaultTwig null) {
  60.         return $this->twigFolder->twigExists($customTwig$defaultTwig);
  61.     }
  62.     public function pathWithLocale($path$pathArray = [], $newLocale null) {
  63.         // @todo locales routes disabled
  64.         return $this->router->generate($path$pathArray);
  65.         if ($newLocale == null) {
  66.             $request $this->requestStack->getCurrentRequest();
  67.             $locale $request->getLocale();
  68.         } else {
  69.             $locale $newLocale;
  70.         }
  71.         if ($locale == 'en' || $locale == 'fr') {
  72.             $pathArray['_locale'] = $locale;
  73.             return $this->router->generate($path '_locale'$pathArray);
  74.         } else {
  75.             return $this->router->generate($path$pathArray);
  76.         }
  77.     }
  78.     public function getName() {
  79.         return 'app_file';
  80.     }
  81.     public function changeCoin($currencyTo$coin$qse null) {
  82.         $feeConvert null;
  83.         if ($this->session->get('typeCoin') != '' && $this->session->get('typeCoin') != 'COP') {
  84.             $initialCoin $coin;
  85.             $coin $this->aviaturChangeCoin->InfoCoin($currencyTo$coin);
  86.             if ($qse != null) {
  87.                 $financialValue $this->session->has('financialValue') ? $this->session->get('financialValue') : 1;
  88.                 $trmValue $this->session->has('trmValue') ? $this->session->get('trmValue') : 1;
  89.                 $feeUSD = ($initialCoin $financialValue) - ($initialCoin $trmValue);
  90.                 $feeConvert $feeUSD * ($this->session->has('RateChange') ? $this->session->get('RateChange') : 1);
  91.             }
  92.             if ($qse == 1) {
  93.                 //obtain value + qse
  94.                 $coin $coin + (($this->session->get('RateDiff') > 0) ? $feeConvert 0);
  95.             } elseif ($qse == 2) {
  96.                 //Obtain only qse value
  97.                 $coin = ($this->session->get('RateDiff') > 0) ? $feeConvert 0;
  98.             } elseif ($qse == 3) {
  99.                 //obtain value + qse in COP
  100.                 $feeTo $coin - (($this->session->get('RateDiff') > 0) ? $feeConvert $coin);
  101.                 $feeCop = ($feeTo $this->session->get('RateChange')) * $this->session->get('trmValue');
  102.                 $coin $initialCoin $feeCop;
  103.             } elseif ($qse == 4) {
  104.                 //obtain only qse in COP
  105.                 $feeTo = ($this->session->get('RateDiff') > 0) ? $feeConvert 0;
  106.                 $coin = ($feeTo $this->session->get('RateChange')) * $this->session->get('trmValue');
  107.             }
  108.         }
  109.         return (float) $coin;
  110.         //return number_format((float)$coin, ($this->session->get('typeCoin') == 'COP') ? 0 : 2, ',', '.');
  111.     }
  112.     public function changeCurrency($currency) {
  113.         if ($this->session->get('typeCoin') != '' && $this->session->get('typeCoin') != 'COP') {
  114.             $currency $this->session->get('typeCoin');
  115.         }
  116.         return (string) $currency;
  117.     }
  118.     public function file_exists($filename) {
  119.         if ($this->env === 'dev') {
  120.             stream_context_set_default([
  121.                 'ssl' => [
  122.                     'verify_peer' => false,
  123.                     'verify_peer_name' => false,
  124.                 ],
  125.             ]);
  126.         }
  127.         if (file_exists($filename)) {
  128.             $fopen = @fopen($filename'r');
  129.         } elseif (substr($filename04) === "http") {
  130.             $img_exits = @get_headers($filename);
  131.             $fopen strpos($img_exits[0], 'OK');
  132.         } else {
  133.             return false;
  134.         }
  135.         $canOpen = (bool) $fopen;
  136.         @fclose($fopen);
  137.         return \file_exists($filename) || $canOpen;
  138.     }
  139.     public function image_exists($filename) {
  140.         return file_exists($filename) ? true false;
  141.     }
  142. }