mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-01 14:04:30 +02:00
Refactored TwigExtensions Part 2
This commit is contained in:
parent
b078389381
commit
a9e527ce2a
22 changed files with 168 additions and 124 deletions
30
src/Twig/AttachmentExtension.php
Normal file
30
src/Twig/AttachmentExtension.php
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace App\Twig;
|
||||
|
||||
use App\Services\Attachments\AttachmentURLGenerator;
|
||||
use App\Services\FAIconGenerator;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
final class AttachmentExtension extends AbstractExtension
|
||||
{
|
||||
protected $attachmentURLGenerator;
|
||||
protected $FAIconGenerator;
|
||||
|
||||
public function __construct(AttachmentURLGenerator $attachmentURLGenerator, FAIconGenerator $FAIconGenerator)
|
||||
{
|
||||
$this->attachmentURLGenerator = $attachmentURLGenerator;
|
||||
$this->FAIconGenerator = $FAIconGenerator;
|
||||
}
|
||||
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
/* Returns the URL to a thumbnail of the given attachment */
|
||||
new TwigFunction('attachment_thumbnail', [$this->attachmentURLGenerator, 'getThumbnailURL']),
|
||||
/* Returns the font awesome icon class which is representing the given file extension */
|
||||
new TwigFunction('ext_to_fa_icon', [$this->FAIconGenerator, 'fileExtensionToFAType']),
|
||||
];
|
||||
}
|
||||
}
|
|
@ -23,18 +23,21 @@ namespace App\Twig;
|
|||
use Com\Tecnick\Barcode\Barcode;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
class BarcodeExtension extends AbstractExtension
|
||||
final class BarcodeExtension extends AbstractExtension
|
||||
{
|
||||
public function getFilters(): array
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
new TwigFilter('barcodeSVG', static function (string $content, string $type = 'QRCODE') {
|
||||
$barcodeFactory = new Barcode();
|
||||
$barcode = $barcodeFactory->getBarcodeObj($type, $content);
|
||||
|
||||
return $barcode->getSvgCode();
|
||||
}),
|
||||
/* Generates a barcode with the given Type and Data and returns it as an SVG represenation */
|
||||
new TwigFunction('barcode_svg', [$this, 'barcodeSVG']),
|
||||
];
|
||||
}
|
||||
|
||||
public function barcodeSVG(string $content, string $type): string
|
||||
{
|
||||
$barcodeFactory = new Barcode();
|
||||
return $barcodeFactory->getBarcodeObj($type, $content)->getSvgCode();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,26 +16,24 @@ use App\Entity\Parts\Supplier;
|
|||
use App\Entity\PriceInformations\Currency;
|
||||
use App\Entity\UserSystem\Group;
|
||||
use App\Entity\UserSystem\User;
|
||||
use App\Services\ElementTypeNameGenerator;
|
||||
use App\Services\EntityURLGenerator;
|
||||
use App\Services\Trees\TreeViewGenerator;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
use Twig\TwigFunction;
|
||||
use Twig\TwigTest;
|
||||
|
||||
class EntityExtension extends AbstractExtension
|
||||
final class EntityExtension extends AbstractExtension
|
||||
{
|
||||
protected $entityURLGenerator;
|
||||
protected $treeBuilder;
|
||||
private $nameGenerator;
|
||||
|
||||
public function __construct(EntityURLGenerator $entityURLGenerator)
|
||||
public function __construct(EntityURLGenerator $entityURLGenerator, TreeViewGenerator $treeBuilder, ElementTypeNameGenerator $elementTypeNameGenerator)
|
||||
{
|
||||
$this->entityURLGenerator = $entityURLGenerator;
|
||||
}
|
||||
|
||||
public function getFilters(): array
|
||||
{
|
||||
return [
|
||||
|
||||
];
|
||||
$this->treeBuilder = $treeBuilder;
|
||||
$this->nameGenerator = $elementTypeNameGenerator;
|
||||
}
|
||||
|
||||
public function getTests(): array
|
||||
|
@ -53,10 +51,23 @@ class EntityExtension extends AbstractExtension
|
|||
return [
|
||||
/* Returns a string representation of the given entity */
|
||||
new TwigFunction('entity_type', [$this, 'getEntityType']),
|
||||
/* Returns the URL to the given entity */
|
||||
new TwigFunction('entity_url', [$this, 'generateEntityURL']),
|
||||
/* Generates a JSON array of the given tree */
|
||||
new TwigFunction('tree_data', [$this, 'treeData']),
|
||||
|
||||
/* Gets a human readable label for the type of the given entity */
|
||||
new TwigFunction('entity_type_label', [$this->nameGenerator, 'getLocalizedTypeLabel']),
|
||||
];
|
||||
}
|
||||
|
||||
public function treeData(AbstractDBElement $element, string $type = 'newEdit'): string
|
||||
{
|
||||
$tree = $this->treeBuilder->getTreeView(get_class($element), null, $type, $element);
|
||||
|
||||
return json_encode($tree, JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
public function generateEntityURL(AbstractDBElement $entity, string $method = 'info'): string
|
||||
{
|
||||
return $this->entityURLGenerator->getURL($entity, $method);
|
||||
|
|
|
@ -75,84 +75,40 @@ use Twig\TwigTest;
|
|||
|
||||
use function get_class;
|
||||
|
||||
class AppExtension extends AbstractExtension
|
||||
final class FormatExtension extends AbstractExtension
|
||||
{
|
||||
protected $markdownParser;
|
||||
protected $serializer;
|
||||
protected $treeBuilder;
|
||||
protected $moneyFormatter;
|
||||
protected $siformatter;
|
||||
protected $amountFormatter;
|
||||
protected $attachmentURLGenerator;
|
||||
protected $FAIconGenerator;
|
||||
protected $translator;
|
||||
|
||||
public function __construct(MarkdownParser $markdownParser,
|
||||
SerializerInterface $serializer, TreeViewGenerator $treeBuilder,
|
||||
MoneyFormatter $moneyFormatter,
|
||||
SIFormatter $SIFormatter, AmountFormatter $amountFormatter,
|
||||
AttachmentURLGenerator $attachmentURLGenerator,
|
||||
FAIconGenerator $FAIconGenerator, TranslatorInterface $translator)
|
||||
|
||||
public function __construct(MarkdownParser $markdownParser, MoneyFormatter $moneyFormatter,
|
||||
SIFormatter $SIFormatter, AmountFormatter $amountFormatter)
|
||||
{
|
||||
$this->markdownParser = $markdownParser;
|
||||
$this->serializer = $serializer;
|
||||
$this->treeBuilder = $treeBuilder;
|
||||
$this->moneyFormatter = $moneyFormatter;
|
||||
$this->siformatter = $SIFormatter;
|
||||
$this->amountFormatter = $amountFormatter;
|
||||
$this->attachmentURLGenerator = $attachmentURLGenerator;
|
||||
$this->FAIconGenerator = $FAIconGenerator;
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
public function getFilters(): array
|
||||
{
|
||||
return [
|
||||
new TwigFilter('markdown', [$this->markdownParser, 'markForRendering'], [
|
||||
/* Mark the given text as markdown, which will be rendered in the browser */
|
||||
new TwigFilter('format_markdown', [$this->markdownParser, 'markForRendering'], [
|
||||
'pre_escape' => 'html',
|
||||
'is_safe' => ['html'],
|
||||
]),
|
||||
new TwigFilter('moneyFormat', [$this, 'formatCurrency']),
|
||||
new TwigFilter('siFormat', [$this, 'siFormat']),
|
||||
new TwigFilter('amountFormat', [$this, 'amountFormat']),
|
||||
new TwigFilter('loginPath', [$this, 'loginPath']),
|
||||
/* Format the given amount as money, using a given currency */
|
||||
new TwigFilter('format_money', [$this, 'formatCurrency']),
|
||||
/* Format the given number using SI prefixes and the given unit (string) */
|
||||
new TwigFilter('format_si', [$this, 'siFormat']),
|
||||
/** Format the given amount using the given MeasurementUnit */
|
||||
new TwigFilter('format_amount', [$this, 'amountFormat']),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
new TwigFunction('generateTreeData', [$this, 'treeData']),
|
||||
new TwigFunction('attachment_thumbnail', [$this->attachmentURLGenerator, 'getThumbnailURL']),
|
||||
new TwigFunction('ext_to_fa_icon', [$this->FAIconGenerator, 'fileExtensionToFAType']),
|
||||
];
|
||||
}
|
||||
|
||||
public function treeData(AbstractDBElement $element, string $type = 'newEdit'): string
|
||||
{
|
||||
$tree = $this->treeBuilder->getTreeView(get_class($element), null, $type, $element);
|
||||
|
||||
return json_encode($tree, JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* This function/filter generates an path.
|
||||
*/
|
||||
public function loginPath(string $path): string
|
||||
{
|
||||
$parts = explode('/', $path);
|
||||
//Remove the part with
|
||||
unset($parts[1]);
|
||||
|
||||
return implode('/', $parts);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function formatCurrency($amount, ?Currency $currency = null, int $decimals = 5): string
|
||||
{
|
||||
if ($amount instanceof BigDecimal) {
|
|
@ -11,7 +11,7 @@ use Twig\TwigTest;
|
|||
/**
|
||||
* The functionalities here extend the Twig with some core functions, which are independently of Part-DB.
|
||||
*/
|
||||
class TwigCoreExtension extends AbstractExtension
|
||||
final class TwigCoreExtension extends AbstractExtension
|
||||
{
|
||||
protected $objectNormalizer;
|
||||
|
||||
|
|
|
@ -1,25 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Twig;
|
||||
|
||||
use App\Services\ElementTypeNameGenerator;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
final class TypeLabelExtension extends AbstractExtension
|
||||
{
|
||||
private $nameGenerator;
|
||||
|
||||
public function __construct(ElementTypeNameGenerator $elementTypeNameGenerator)
|
||||
{
|
||||
$this->nameGenerator = $elementTypeNameGenerator;
|
||||
}
|
||||
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
new TwigFunction('elementType', [$this->nameGenerator, 'getLocalizedTypeLabel']),
|
||||
new TwigFunction('elementTypeName', [$this->nameGenerator, 'getTypeNameCombination']),
|
||||
];
|
||||
}
|
||||
}
|
|
@ -27,9 +27,10 @@ use App\Entity\LogSystem\AbstractLogEntry;
|
|||
use App\Repository\LogEntryRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
class UserExtension extends AbstractExtension
|
||||
final class UserExtension extends AbstractExtension
|
||||
{
|
||||
/** @var LogEntryRepository */
|
||||
private $repo;
|
||||
|
@ -39,6 +40,13 @@ class UserExtension extends AbstractExtension
|
|||
$this->repo = $em->getRepository(AbstractLogEntry::class);
|
||||
}
|
||||
|
||||
public function getFilters(): array
|
||||
{
|
||||
return [
|
||||
new TwigFilter('remove_locale_from_path', [$this, 'removeLocaleFromPath']),
|
||||
];
|
||||
}
|
||||
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
|
@ -48,4 +56,22 @@ class UserExtension extends AbstractExtension
|
|||
new TwigFunction('creating_user', [$this->repo, 'getCreatingUser']),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* This function/filter generates an path.
|
||||
*/
|
||||
public function removeLocaleFromPath(string $path): string
|
||||
{
|
||||
//Ensure the path has the correct format
|
||||
if (!preg_match('/^\/\w{2}\//', $path)) {
|
||||
throw new \InvalidArgumentException('The given path is not a localized path!');
|
||||
}
|
||||
|
||||
$parts = explode('/', $path);
|
||||
//Remove the part with locale
|
||||
unset($parts[1]);
|
||||
|
||||
return implode('/', $parts);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue