1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHP Translation package. |
5
|
|
|
* |
6
|
|
|
* (c) PHP Translation team <[email protected]> |
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
|
|
|
namespace Translation\Bundle\Controller; |
13
|
|
|
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
use Symfony\Component\HttpKernel\Profiler\Profiler; |
18
|
|
|
use Symfony\Component\Translation\DataCollectorTranslator; |
19
|
|
|
use Symfony\Component\VarDumper\Cloner\Data; |
20
|
|
|
use Translation\Bundle\Model\SfProfilerMessage; |
21
|
|
|
use Translation\Bundle\Service\StorageService; |
22
|
|
|
use Translation\Common\Model\MessageInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Tobias Nyholm <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class SymfonyProfilerController extends AbstractController |
28
|
|
|
{ |
29
|
|
|
private $storage; |
30
|
|
|
private $profiler; |
31
|
|
|
private $isToolbarAllowEdit; |
32
|
|
|
|
33
|
|
|
public function __construct(StorageService $storage, Profiler $profiler, bool $isToolbarAllowEdit) |
34
|
|
|
{ |
35
|
|
|
$this->storage = $storage; |
36
|
|
|
$this->profiler = $profiler; |
37
|
|
|
$this->isToolbarAllowEdit = $isToolbarAllowEdit; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function editAction(Request $request, string $token): Response |
41
|
|
|
{ |
42
|
|
|
if (!$this->isToolbarAllowEdit) { |
43
|
|
|
return new Response('You are not allowed to edit the translations.'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (!$request->isXmlHttpRequest()) { |
47
|
|
|
return $this->redirectToRoute('_profiler', ['token' => $token]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$message = $this->getMessage($request, $token); |
51
|
|
|
|
52
|
|
|
if ($request->isMethod('GET')) { |
53
|
|
|
$translation = $this->storage->syncAndFetchMessage($message->getLocale(), $message->getDomain(), $message->getKey()); |
54
|
|
|
|
55
|
|
|
return $this->render('@Translation/SymfonyProfiler/edit.html.twig', [ |
56
|
|
|
'message' => $translation, |
57
|
|
|
'key' => $request->query->get('message_id'), |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
//Assert: This is a POST request |
62
|
|
|
$message->setTranslation($request->request->get('translation')); |
63
|
|
|
$this->storage->update($message->convertToMessage()); |
64
|
|
|
|
65
|
|
|
return new Response($message->getTranslation()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function syncAction(Request $request, string $token): Response |
69
|
|
|
{ |
70
|
|
|
if (!$request->isXmlHttpRequest()) { |
71
|
|
|
return $this->redirectToRoute('_profiler', ['token' => $token]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$sfMessage = $this->getMessage($request, $token); |
75
|
|
|
$message = $this->storage->syncAndFetchMessage($sfMessage->getLocale(), $sfMessage->getDomain(), $sfMessage->getKey()); |
76
|
|
|
|
77
|
|
|
if (null !== $message) { |
78
|
|
|
return new Response($message->getTranslation()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return new Response('Asset not found', 404); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response |
86
|
|
|
*/ |
87
|
|
|
public function syncAllAction(Request $request, string $token): Response |
88
|
|
|
{ |
89
|
|
|
if (!$request->isXmlHttpRequest()) { |
90
|
|
|
return $this->redirectToRoute('_profiler', ['token' => $token]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->storage->sync(); |
94
|
|
|
|
95
|
|
|
return new Response('Started synchronization of all translations'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Save the selected translation to resources. |
100
|
|
|
* |
101
|
|
|
* @author Damien Alexandre (damienalexandre) |
102
|
|
|
*/ |
103
|
|
|
public function createAssetsAction(Request $request, string $token): Response |
104
|
|
|
{ |
105
|
|
|
if (!$request->isXmlHttpRequest()) { |
106
|
|
|
return $this->redirectToRoute('_profiler', ['token' => $token]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$messages = $this->getSelectedMessages($request, $token); |
110
|
|
|
if (empty($messages)) { |
111
|
|
|
return new Response('No translations selected.'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$uploaded = []; |
115
|
|
|
foreach ($messages as $message) { |
116
|
|
|
$this->storage->create($message); |
117
|
|
|
$uploaded[] = $message; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return new Response(\sprintf('%s new assets created!', \count($uploaded))); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
private function getMessage(Request $request, string $token): SfProfilerMessage |
124
|
|
|
{ |
125
|
|
|
$profiler = $this->get('profiler'); |
126
|
|
|
$profiler->disable(); |
127
|
|
|
|
128
|
|
|
$messageId = $request->request->get('message_id', $request->query->get('message_id')); |
129
|
|
|
|
130
|
|
|
$profile = $profiler->loadProfile($token); |
131
|
|
|
if (null === $dataCollector = $profile->getCollector('translation')) { |
132
|
|
|
throw $this->createNotFoundException('No collector with name "translation" was found.'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$collectorMessages = $dataCollector->getMessages(); |
136
|
|
|
|
137
|
|
|
if ($collectorMessages instanceof Data) { |
138
|
|
|
$collectorMessages = $collectorMessages->getValue(true); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if (!isset($collectorMessages[$messageId])) { |
142
|
|
|
throw $this->createNotFoundException(\sprintf('No message with key "%s" was found.', $messageId)); |
143
|
|
|
} |
144
|
|
|
$message = SfProfilerMessage::create($collectorMessages[$messageId]); |
145
|
|
|
|
146
|
|
|
if (DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK === $message->getState()) { |
147
|
|
|
$message->setLocale($profile->getCollector('request')->getLocale()) |
148
|
|
|
->setTranslation(\sprintf('[%s]', $message->getTranslation())); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $message; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return MessageInterface[] |
156
|
|
|
*/ |
157
|
|
|
protected function getSelectedMessages(Request $request, string $token): array |
158
|
|
|
{ |
159
|
|
|
$profiler = $this->get('profiler'); |
160
|
|
|
$profiler->disable(); |
161
|
|
|
|
162
|
|
|
$selected = $request->request->get('selected'); |
163
|
|
|
if (!$selected || 0 == \count($selected)) { |
164
|
|
|
return []; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$profile = $profiler->loadProfile($token); |
168
|
|
|
$dataCollector = $profile->getCollector('translation'); |
169
|
|
|
$messages = $dataCollector->getMessages(); |
170
|
|
|
|
171
|
|
|
if ($messages instanceof Data) { |
172
|
|
|
$messages = $messages->getValue(true); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$toSave = \array_intersect_key($messages, \array_flip($selected)); |
176
|
|
|
|
177
|
|
|
$messages = []; |
178
|
|
|
foreach ($toSave as $data) { |
179
|
|
|
$messages[] = SfProfilerMessage::create($data)->convertToMessage(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $messages; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|