-
-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathTwigGridRenderer.php
More file actions
95 lines (78 loc) · 2.81 KB
/
Copy pathTwigGridRenderer.php
File metadata and controls
95 lines (78 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Bundle\ResourceBundle\Grid\Renderer;
use Sylius\Bundle\ResourceBundle\Grid\Parser\OptionsParserInterface;
use Sylius\Bundle\ResourceBundle\Grid\View\ResourceGridView;
use Sylius\Component\Grid\Definition\Action;
use Sylius\Component\Grid\Definition\Field;
use Sylius\Component\Grid\Definition\Filter;
use Sylius\Component\Grid\Renderer\GridRendererInterface;
use Sylius\Component\Grid\View\GridViewInterface;
use Twig\Environment;
final class TwigGridRenderer implements GridRendererInterface
{
private GridRendererInterface $gridRenderer;
private Environment $twig;
private OptionsParserInterface $optionsParser;
private array $actionTemplates;
public function __construct(
GridRendererInterface $gridRenderer,
Environment $twig,
OptionsParserInterface $optionsParser,
array $actionTemplates = [],
) {
$this->gridRenderer = $gridRenderer;
$this->twig = $twig;
$this->optionsParser = $optionsParser;
$this->actionTemplates = $actionTemplates;
}
public function render(GridViewInterface $gridView, ?string $template = null): string
{
return $this->gridRenderer->render($gridView, $template);
}
/**
* @param mixed $data
*/
public function renderField(GridViewInterface $gridView, Field $field, $data): string
{
return $this->gridRenderer->renderField($gridView, $field, $data);
}
/**
* @param mixed $data
*/
public function renderAction(GridViewInterface $gridView, Action $action, $data = null): string
{
if (!$gridView instanceof ResourceGridView) {
return $this->gridRenderer->renderAction($gridView, $action, $data);
}
$type = $action->getType();
$template = method_exists($action, 'getTemplate') ? $action->getTemplate() : null;
$template ??= $this->actionTemplates[$type] ?? null;
if (null === $template) {
throw new \InvalidArgumentException(sprintf('Missing template for action type "%s".', $type));
}
$options = $this->optionsParser->parseOptions(
$action->getOptions(),
$gridView->getRequestConfiguration()->getRequest(),
$data,
);
return $this->twig->render($template, [
'grid' => $gridView,
'action' => $action,
'data' => $data,
'options' => $options,
]);
}
public function renderFilter(GridViewInterface $gridView, Filter $filter): string
{
return $this->gridRenderer->renderFilter($gridView, $filter);
}
}