4
0
Bifurcation 1

[TWIG] Add `instanceof` test

Use with:
{% if var is instanceof(Namespace\Class) %}
Cette révision appartient à :
Hugo Sales 2021-09-27 10:38:06 +01:00
Parent 2d8b220e92
révision 1107d8257d
Signé par: someonewithpc
ID de la clé GPG: 7D0C7EAFC9D835A0
2 fichiers modifiés avec 14 ajouts et 1 suppressions

Voir le fichier

@ -32,6 +32,7 @@ namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use Twig\TwigTest;
class Extension extends AbstractExtension
{
@ -42,6 +43,13 @@ class Extension extends AbstractExtension
];
}
public function getTests()
{
return [
new TwigTest('instanceof', [Runtime::class, 'isInstanceOf']),
];
}
/**
* get twig functions
*

Voir le fichier

@ -119,11 +119,16 @@ class Runtime implements RuntimeExtensionInterface, EventSubscriberInterface
public function isFirefox(): bool
{
$re_has_chrome = '/.*(?i)\bchrome\b.*/m';
$re_has_gecko = '/.*(?i)\bgecko\b.*/m';
$re_has_gecko = '/.*(?i)\bgecko\b.*/m';
return (preg_match(pattern: $re_has_chrome, subject: $this->request->headers->get('User-Agent')) !== 1)
&& (preg_match(pattern: $re_has_gecko, subject: $this->request->headers->get('User-Agent')) === 1);
}
public function isInstanceOf($var, string $instance): bool
{
return $var instanceof $instance;
}
// ----------------------------------------------------------
/**