1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\MediaBundle\Twig\Extension; |
15
|
|
|
|
16
|
|
|
use Sonata\MediaBundle\Model\MediaInterface; |
17
|
|
|
use Sonata\MediaBundle\Twig\TokenParser\MediaTokenParser; |
18
|
|
|
use Sonata\MediaBundle\Twig\TokenParser\PathTokenParser; |
19
|
|
|
use Sonata\MediaBundle\Twig\TokenParser\ThumbnailTokenParser; |
20
|
|
|
use Twig\Extension\AbstractExtension; |
21
|
|
|
use Twig\Extension\ExtensionInterface; |
22
|
|
|
|
23
|
|
|
class FormatterMediaExtension extends AbstractExtension implements ExtensionInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var ExtensionInterface |
27
|
|
|
*/ |
28
|
|
|
protected $twigExtension; |
29
|
|
|
|
30
|
|
|
public function __construct(ExtensionInterface $twigExtension) |
31
|
|
|
{ |
32
|
|
|
$this->twigExtension = $twigExtension; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function getAllowedTags(): array |
39
|
|
|
{ |
40
|
|
|
return [ |
41
|
|
|
'media', |
42
|
|
|
'path', |
43
|
|
|
'thumbnail', |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function getAllowedMethods(): array |
51
|
|
|
{ |
52
|
|
|
return [ |
53
|
|
|
MediaInterface::class => [ |
54
|
|
|
'getproviderreference', |
55
|
|
|
], |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function getTokenParsers(): array |
63
|
|
|
{ |
64
|
|
|
return [ |
65
|
|
|
new MediaTokenParser(__CLASS__), |
66
|
|
|
new ThumbnailTokenParser(__CLASS__), |
67
|
|
|
new PathTokenParser(__CLASS__), |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return ExtensionInterface |
73
|
|
|
*/ |
74
|
|
|
public function getTwigExtension(): ExtensionInterface |
75
|
|
|
{ |
76
|
|
|
return $this->twigExtension; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param int $media |
81
|
|
|
* @param string $format |
82
|
|
|
* @param array $options |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function media($media, $format, $options = []) |
87
|
|
|
{ |
88
|
|
|
return $this->getTwigExtension()->media($media, $format, $options); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param int $media |
93
|
|
|
* @param string $format |
94
|
|
|
* @param array $options |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function thumbnail($media, $format, $options = []) |
99
|
|
|
{ |
100
|
|
|
return $this->getTwigExtension()->thumbnail($media, $format, $options); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param int $media |
105
|
|
|
* @param string $format |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function path($media, $format) |
110
|
|
|
{ |
111
|
|
|
return $this->getTwigExtension()->path($media, $format); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getNodeVisitors() |
115
|
|
|
{ |
116
|
|
|
return $this->getTwigExtension()->getNodeVisitors(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getFilters() |
120
|
|
|
{ |
121
|
|
|
return $this->getTwigExtension()->getFilters(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getTests() |
125
|
|
|
{ |
126
|
|
|
return $this->getTwigExtension()->getTests(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function getFunctions() |
130
|
|
|
{ |
131
|
|
|
return $this->getTwigExtension()->getFunctions(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function getOperators() |
135
|
|
|
{ |
136
|
|
|
return $this->getTwigExtension()->getOperators(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function getAllowedFilters() |
140
|
|
|
{ |
141
|
|
|
return []; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function getAllowedFunctions() |
145
|
|
|
{ |
146
|
|
|
return []; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function getAllowedProperties() |
150
|
|
|
{ |
151
|
|
|
return []; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
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: