<?php
namespace Aviatur\TwigBundle\Twig\Extension;
use Aviatur\GeneralBundle\Services\AviaturChangeCoin;
use Aviatur\GeneralBundle\Services\AviaturErrorHandler;
use Aviatur\TwigBundle\Services\TwigFolder;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\RouterInterface;
class FileExtension extends \Twig_Extension {
private \Symfony\Component\HttpFoundation\RequestStack $requestStack;
private \Symfony\Component\HttpFoundation\Session\SessionInterface $session;
private \Symfony\Component\Routing\RouterInterface $router;
private \Aviatur\GeneralBundle\Services\AviaturChangeCoin $aviaturChangeCoin;
private \Aviatur\GeneralBundle\Services\AviaturErrorHandler $errorHandler;
private \Aviatur\TwigBundle\Services\TwigFolder $twigFolder;
private $env;
public function __construct(RequestStack $requestStack, SessionInterface $session, RouterInterface $router, AviaturChangeCoin $aviaturChangeCoin, AviaturErrorHandler $errorHandler, TwigFolder $twigFolder, $env) {
$this->requestStack = $requestStack;
$this->session = $session;
$this->router = $router;
$this->aviaturChangeCoin = $aviaturChangeCoin;
$this->errorHandler = $errorHandler;
$this->twigFolder = $twigFolder;
$this->env = $env;
}
/**
* Return the functions registered as twig extensions
*
* @return array
*/
public function getFunctions() {
return [
new \Twig_SimpleFunction('twig_exists', [
$this,
'twigExists'
]),
new \Twig_SimpleFunction('path_with_locale', [
$this,
'pathWithLocale'
]),
new \Twig_SimpleFunction('file_exists', [
$this,
'file_exists'
]),
new \Twig_SimpleFunction('image_exists', [
$this,
'image_exists'
]),
new \Twig_SimpleFunction('change_coin', [
$this,
'changeCoin'
]),
new \Twig_SimpleFunction('change_currency', [
$this,
'changeCurrency'
])
];
}
public function twigExists($customTwig, $defaultTwig = null) {
return $this->twigFolder->twigExists($customTwig, $defaultTwig);
}
public function pathWithLocale($path, $pathArray = [], $newLocale = null) {
// @todo locales routes disabled
return $this->router->generate($path, $pathArray);
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);
}
}
public function getName() {
return 'app_file';
}
public function changeCoin($currencyTo, $coin, $qse = null) {
$feeConvert = null;
if ($this->session->get('typeCoin') != '' && $this->session->get('typeCoin') != 'COP') {
$initialCoin = $coin;
$coin = $this->aviaturChangeCoin->InfoCoin($currencyTo, $coin);
if ($qse != null) {
$financialValue = $this->session->has('financialValue') ? $this->session->get('financialValue') : 1;
$trmValue = $this->session->has('trmValue') ? $this->session->get('trmValue') : 1;
$feeUSD = ($initialCoin / $financialValue) - ($initialCoin / $trmValue);
$feeConvert = $feeUSD * ($this->session->has('RateChange') ? $this->session->get('RateChange') : 1);
}
if ($qse == 1) {
//obtain value + qse
$coin = $coin + (($this->session->get('RateDiff') > 0) ? $feeConvert : 0);
} elseif ($qse == 2) {
//Obtain only qse value
$coin = ($this->session->get('RateDiff') > 0) ? $feeConvert : 0;
} elseif ($qse == 3) {
//obtain value + qse in COP
$feeTo = $coin - (($this->session->get('RateDiff') > 0) ? $feeConvert : $coin);
$feeCop = ($feeTo / $this->session->get('RateChange')) * $this->session->get('trmValue');
$coin = $initialCoin + $feeCop;
} elseif ($qse == 4) {
//obtain only qse in COP
$feeTo = ($this->session->get('RateDiff') > 0) ? $feeConvert : 0;
$coin = ($feeTo / $this->session->get('RateChange')) * $this->session->get('trmValue');
}
}
return (float) $coin;
//return number_format((float)$coin, ($this->session->get('typeCoin') == 'COP') ? 0 : 2, ',', '.');
}
public function changeCurrency($currency) {
if ($this->session->get('typeCoin') != '' && $this->session->get('typeCoin') != 'COP') {
$currency = $this->session->get('typeCoin');
}
return (string) $currency;
}
public function file_exists($filename) {
if ($this->env === 'dev') {
stream_context_set_default([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
],
]);
}
if (file_exists($filename)) {
$fopen = @fopen($filename, 'r');
} elseif (substr($filename, 0, 4) === "http") {
$img_exits = @get_headers($filename);
$fopen = strpos($img_exits[0], 'OK');
} else {
return false;
}
$canOpen = (bool) $fopen;
@fclose($fopen);
return \file_exists($filename) || $canOpen;
}
public function image_exists($filename) {
return file_exists($filename) ? true : false;
}
}