1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
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 Sylius\Bundle\ResourceBundle\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
|
|
|
|
23
|
|
|
final class TwigGridRenderer implements GridRendererInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var GridRendererInterface |
27
|
|
|
*/ |
28
|
|
|
private $gridRenderer; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \Twig_Environment |
32
|
|
|
*/ |
33
|
|
|
private $twig; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var OptionsParserInterface |
37
|
|
|
*/ |
38
|
|
|
private $optionsParser; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $actionTemplates; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
private $bulkActionTemplates; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param GridRendererInterface $gridRenderer |
52
|
|
|
* @param \Twig_Environment $twig |
53
|
|
|
* @param OptionsParserInterface $optionsParser |
54
|
|
|
* @param array $actionTemplates |
55
|
|
|
* @param array $bulkActionTemplates |
56
|
|
|
*/ |
57
|
|
|
public function __construct( |
58
|
|
|
GridRendererInterface $gridRenderer, |
59
|
|
|
\Twig_Environment $twig, |
60
|
|
|
OptionsParserInterface $optionsParser, |
61
|
|
|
array $actionTemplates = [], |
62
|
|
|
array $bulkActionTemplates = [] |
63
|
|
|
) { |
64
|
|
|
$this->gridRenderer = $gridRenderer; |
65
|
|
|
$this->twig = $twig; |
66
|
|
|
$this->optionsParser = $optionsParser; |
67
|
|
|
$this->actionTemplates = $actionTemplates; |
68
|
|
|
$this->bulkActionTemplates = $bulkActionTemplates; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function render(GridViewInterface $gridView, ?string $template = null): string |
75
|
|
|
{ |
76
|
|
|
return (string) $this->gridRenderer->render($gridView, $template); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
public function renderField(GridViewInterface $gridView, Field $field, $data): string |
83
|
|
|
{ |
84
|
|
|
return (string) $this->gridRenderer->renderField($gridView, $field, $data); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
public function renderAction(GridViewInterface $gridView, Action $action, $data = null): string |
91
|
|
|
{ |
92
|
|
|
$type = $action->getType(); |
93
|
|
|
if (!isset($this->actionTemplates[$type])) { |
94
|
|
|
throw new \InvalidArgumentException(sprintf('Missing template for action type "%s".', $type)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$options = $this->optionsParser->parseOptions( |
98
|
|
|
$action->getOptions(), |
99
|
|
|
$gridView->getRequestConfiguration()->getRequest(), |
|
|
|
|
100
|
|
|
$data |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
return (string) $this->twig->render($this->actionTemplates[$type], [ |
104
|
|
|
'grid' => $gridView, |
105
|
|
|
'action' => $action, |
106
|
|
|
'data' => $data, |
107
|
|
|
'options' => $options, |
108
|
|
|
]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
|
|
public function renderBulkAction(GridViewInterface $gridView, Action $bulkAction, $data = null): string |
115
|
|
|
{ |
116
|
|
|
$type = $bulkAction->getType(); |
117
|
|
|
if (!isset($this->bulkActionTemplates[$type])) { |
118
|
|
|
throw new \InvalidArgumentException(sprintf('Missing template for bulk action type "%s".', $type)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$options = $this->optionsParser->parseOptions( |
122
|
|
|
$bulkAction->getOptions(), |
123
|
|
|
$gridView->getRequestConfiguration()->getRequest(), |
|
|
|
|
124
|
|
|
$data |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
return (string) $this->twig->render($this->bulkActionTemplates[$type], [ |
128
|
|
|
'grid' => $gridView, |
129
|
|
|
'action' => $bulkAction, |
130
|
|
|
'data' => $data, |
131
|
|
|
'options' => $options, |
132
|
|
|
]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
public function renderFilter(GridViewInterface $gridView, Filter $filter): string |
139
|
|
|
{ |
140
|
|
|
return (string) $this->gridRenderer->renderFilter($gridView, $filter); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: