webcron/lib/Framework/Twig.php

114 lines
3.6 KiB
PHP
Raw Normal View History

2021-04-08 13:19:33 +02:00
<?php
namespace JeroenED\Framework;
2021-05-26 13:09:13 +02:00
use Mehrkanal\EncoreTwigExtension\Extensions\EntryFilesTwigExtension;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookup;
2021-08-02 13:28:14 +02:00
use Twig\Cache\FilesystemCache;
2021-04-08 13:19:33 +02:00
use Twig\Environment;
2021-05-28 14:24:33 +02:00
use Twig\Extra\Intl\IntlExtension;
2021-04-08 13:19:33 +02:00
use Twig\Loader\FilesystemLoader;
2021-04-08 13:28:13 +02:00
use Twig\TwigFilter;
2021-04-08 13:19:33 +02:00
use Twig\TwigFunction;
class Twig
{
private Environment $environment;
private Kernel $kernel;
2021-08-02 13:28:14 +02:00
2021-04-08 13:19:33 +02:00
public function __construct(Kernel $kernel)
{
$loader = new FilesystemLoader([$kernel->getTemplateDir()]);
$this->environment = new Environment($loader);
2021-11-25 15:20:38 +01:00
if($_ENV['DEBUG'] != 'true') {
$cache = new FilesystemCache($kernel->getCacheDir() . '/twig');
$this->environment->setCache($cache);
}
2021-04-08 13:19:33 +02:00
$this->kernel = $kernel;
2021-05-28 14:24:33 +02:00
$this->addExtensions();
2021-04-08 13:19:33 +02:00
$this->addFunctions();
2021-04-08 13:28:13 +02:00
$this->addFilters();
2021-04-08 13:19:33 +02:00
}
public function render(string $template, array $vars = []): string
{
return $this->environment->render($template, $vars);
}
2021-05-28 14:24:33 +02:00
public function addExtensions()
{
$this->environment->addExtension(new IntlExtension());
$this->environment->addExtension(new EntryFilesTwigExtension(new EntrypointLookup('./public/build/entrypoints.json')));
}
2021-04-08 13:19:33 +02:00
public function addFunctions()
{
2021-04-08 16:34:25 +02:00
$path = new TwigFunction('path', function(string $route, array $params = []) {
2021-04-08 14:54:30 +02:00
return $this->kernel->getRouter()->getUrlForRoute($route, $params);
2021-04-08 13:19:33 +02:00
});
$this->environment->addFunction($path);
2021-04-08 13:28:13 +02:00
}
public function addFilters() {
2022-01-26 15:34:32 +01:00
$secondsToInterval = new TwigFilter('interval', function(int|float $time) {
$return = '';
2021-04-08 13:28:13 +02:00
$days = floor($time / (60 * 60 * 24));
$time -= $days * (60 * 60 * 24);
2022-01-27 10:39:18 +01:00
$return .= ($days != 0 || !empty($return)) ? "{$days}d " : '';
2021-04-08 13:28:13 +02:00
$hours = floor($time / (60 * 60));
$time -= $hours * (60 * 60);
2022-01-27 10:39:18 +01:00
$return .= ($hours != 0 || !empty($return)) ? "{$hours}h " : '';
2021-04-08 13:28:13 +02:00
$minutes = floor($time / 60);
$time -= $minutes * 60;
2022-01-27 10:39:18 +01:00
$return .= ($minutes != 0 || !empty($return)) ? "{$minutes}m " : '';
2021-04-08 13:28:13 +02:00
2022-01-26 15:34:32 +01:00
$time = round($time, 3);
2022-01-27 10:39:18 +01:00
$return .= ($time != 0 || !empty($return)) ? "{$time}s " : '';
2021-04-08 13:28:13 +02:00
2022-01-26 15:34:32 +01:00
return $return;
2021-04-08 13:28:13 +02:00
});
$parseTags = new TwigFilter('parsetags', function(string $text) {
$results = [];
preg_match_all('/\[([A-Za-z0-9 \-]+)\]/', $text, $results);
foreach ($results[0] as $key=>$result) {
$background = substr(md5($results[0][$key]), 0, 6);
$color = $this->lightOrDark($background) == 'dark' ? 'ffffff' : '000000';
2022-02-01 14:29:19 +01:00
$text = str_replace($results[0][$key], '<span class="tag" data-background-color="#' . $background . '" data-color="#' . $color . '">' . $results[1][$key] . '</span>', $text);
}
return $text;
});
2022-01-26 15:34:32 +01:00
$this->environment->addFilter($secondsToInterval);
$this->environment->addFilter($parseTags);
}
private function lightOrDark ($color) {
$color = str_split($color, 2);
foreach($color as &$value) {
$value = hexdec($value);
}
// HSP (Highly Sensitive Poo) equation from http://alienryderflex.com/hsp.html
$hsp = sqrt(
0.299 * ($color[0] * $color[0]) +
0.587 * ($color[1] * $color[1]) +
0.114 * ($color[2] * $color[2])
);
// Using the HSP value, determine whether the color is light or dark
if ($hsp>140) {
return 'light';
} else {
return 'dark';
}
2021-04-08 13:19:33 +02:00
}
}