|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Sylius package. |
| 5 | + * |
| 6 | + * (c) Sylius Sp. z o.o. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace App\Grid\Renderer; |
| 15 | + |
| 16 | +use Sylius\Bundle\ResourceBundle\Grid\Parser\OptionsParserInterface; |
| 17 | +use Sylius\Component\Grid\Definition\Action; |
| 18 | +use Sylius\Component\Grid\Definition\Field; |
| 19 | +use Sylius\Component\Grid\Definition\Filter; |
| 20 | +use Sylius\Component\Grid\Renderer\GridRendererInterface; |
| 21 | +use Sylius\Component\Grid\View\GridViewInterface; |
| 22 | +use Symfony\Component\DependencyInjection\Attribute\AsDecorator; |
| 23 | +use Symfony\Component\DependencyInjection\Attribute\Autowire; |
| 24 | +use Symfony\Component\HttpFoundation\Request; |
| 25 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 26 | +use Twig\Environment; |
| 27 | + |
| 28 | +#[AsDecorator(decorates: 'sylius.custom_grid_renderer.twig')] |
| 29 | +final class TwigGridRenderer implements GridRendererInterface |
| 30 | +{ |
| 31 | + public function __construct( |
| 32 | + private readonly GridRendererInterface $gridRenderer, |
| 33 | + private readonly Environment $twig, |
| 34 | + private readonly OptionsParserInterface $optionsParser, |
| 35 | + #[Autowire(param: 'sylius.grid.templates.action')] |
| 36 | + private array $actionTemplates = [], |
| 37 | + private readonly RequestStack|null $requestStack = null, |
| 38 | + ) { |
| 39 | + } |
| 40 | + |
| 41 | + public function render(GridViewInterface $gridView, string|null $template = null): string |
| 42 | + { |
| 43 | + return $this->gridRenderer->render($gridView, $template); |
| 44 | + } |
| 45 | + |
| 46 | + public function renderField(GridViewInterface $gridView, Field $field, $data): string |
| 47 | + { |
| 48 | + return $this->gridRenderer->renderField($gridView, $field, $data); |
| 49 | + } |
| 50 | + |
| 51 | + public function renderAction(GridViewInterface $gridView, Action $action, $data = null): string |
| 52 | + { |
| 53 | + $type = $action->getType(); |
| 54 | + |
| 55 | + $template = $action->getOptions()['template'] ?? $this->actionTemplates[$type] ?? null; |
| 56 | + |
| 57 | + if (null === $template) { |
| 58 | + throw new \InvalidArgumentException(sprintf('Missing template for action type "%s".', $type)); |
| 59 | + } |
| 60 | + |
| 61 | + $options = $this->optionsParser->parseOptions( |
| 62 | + $action->getOptions(), |
| 63 | + $this->requestStack?->getCurrentRequest() ?? new Request(), |
| 64 | + $data, |
| 65 | + ); |
| 66 | + |
| 67 | + return $this->twig->render($template, [ |
| 68 | + 'grid' => $gridView, |
| 69 | + 'action' => $action, |
| 70 | + 'data' => $data, |
| 71 | + 'options' => $options, |
| 72 | + ]); |
| 73 | + } |
| 74 | + |
| 75 | + public function renderFilter(GridViewInterface $gridView, Filter $filter): string |
| 76 | + { |
| 77 | + return $this->gridRenderer->renderFilter($gridView, $filter); |
| 78 | + } |
| 79 | +} |
0 commit comments