Refactored TwigExtensions Part 1

This commit is contained in:
Jan Böhmer 2022-09-18 16:45:12 +02:00
parent 8e6300079a
commit b078389381
21 changed files with 301 additions and 89 deletions

View file

@ -77,7 +77,6 @@ use function get_class;
class AppExtension extends AbstractExtension
{
protected $entityURLGenerator;
protected $markdownParser;
protected $serializer;
protected $treeBuilder;
@ -88,16 +87,13 @@ class AppExtension extends AbstractExtension
protected $FAIconGenerator;
protected $translator;
protected $objectNormalizer;
public function __construct(EntityURLGenerator $entityURLGenerator, MarkdownParser $markdownParser,
public function __construct(MarkdownParser $markdownParser,
SerializerInterface $serializer, TreeViewGenerator $treeBuilder,
MoneyFormatter $moneyFormatter,
SIFormatter $SIFormatter, AmountFormatter $amountFormatter,
AttachmentURLGenerator $attachmentURLGenerator,
FAIconGenerator $FAIconGenerator, TranslatorInterface $translator, ObjectNormalizer $objectNormalizer)
FAIconGenerator $FAIconGenerator, TranslatorInterface $translator)
{
$this->entityURLGenerator = $entityURLGenerator;
$this->markdownParser = $markdownParser;
$this->serializer = $serializer;
$this->treeBuilder = $treeBuilder;
@ -107,14 +103,11 @@ class AppExtension extends AbstractExtension
$this->attachmentURLGenerator = $attachmentURLGenerator;
$this->FAIconGenerator = $FAIconGenerator;
$this->translator = $translator;
$this->objectNormalizer = $objectNormalizer;
}
public function getFilters(): array
{
return [
new TwigFilter('entityURL', [$this, 'generateEntityURL']),
new TwigFilter('markdown', [$this->markdownParser, 'markForRendering'], [
'pre_escape' => 'html',
'is_safe' => ['html'],
@ -123,25 +116,10 @@ class AppExtension extends AbstractExtension
new TwigFilter('siFormat', [$this, 'siFormat']),
new TwigFilter('amountFormat', [$this, 'amountFormat']),
new TwigFilter('loginPath', [$this, 'loginPath']),
new TwigFilter('toArray', [$this, 'toArray'])
];
}
public function getTests(): array
{
return [
new TwigTest('instanceof', static function ($var, $instance) {
return $var instanceof $instance;
}),
new TwigTest('entity', static function ($var) {
return $var instanceof AbstractDBElement;
}),
new TwigTest('object', static function ($var) {
return is_object($var);
}),
];
}
public function getFunctions(): array
{
@ -149,31 +127,9 @@ class AppExtension extends AbstractExtension
new TwigFunction('generateTreeData', [$this, 'treeData']),
new TwigFunction('attachment_thumbnail', [$this->attachmentURLGenerator, 'getThumbnailURL']),
new TwigFunction('ext_to_fa_icon', [$this->FAIconGenerator, 'fileExtensionToFAType']),
new TwigFunction('entity_type', [$this, 'getEntityType']),
];
}
public function getEntityType($entity): ?string
{
$map = [
Part::class => 'part',
Footprint::class => 'footprint',
Storelocation::class => 'storelocation',
Manufacturer::class => 'manufacturer',
Category::class => 'category',
Device::class => 'device',
Attachment::class => 'attachment',
Supplier::class => 'supplier',
User::class => 'user',
Group::class => 'group',
Currency::class => 'currency',
MeasurementUnit::class => 'measurement_unit',
LabelProfile::class => 'label_profile',
];
return $map[get_class($entity)] ?? null;
}
public function treeData(AbstractDBElement $element, string $type = 'newEdit'): string
{
$tree = $this->treeBuilder->getTreeView(get_class($element), null, $type, $element);
@ -181,10 +137,7 @@ class AppExtension extends AbstractExtension
return json_encode($tree, JSON_THROW_ON_ERROR);
}
public function toArray($object): array
{
return $this->objectNormalizer->normalize($object, null);
}
/**
* This function/filter generates an path.
@ -198,10 +151,7 @@ class AppExtension extends AbstractExtension
return implode('/', $parts);
}
public function generateEntityURL(AbstractDBElement $entity, string $method = 'info'): string
{
return $this->entityURLGenerator->getURL($entity, $method);
}
public function formatCurrency($amount, ?Currency $currency = null, int $decimals = 5): string
{

View file

@ -0,0 +1,91 @@
<?php
namespace App\Twig;
use App\Entity\Attachments\Attachment;
use App\Entity\Base\AbstractDBElement;
use App\Entity\Devices\Device;
use App\Entity\LabelSystem\LabelProfile;
use App\Entity\Parts\Category;
use App\Entity\Parts\Footprint;
use App\Entity\Parts\Manufacturer;
use App\Entity\Parts\MeasurementUnit;
use App\Entity\Parts\Part;
use App\Entity\Parts\Storelocation;
use App\Entity\Parts\Supplier;
use App\Entity\PriceInformations\Currency;
use App\Entity\UserSystem\Group;
use App\Entity\UserSystem\User;
use App\Services\EntityURLGenerator;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Twig\TwigTest;
class EntityExtension extends AbstractExtension
{
protected $entityURLGenerator;
public function __construct(EntityURLGenerator $entityURLGenerator)
{
$this->entityURLGenerator = $entityURLGenerator;
}
public function getFilters(): array
{
return [
];
}
public function getTests(): array
{
return [
/* Checks if the given variable is an entitity (instance of AbstractDBElement) */
new TwigTest('entity', static function ($var) {
return $var instanceof AbstractDBElement;
}),
];
}
public function getFunctions(): array
{
return [
/* Returns a string representation of the given entity */
new TwigFunction('entity_type', [$this, 'getEntityType']),
new TwigFunction('entity_url', [$this, 'generateEntityURL']),
];
}
public function generateEntityURL(AbstractDBElement $entity, string $method = 'info'): string
{
return $this->entityURLGenerator->getURL($entity, $method);
}
public function getEntityType(object $entity): ?string
{
$map = [
Part::class => 'part',
Footprint::class => 'footprint',
Storelocation::class => 'storelocation',
Manufacturer::class => 'manufacturer',
Category::class => 'category',
Device::class => 'device',
Attachment::class => 'attachment',
Supplier::class => 'supplier',
User::class => 'user',
Group::class => 'group',
Currency::class => 'currency',
MeasurementUnit::class => 'measurement_unit',
LabelProfile::class => 'label_profile',
];
foreach ($map as $class => $type) {
if ($entity instanceof $class) {
return $type;
}
}
return false;
}
}

