2022-04-27 14:24:48 +02:00
|
|
|
<?php
|
|
|
|
namespace App\Twig;
|
|
|
|
|
2022-05-20 14:48:22 +02:00
|
|
|
use App\Service\Secret;
|
2022-04-27 14:24:48 +02:00
|
|
|
use Twig\Extension\AbstractExtension;
|
|
|
|
use Twig\TwigFilter;
|
2022-09-05 14:43:01 +02:00
|
|
|
use Twig\TwigTest;
|
2022-04-27 14:24:48 +02:00
|
|
|
|
|
|
|
class AppExtension extends AbstractExtension
|
|
|
|
{
|
|
|
|
public function getFilters()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
new TwigFilter('interval', [$this, 'parseInterval']),
|
|
|
|
new TwigFilter('parsetags', [$this, 'parseTags']),
|
2022-05-20 14:48:22 +02:00
|
|
|
new TwigFilter('decryptsecret', [$this, 'decryptSecret']),
|
2022-09-05 14:43:01 +02:00
|
|
|
new TwigFilter('contents', [$this, 'getContents']),
|
2022-04-27 14:24:48 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:43:01 +02:00
|
|
|
public function getTests()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
new TwigTest('ondisk', [$this, 'onDisk'])
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseInterval(int|float $time)
|
|
|
|
{
|
2022-04-27 14:24:48 +02:00
|
|
|
$return = '';
|
|
|
|
|
|
|
|
$days = floor($time / (60 * 60 * 24));
|
|
|
|
$time -= $days * (60 * 60 * 24);
|
|
|
|
$return .= ($days != 0 || !empty($return)) ? "{$days}d " : '';
|
|
|
|
|
|
|
|
$hours = floor($time / (60 * 60));
|
|
|
|
$time -= $hours * (60 * 60);
|
|
|
|
$return .= ($hours != 0 || !empty($return)) ? "{$hours}h " : '';
|
|
|
|
|
|
|
|
$minutes = floor($time / 60);
|
|
|
|
$time -= $minutes * 60;
|
|
|
|
$return .= ($minutes != 0 || !empty($return)) ? "{$minutes}m " : '';
|
|
|
|
|
|
|
|
$time = round($time, 3);
|
|
|
|
$return .= ($time != 0 || !empty($return)) ? "{$time}s " : '';
|
|
|
|
|
2022-07-04 11:11:44 +02:00
|
|
|
return (!empty($return)) ? trim($return) : '0.000s';
|
2022-04-27 14:24:48 +02:00
|
|
|
}
|
|
|
|
|
2022-09-05 14:43:01 +02:00
|
|
|
function parseTags(string $text)
|
|
|
|
{
|
2022-04-27 14:24:48 +02:00
|
|
|
$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';
|
|
|
|
$text = str_replace($results[0][$key], '<span class="tag" data-background-color="#' . $background . '" data-color="#' . $color . '">' . $results[1][$key] . '</span>', $text);
|
|
|
|
}
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:43:01 +02:00
|
|
|
private function lightOrDark ($color)
|
|
|
|
{
|
2022-04-27 14:24:48 +02:00
|
|
|
$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';
|
|
|
|
}
|
|
|
|
}
|
2022-05-20 14:48:22 +02:00
|
|
|
|
2022-09-05 14:43:01 +02:00
|
|
|
function decryptSecret(string $text)
|
|
|
|
{
|
2022-05-20 14:48:22 +02:00
|
|
|
return Secret::decrypt(base64_decode($text));
|
|
|
|
}
|
2022-09-05 14:43:01 +02:00
|
|
|
|
|
|
|
function getContents(string $file)
|
|
|
|
{
|
|
|
|
return file_get_contents($file);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onDisk(string $file)
|
|
|
|
{
|
|
|
|
return file_exists($file);
|
|
|
|
}
|
2022-04-27 14:24:48 +02:00
|
|
|
}
|