Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
63.29% |
50 / 79 |
|
62.50% |
10 / 16 |
CRAP | |
0.00% |
0 / 1 |
| ComponentHelperExtension | |
63.29% |
50 / 79 |
|
62.50% |
10 / 16 |
66.78 | |
0.00% |
0 / 1 |
| selectComponent | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| deselectComponent | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| apieConstant | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| translate | |
66.67% |
8 / 12 |
|
0.00% |
0 / 1 |
4.59 | |||
| safeJsonEncode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getFilters | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getFunctions | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
1 | |||
| renderValidationError | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
30 | |||
| getCurrentContext | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| getCurrentRenderer | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| getCurrentComponent | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| assetContent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| assetUrl | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| property | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| component | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| isPrototyped | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\TwigTemplateLayoutRenderer\Extension; |
| 3 | |
| 4 | use Apie\Core\ApieLib; |
| 5 | use Apie\Core\Context\ApieContext; |
| 6 | use Apie\Core\Translator\ApieTranslator; |
| 7 | use Apie\Core\Translator\ApieTranslatorInterface; |
| 8 | use Apie\Core\Translator\Lists\TranslationStringSet; |
| 9 | use Apie\Core\Translator\ValueObjects\TranslationString; |
| 10 | use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException; |
| 11 | use Apie\HtmlBuilders\Interfaces\ComponentInterface; |
| 12 | use Apie\TwigTemplateLayoutRenderer\TwigRenderer; |
| 13 | use LogicException; |
| 14 | use ReflectionClass; |
| 15 | use Twig\Environment; |
| 16 | use Twig\Extension\AbstractExtension; |
| 17 | use Twig\Runtime\EscaperRuntime; |
| 18 | use Twig\TwigFilter; |
| 19 | use Twig\TwigFunction; |
| 20 | |
| 21 | class ComponentHelperExtension extends AbstractExtension |
| 22 | { |
| 23 | /** @var ComponentInterface[] */ |
| 24 | private array $componentsHandled = []; |
| 25 | |
| 26 | /** @var TwigRenderer[] */ |
| 27 | private array $renderers = []; |
| 28 | |
| 29 | /** @var ApieContext[] */ |
| 30 | private array $contexts = []; |
| 31 | |
| 32 | public function selectComponent( |
| 33 | TwigRenderer $renderer, |
| 34 | ComponentInterface $component, |
| 35 | ApieContext $apieContext |
| 36 | ): void { |
| 37 | $this->renderers[] = $renderer; |
| 38 | $this->componentsHandled[] = $component; |
| 39 | $this->contexts[] = $apieContext; |
| 40 | } |
| 41 | |
| 42 | public function deselectComponent(ComponentInterface $component): void |
| 43 | { |
| 44 | if (end($this->componentsHandled) !== $component) { |
| 45 | throw new LogicException('Last component is not the one being deselected'); |
| 46 | } |
| 47 | array_pop($this->componentsHandled); |
| 48 | array_pop($this->renderers); |
| 49 | array_pop($this->contexts); |
| 50 | } |
| 51 | |
| 52 | public function apieConstant(string $constantName): mixed |
| 53 | { |
| 54 | $refl = new ReflectionClass(ApieLib::class); |
| 55 | return $refl->getConstant($constantName); |
| 56 | } |
| 57 | |
| 58 | public function translate(string|TranslationString|TranslationStringSet $translation): string |
| 59 | { |
| 60 | $apieContext = $this->getCurrentContext(); |
| 61 | $translator = $apieContext->getContext(ApieTranslatorInterface::class, false) ?? ApieTranslator::create(); |
| 62 | try { |
| 63 | return $translator->getGeneralTranslation( |
| 64 | $apieContext, |
| 65 | is_string($translation) |
| 66 | ? new TranslationString($translation) |
| 67 | : $translation |
| 68 | ) ?? $translation; |
| 69 | } catch (InvalidStringForValueObjectException) { |
| 70 | if ($translation instanceof TranslationStringSet) { |
| 71 | return $translation->first(); |
| 72 | } |
| 73 | return (string) $translation; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | public function safeJsonEncode(mixed $data): string |
| 78 | { |
| 79 | return json_encode($data, JSON_HEX_QUOT|JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS); |
| 80 | } |
| 81 | |
| 82 | public function getFilters(): array |
| 83 | { |
| 84 | return [ |
| 85 | new TwigFilter('safe_json_encode', [$this, 'safeJsonEncode'], ['is_safe' => ['all']]), |
| 86 | ]; |
| 87 | } |
| 88 | |
| 89 | public function getFunctions(): array |
| 90 | { |
| 91 | return [ |
| 92 | new TwigFunction('component', [$this, 'component'], ['is_safe' => ['all']]), |
| 93 | new TwigFunction('isPrototyped', [$this, 'isPrototyped']), |
| 94 | new TwigFunction('apieConstant', [$this, 'apieConstant']), |
| 95 | new TwigFunction('translate', [$this, 'translate'], []), |
| 96 | new TwigFunction('property', [$this, 'property'], []), |
| 97 | new TwigFunction('assetUrl', [$this, 'assetUrl'], []), |
| 98 | new TwigFunction('assetContent', [$this, 'assetContent'], ['is_safe' => ['all']]), |
| 99 | new TwigFunction( |
| 100 | 'renderValidationError', |
| 101 | [$this, 'renderValidationError'], |
| 102 | ['needs_environment' => true, 'is_safe' => ['all']] |
| 103 | ), |
| 104 | ]; |
| 105 | } |
| 106 | |
| 107 | public function renderValidationError( |
| 108 | Environment $env, |
| 109 | mixed $value, |
| 110 | array|string|null $validationError |
| 111 | ): string { |
| 112 | if ($validationError === null) { |
| 113 | return ''; |
| 114 | } |
| 115 | $escaper = $env->getRuntime(EscaperRuntime::class); |
| 116 | $valueAttr = ''; |
| 117 | $valueScript = ''; |
| 118 | if ($value !== null) { |
| 119 | if (is_string($value)) { |
| 120 | $valueAttr = ' exact-match="' . $escaper->escape($value, 'html_attr', null, false) . '"'; |
| 121 | } else { |
| 122 | $valueAttr = ' class="unhandled-constraint"'; |
| 123 | $valueScript = '<script> |
| 124 | (function (elm) { |
| 125 | elm.classList.remove("unhandled-constraint"); |
| 126 | elm.value = ' . str_replace('<', '<', json_encode($value)) . '; |
| 127 | }(document.querySelector(".unhandled-constraint")); |
| 128 | </script>'; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if (is_string($validationError)) { |
| 133 | $escapedValidationError = $escaper->escape($validationError, 'html_attr', null, false); |
| 134 | return '<apie-constraint-check-definition message="' |
| 135 | . $escapedValidationError |
| 136 | . '"' |
| 137 | . $valueAttr |
| 138 | . '></apie-constraint-check-definition>' |
| 139 | . $valueScript; |
| 140 | } |
| 141 | return ''; |
| 142 | } |
| 143 | |
| 144 | private function getCurrentContext(): ApieContext |
| 145 | { |
| 146 | if (empty($this->contexts)) { |
| 147 | throw new LogicException('No component is selected'); |
| 148 | } |
| 149 | return end($this->contexts); |
| 150 | } |
| 151 | |
| 152 | private function getCurrentRenderer(): TwigRenderer |
| 153 | { |
| 154 | if (empty($this->renderers)) { |
| 155 | throw new LogicException('No component is selected'); |
| 156 | } |
| 157 | return end($this->renderers); |
| 158 | } |
| 159 | |
| 160 | private function getCurrentComponent(): ComponentInterface |
| 161 | { |
| 162 | if (empty($this->componentsHandled)) { |
| 163 | throw new LogicException('No component is selected'); |
| 164 | } |
| 165 | return end($this->componentsHandled); |
| 166 | } |
| 167 | |
| 168 | public function assetContent(string $filename): string |
| 169 | { |
| 170 | return $this->getCurrentRenderer()->getAssetContents($filename); |
| 171 | } |
| 172 | |
| 173 | public function assetUrl(string $filename): string |
| 174 | { |
| 175 | return $this->getCurrentRenderer()->getAssetUrl($filename); |
| 176 | } |
| 177 | |
| 178 | public function property(string $attributeKey): mixed |
| 179 | { |
| 180 | return $this->getCurrentComponent()->getAttribute($attributeKey); |
| 181 | } |
| 182 | |
| 183 | public function component(string $componentName): string |
| 184 | { |
| 185 | return $this->getCurrentRenderer()->render( |
| 186 | $this->getCurrentComponent()->getComponent($componentName), |
| 187 | $this->getCurrentContext() |
| 188 | ); |
| 189 | } |
| 190 | |
| 191 | public function isPrototyped(): bool |
| 192 | { |
| 193 | $attrs = $this->getCurrentComponent()->getAttribute('additionalAttributes'); |
| 194 | return is_array($attrs) ? ((bool) ($attrs['prototyped'] ?? false)) : false; |
| 195 | } |
| 196 | } |