|
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\AdminBundle\Translator\Extractor\JMSTranslatorBundle; |
|
15
|
|
|
|
|
16
|
|
|
use JMS\TranslationBundle\Model\FileSource; |
|
17
|
|
|
use JMS\TranslationBundle\Model\Message; |
|
18
|
|
|
use JMS\TranslationBundle\Model\MessageCatalogue; |
|
19
|
|
|
use JMS\TranslationBundle\Translation\ExtractorInterface; |
|
20
|
|
|
use Psr\Log\LoggerInterface; |
|
21
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
|
22
|
|
|
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface; |
|
23
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
|
24
|
|
|
use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface; |
|
25
|
|
|
use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface; |
|
26
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* NEXT_MAJOR: Remove this class and the jms/translation-bundle dev dependency. |
|
30
|
|
|
* |
|
31
|
|
|
* @deprecated since sonata-project/admin-bundle 3.72. Use `translation:update` Symfony command instead. |
|
32
|
|
|
*/ |
|
33
|
|
|
final class AdminExtractor implements ExtractorInterface, TranslatorInterface, SecurityHandlerInterface, LabelTranslatorStrategyInterface |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var LoggerInterface|null |
|
37
|
|
|
*/ |
|
38
|
|
|
private $logger; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var Pool |
|
42
|
|
|
*/ |
|
43
|
|
|
private $adminPool; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var MessageCatalogue|bool |
|
47
|
|
|
*/ |
|
48
|
|
|
private $catalogue; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var TranslatorInterface|null |
|
52
|
|
|
*/ |
|
53
|
|
|
private $translator; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var LabelTranslatorStrategyInterface|bool |
|
57
|
|
|
*/ |
|
58
|
|
|
private $labelStrategy; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var string|bool |
|
62
|
|
|
*/ |
|
63
|
|
|
private $domain; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var BreadcrumbsBuilderInterface |
|
67
|
|
|
*/ |
|
68
|
|
|
private $breadcrumbsBuilder; |
|
69
|
|
|
|
|
70
|
|
|
public function __construct( |
|
71
|
|
|
Pool $adminPool, |
|
72
|
|
|
?TranslatorInterface $translator, |
|
73
|
|
|
?LoggerInterface $logger = null |
|
74
|
|
|
) { |
|
75
|
|
|
$this->logger = $logger; |
|
76
|
|
|
$this->adminPool = $adminPool; |
|
77
|
|
|
$this->translator = $translator; |
|
78
|
|
|
|
|
79
|
|
|
// state variable |
|
80
|
|
|
$this->catalogue = false; |
|
81
|
|
|
$this->labelStrategy = false; |
|
82
|
|
|
$this->domain = false; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function setLogger(LoggerInterface $logger): void |
|
86
|
|
|
{ |
|
87
|
|
|
$this->logger = $logger; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* NEXT_MAJOR : use a constructor argument instead. |
|
92
|
|
|
*/ |
|
93
|
|
|
public function setBreadcrumbsBuilder(BreadcrumbsBuilderInterface $breadcrumbsBuilder): void |
|
94
|
|
|
{ |
|
95
|
|
|
$this->breadcrumbsBuilder = $breadcrumbsBuilder; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Extract messages to MessageCatalogue. |
|
100
|
|
|
* |
|
101
|
|
|
* @throws \Exception|\RuntimeException |
|
102
|
|
|
* |
|
103
|
|
|
* @return MessageCatalogue |
|
104
|
|
|
*/ |
|
105
|
|
|
public function extract() |
|
106
|
|
|
{ |
|
107
|
|
|
if ($this->catalogue) { |
|
108
|
|
|
throw new \RuntimeException('Invalid state'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$this->catalogue = new MessageCatalogue(); |
|
112
|
|
|
|
|
113
|
|
|
foreach ($this->adminPool->getAdminGroups() as $name => $group) { |
|
114
|
|
|
$this->trans($name, [], $group['label_catalogue']); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
foreach ($this->adminPool->getAdminServiceIds() as $id) { |
|
118
|
|
|
$admin = $this->getAdmin($id); |
|
119
|
|
|
|
|
120
|
|
|
$label = $admin->getLabel(); |
|
121
|
|
|
if (!empty($label)) { |
|
122
|
|
|
$this->trans($label, [], $admin->getTranslationDomain()); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$this->labelStrategy = $admin->getLabelTranslatorStrategy(); |
|
126
|
|
|
$this->domain = $admin->getTranslationDomain(); |
|
127
|
|
|
|
|
128
|
|
|
$admin->setTranslator($this); |
|
129
|
|
|
$admin->setSecurityHandler($this); |
|
130
|
|
|
$admin->setLabelTranslatorStrategy($this); |
|
131
|
|
|
|
|
132
|
|
|
// call the different public method |
|
133
|
|
|
$methods = [ |
|
134
|
|
|
'getShow', |
|
135
|
|
|
'getDatagrid', |
|
136
|
|
|
'getList', |
|
137
|
|
|
'getForm', |
|
138
|
|
|
]; |
|
139
|
|
|
|
|
140
|
|
|
$actions = [ |
|
141
|
|
|
'list', |
|
142
|
|
|
'edit', |
|
143
|
|
|
'create', |
|
144
|
|
|
'update', |
|
145
|
|
|
'batch', |
|
146
|
|
|
'delete', |
|
147
|
|
|
]; |
|
148
|
|
|
|
|
149
|
|
|
if ($this->logger) { |
|
150
|
|
|
$this->logger->info(sprintf( |
|
151
|
|
|
'Retrieving message from admin:%s - class: %s', |
|
152
|
|
|
$admin->getCode(), |
|
153
|
|
|
\get_class($admin) |
|
154
|
|
|
)); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
foreach ($methods as $method) { |
|
158
|
|
|
try { |
|
159
|
|
|
$admin->$method(); |
|
160
|
|
|
} catch (\Exception $e) { |
|
161
|
|
|
if ($this->logger) { |
|
162
|
|
|
$this->logger->error(sprintf( |
|
163
|
|
|
'ERROR : admin:%s - Raise an exception : %s', |
|
164
|
|
|
$admin->getCode(), |
|
165
|
|
|
$e->getMessage() |
|
166
|
|
|
)); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
throw $e; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
foreach ($actions as $action) { |
|
174
|
|
|
try { |
|
175
|
|
|
$this->breadcrumbsBuilder->getBreadcrumbs($admin, $action); |
|
176
|
|
|
} catch (\Exception $e) { |
|
177
|
|
|
if ($this->logger) { |
|
178
|
|
|
$this->logger->error( |
|
179
|
|
|
sprintf( |
|
180
|
|
|
'ERROR : admin:%s - Raises an exception : %s', |
|
181
|
|
|
$admin->getCode(), |
|
182
|
|
|
$e->getMessage() |
|
183
|
|
|
), |
|
184
|
|
|
['exception' => $e] |
|
185
|
|
|
); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
throw $e; |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$catalogue = $this->catalogue; |
|
194
|
|
|
$this->catalogue = false; |
|
195
|
|
|
|
|
196
|
|
|
return $catalogue; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function trans($id, array $parameters = [], $domain = null, $locale = null) |
|
200
|
|
|
{ |
|
201
|
|
|
$this->addMessage($id, $domain ?? ''); |
|
202
|
|
|
|
|
203
|
|
|
return $id; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
public function transChoice($id, $number, array $parameters = [], ?string $domain = null, ?string $locale = null) |
|
|
|
|
|
|
207
|
|
|
{ |
|
208
|
|
|
$this->addMessage($id, $domain); |
|
209
|
|
|
|
|
210
|
|
|
return $id; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
public function isGranted(AdminInterface $admin, $attributes, $object = null) |
|
214
|
|
|
{ |
|
215
|
|
|
return true; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
public function buildSecurityInformation(AdminInterface $admin): void |
|
219
|
|
|
{ |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
public function createObjectSecurity(AdminInterface $admin, $object): void |
|
223
|
|
|
{ |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
public function deleteObjectSecurity(AdminInterface $admin, $object): void |
|
227
|
|
|
{ |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
public function getBaseRole(AdminInterface $admin): void |
|
231
|
|
|
{ |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
public function getLabel($label, $context = '', $type = '') |
|
235
|
|
|
{ |
|
236
|
|
|
$label = $this->labelStrategy->getLabel($label, $context, $type); |
|
237
|
|
|
|
|
238
|
|
|
$this->addMessage($label, $this->domain); |
|
|
|
|
|
|
239
|
|
|
|
|
240
|
|
|
return $label; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
private function getAdmin(string $id): AdminInterface |
|
244
|
|
|
{ |
|
245
|
|
|
$admin = $this->adminPool->getContainer()->get($id); |
|
246
|
|
|
\assert($admin instanceof AdminInterface); |
|
247
|
|
|
|
|
248
|
|
|
return $admin; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
private function addMessage(string $id, string $domain): void |
|
252
|
|
|
{ |
|
253
|
|
|
$message = new Message($id, $domain); |
|
254
|
|
|
|
|
255
|
|
|
$trace = debug_backtrace(0); |
|
256
|
|
|
if (isset($trace[1]['file'])) { |
|
257
|
|
|
$message->addSource(new FileSource($trace[1]['file'])); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
$this->catalogue->add($message); |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.