View file

@ -0,0 +1,60 @@
<?php
namespace App\Twig;
use App\Entity\Base\AbstractDBElement;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigTest;
/**
* The functionalities here extend the Twig with some core functions, which are independently of Part-DB.
*/
class TwigCoreExtension extends AbstractExtension
{
protected $objectNormalizer;
public function __construct(ObjectNormalizer $objectNormalizer)
{
$this->objectNormalizer = $objectNormalizer;
}
public function getTests(): array
{
return [
/*
* Checks if a given variable is an instance of a given class. E.g. ` x is instanceof('App\Entity\Parts\Part')`
*/
new TwigTest('instanceof', static function ($var, $instance) {
return $var instanceof $instance;
}),
/* Checks if a given variable is an object. E.g. `x is object` */
new TwigTest('object', static function ($var) {
return is_object($var);
}),
];
}
public function getFilters()
{
return [
/* Converts the given object to an array representation of the public/accessible properties */
new TwigFilter('to_array', [$this, 'toArray']),
];
}
public function toArray($object)
{
if(! is_object($object) && ! is_array($object)) {
throw new \InvalidArgumentException('The given variable is not an object or array!');
}
//If it is already an array, we can just return it
if(is_array($object)) {
return $object;
}
return $this->objectNormalizer->normalize($object, null);
}
}

View file

@ -29,7 +29,7 @@ use Doctrine\ORM\EntityManagerInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class LastUserExtension extends AbstractExtension
class UserExtension extends AbstractExtension
{
/** @var LogEntryRepository */
private $repo;
@ -42,8 +42,10 @@ class LastUserExtension extends AbstractExtension
public function getFunctions(): array
{
return [
new TwigFunction('getLastEditingUser', [$this->repo, 'getLastEditingUser']),
new TwigFunction('getCreatingUser', [$this->repo, 'getCreatingUser']),
/* Returns the user which has edited the given entity the last time. */
new TwigFunction('last_editing_user', [$this->repo, 'getLastEditingUser']),
/* Returns the user which has created the given entity. */
new TwigFunction('creating_user', [$this->repo, 'getCreatingUser']),
];
}
}