You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.8 KiB
83 lines
2.8 KiB
<?php
|
|
|
|
namespace App\Wrappers;
|
|
|
|
use App\Common\UUID;
|
|
use App\Configuration;
|
|
use App\Enumerators\SessionElement;
|
|
use App\Security;
|
|
use App\TwigExtensions\FiltersExtension;
|
|
use App\TwigExtensions\FunctionExtensions;
|
|
use App\TwigExtensions\NavigationExtension;
|
|
use App\TwigExtensions\StringManipulationExtension;
|
|
use App\TwigExtensions\TextLinkExtension;
|
|
use App\TwigTests\TwigTests;
|
|
use Gravatar\Gravatar;
|
|
use JetBrains\PhpStorm\NoReturn;
|
|
use Twig\Environment;
|
|
use Twig\Loader\FilesystemLoader;
|
|
|
|
|
|
class TwigWrapper
|
|
{
|
|
public FilesystemLoader $loader;
|
|
public Environment $twig;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->loader = new FilesystemLoader(dirname($_SERVER["DOCUMENT_ROOT"]) . "/Templates/");
|
|
$this->twig = new Environment($this->loader, ["debug" => true]);
|
|
|
|
SessionWrapper::Start();
|
|
|
|
$this->twig->addGlobal('_SESSION_', $_SESSION);
|
|
|
|
$this->twig->addGlobal('_ALGOLIA_INDEXES_', [
|
|
"Tunes" => Configuration::GetConfig("Algolia", "Indexes", "Tunes"),
|
|
"Dances" => Configuration::GetConfig("Algolia", "Indexes", "Dances"),
|
|
]);
|
|
$this->twig->addGlobal('_ALGOLIA_APP_ID_', Configuration::GetConfig("Algolia", "AppID"));
|
|
$this->twig->addGlobal('_ALGOLIA_SEARCH_ONLY_API_KEY_', Configuration::GetConfig("Algolia", "APIKeys", "SearchOnly"));
|
|
|
|
$this->twig->addGlobal('_CURRENT_PAGE_', strtok($_SERVER['REQUEST_URI'], '?'));
|
|
$this->twig->addGlobal('_CURRENT_URI_', urlencode("http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"));
|
|
$this->twig->addGlobal('_USER_TIMEZONE_OFFSET_', 60);
|
|
|
|
$this->twig->addGlobal('_CAPTCHA_KEY_ID_', Configuration::GetConfig('reCAPTCHA', 'KeyID'));
|
|
|
|
|
|
$this->twig->addExtension(new FiltersExtension());
|
|
$this->twig->addExtension(new FunctionExtensions($this->twig));
|
|
$this->twig->addExtension(new NavigationExtension());
|
|
$this->twig->addExtension(new StringManipulationExtension());
|
|
|
|
$this->twig->addExtension(new TwigTests());
|
|
}
|
|
|
|
public static function AutoRenderTwig(array $arguments)
|
|
{
|
|
$twigRenderer = new TwigWrapper();
|
|
$twigRenderer->RenderTwig($twigRenderer->GuessTargetTwigFile(), $arguments);
|
|
}
|
|
|
|
public static function RenderTwig(string $target, array $arguments)
|
|
{
|
|
$twigRenderer = new TwigWrapper();
|
|
echo $twigRenderer->twig->render($target, $arguments);
|
|
die();
|
|
}
|
|
|
|
private function GuessTargetTwigFile() : string
|
|
{
|
|
$twigFile = str_replace(search: ".php",replace: ".html.twig", subject: $_SERVER['REQUEST_URI']);
|
|
if($twigFile == "/")
|
|
$twigFile = "/index.html.twig";
|
|
|
|
$pageTemplateDirectory = __DIR__ . "/../../Templates/Pages";
|
|
|
|
if(!file_exists($pageTemplateDirectory . $twigFile))
|
|
return "ErrorPages/404.html.twig";
|
|
|
|
return "Pages" . $twigFile;
|
|
}
|
|
}
|
|
|