DigiComp.AttachmentViewUtility/Classes/EelFilenameTrait.php

73 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2023-10-17 18:49:19 +02:00
<?php
declare(strict_types=1);
namespace DigiComp\AttachmentViewUtility;
use Neos\Eel\CompilingEvaluator;
use Neos\Eel\Context;
use Neos\Eel\Helper\ArrayHelper;
use Neos\Eel\Helper\StringHelper;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\I18n\EelHelper\TranslationHelper;
use Neos\FluidAdaptor\View\TemplateView;
use Neos\Utility\TypeHandling;
trait EelFilenameTrait
{
protected string $eelExpressionOptionKey = 'filenameEelExpression';
protected function addOptionFilenameEelExpression(): void
{
if (\property_exists($this, 'supportedOptions')) {
$this->supportedOptions['filenameEelExpression'] = [
null,
'Callable which creates a filename from variables',
'string',
true,
];
} else {
throw new \RuntimeException('supported option could not be set', 1697552694);
}
}
protected function getAttachmentName(): string
{
if ($this instanceof TemplateView) {
$variables = $this->getRenderingContext()->getVariableProvider()->getAll();
} elseif (\property_exists($this, 'variables')) {
$variables = $this->variables;
} else {
throw new \RuntimeException(
'No variables can be detected for this kind of view: ' . TypeHandling::getTypeForValue($this),
1697550214
);
}
if (!\property_exists($this, 'options')) {
throw new \RuntimeException('Your view options could not be found', 1697550440);
}
$expression = $this->options[$this->eelExpressionOptionKey];
return $this->getEelResult($expression, $variables);
}
protected function getEelResult(string $expression, array $contextVariables)
{
$context = new Context(\array_merge($contextVariables, [
2023-10-17 18:49:19 +02:00
'Array' => new ArrayHelper(),
'String' => new StringHelper(),
'Translation' => new TranslationHelper(),
]));
$this->emitFilenameEelExpressionContext($context);
return (new CompilingEvaluator())->evaluate($expression, $context);
}
/**
* @SuppressWarnings("unused")
*/
#[Flow\Signal]
protected function emitFilenameEelExpressionContext(Context $context): void
{
}
}