1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Sylius\Bundle\ResourceBundle\Grid\Renderer; |
6
|
|
|
|
7
|
|
|
use Sylius\Bundle\ResourceBundle\Grid\Parser\OptionsParserInterface; |
8
|
|
|
use Sylius\Component\Grid\Definition\Action; |
9
|
|
|
use Sylius\Component\Grid\Renderer\BulkActionGridRendererInterface; |
10
|
|
|
use Sylius\Component\Grid\View\GridViewInterface; |
11
|
|
|
|
12
|
|
|
final class TwigBulkActionGridRenderer implements BulkActionGridRendererInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var \Twig_Environment |
16
|
|
|
*/ |
17
|
|
|
private $twig; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var OptionsParserInterface |
21
|
|
|
*/ |
22
|
|
|
private $optionsParser; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $bulkActionTemplates; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param \Twig_Environment $twig |
31
|
|
|
* @param OptionsParserInterface $optionsParser |
32
|
|
|
* @param array $bulkActionTemplates |
33
|
|
|
*/ |
34
|
|
|
public function __construct( |
35
|
|
|
\Twig_Environment $twig, |
36
|
|
|
OptionsParserInterface $optionsParser, |
37
|
|
|
array $bulkActionTemplates = [] |
38
|
|
|
) { |
39
|
|
|
$this->twig = $twig; |
40
|
|
|
$this->optionsParser = $optionsParser; |
41
|
|
|
$this->bulkActionTemplates = $bulkActionTemplates; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function renderBulkAction(GridViewInterface $gridView, Action $bulkAction, $data = null): string |
48
|
|
|
{ |
49
|
|
|
$type = $bulkAction->getType(); |
50
|
|
|
if (!isset($this->bulkActionTemplates[$type])) { |
51
|
|
|
throw new \InvalidArgumentException(sprintf('Missing template for bulk action type "%s".', $type)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$options = $this->optionsParser->parseOptions( |
55
|
|
|
$bulkAction->getOptions(), |
56
|
|
|
$gridView->getRequestConfiguration()->getRequest(), |
|
|
|
|
57
|
|
|
$data |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
return (string) $this->twig->render($this->bulkActionTemplates[$type], [ |
61
|
|
|
'grid' => $gridView, |
62
|
|
|
'action' => $bulkAction, |
63
|
|
|
'data' => $data, |
64
|
|
|
'options' => $options, |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
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: