1 changed files with 79 additions and 0 deletions
@ -0,0 +1,79 @@ |
|||||
|
<?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]); |
||||
|
|
||||
|
if (session_status() == PHP_SESSION_NONE) { |
||||
|
session_start(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
$this->twig->addGlobal('_SESSION_', $_SESSION); |
||||
|
|
||||
|
$this->twig->addGlobal('_ALGOLIA_INDEX_NAME_', Configuration::GetConfig("Algolia", "IndexName")); |
||||
|
$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->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()); |
||||
|
} |
||||
|
|
||||
|
{ |
||||
|
$twigRenderer = new TwigWrapper(); |
||||
|
$twigRenderer->RenderTwig($twigRenderer->GuessTargetTwigFile(), $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; |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue