11
11
12
12
namespace Translation \Bundle \Controller ;
13
13
14
- use Symfony \Bundle \ FrameworkBundle \ Controller \ AbstractController ;
14
+ use Symfony \Component \ HttpFoundation \ RedirectResponse ;
15
15
use Symfony \Component \HttpFoundation \Request ;
16
16
use Symfony \Component \HttpFoundation \Response ;
17
+ use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
17
18
use Symfony \Component \HttpKernel \Profiler \Profiler ;
19
+ use Symfony \Component \Routing \Exception \RouteNotFoundException ;
20
+ use Symfony \Component \Routing \RouterInterface ;
18
21
use Symfony \Component \Translation \DataCollector \TranslationDataCollector ;
19
22
use Symfony \Component \Translation \DataCollectorTranslator ;
20
23
use Symfony \Component \VarDumper \Cloner \Data ;
21
24
use Translation \Bundle \Model \SfProfilerMessage ;
22
25
use Translation \Bundle \Service \StorageService ;
23
26
use Translation \Common \Model \MessageInterface ;
27
+ use Twig \Environment ;
24
28
25
29
/**
26
30
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
27
31
*/
28
- class SymfonyProfilerController extends AbstractController
32
+ class SymfonyProfilerController
29
33
{
30
- private $ storage ;
34
+ /**
35
+ * @var Profiler An optional dependency
36
+ */
31
37
private $ profiler ;
38
+ private $ storage ;
39
+ private $ twig ;
40
+ private $ router ;
32
41
private $ isToolbarAllowEdit ;
33
42
34
- public function __construct (StorageService $ storage , Profiler $ profiler , bool $ isToolbarAllowEdit )
43
+ public function __construct (StorageService $ storage , Environment $ twig , RouterInterface $ router , bool $ isToolbarAllowEdit )
35
44
{
36
45
$ this ->storage = $ storage ;
37
- $ this ->profiler = $ profiler ;
46
+ $ this ->twig = $ twig ;
47
+ $ this ->router = $ router ;
38
48
$ this ->isToolbarAllowEdit = $ isToolbarAllowEdit ;
39
49
}
40
50
@@ -45,18 +55,20 @@ public function editAction(Request $request, string $token): Response
45
55
}
46
56
47
57
if (!$ request ->isXmlHttpRequest ()) {
48
- return $ this ->redirectToRoute ( ' _profiler ' , [ ' token ' => $ token] );
58
+ return $ this ->redirectToProfiler ( $ token );
49
59
}
50
60
51
61
$ message = $ this ->getMessage ($ request , $ token );
52
62
53
63
if ($ request ->isMethod ('GET ' )) {
54
64
$ translation = $ this ->storage ->syncAndFetchMessage ($ message ->getLocale (), $ message ->getDomain (), $ message ->getKey ());
55
65
56
- return $ this ->render ('@Translation/SymfonyProfiler/edit.html.twig ' , [
66
+ $ content = $ this -> twig ->render ('@Translation/SymfonyProfiler/edit.html.twig ' , [
57
67
'message ' => $ translation ,
58
68
'key ' => $ request ->query ->get ('message_id ' ),
59
69
]);
70
+
71
+ return new Response ($ content );
60
72
}
61
73
62
74
//Assert: This is a POST request
@@ -69,7 +81,7 @@ public function editAction(Request $request, string $token): Response
69
81
public function syncAction (Request $ request , string $ token ): Response
70
82
{
71
83
if (!$ request ->isXmlHttpRequest ()) {
72
- return $ this ->redirectToRoute ( ' _profiler ' , [ ' token ' => $ token] );
84
+ return $ this ->redirectToProfiler ( $ token );
73
85
}
74
86
75
87
$ sfMessage = $ this ->getMessage ($ request , $ token );
@@ -88,7 +100,7 @@ public function syncAction(Request $request, string $token): Response
88
100
public function syncAllAction (Request $ request , string $ token ): Response
89
101
{
90
102
if (!$ request ->isXmlHttpRequest ()) {
91
- return $ this ->redirectToRoute ( ' _profiler ' , [ ' token ' => $ token] );
103
+ return $ this ->redirectToProfiler ( $ token );
92
104
}
93
105
94
106
$ this ->storage ->sync ();
@@ -104,7 +116,7 @@ public function syncAllAction(Request $request, string $token): Response
104
116
public function createAssetsAction (Request $ request , string $ token ): Response
105
117
{
106
118
if (!$ request ->isXmlHttpRequest ()) {
107
- return $ this ->redirectToRoute ( ' _profiler ' , [ ' token ' => $ token] );
119
+ return $ this ->redirectToProfiler ( $ token );
108
120
}
109
121
110
122
$ messages = $ this ->getSelectedMessages ($ request , $ token );
@@ -123,20 +135,20 @@ public function createAssetsAction(Request $request, string $token): Response
123
135
124
136
private function getMessage (Request $ request , string $ token ): SfProfilerMessage
125
137
{
126
- $ this ->profiler ->disable ();
138
+ $ this ->getProfiler () ->disable ();
127
139
128
140
$ messageId = (string ) $ request ->request ->get ('message_id ' , $ request ->query ->get ('message_id ' ));
129
141
130
142
$ collectorMessages = $ this ->getMessages ($ token );
131
143
132
144
if (!isset ($ collectorMessages [$ messageId ])) {
133
- throw $ this -> createNotFoundException (\sprintf ('No message with key "%s" was found. ' , $ messageId ));
145
+ throw new NotFoundHttpException (\sprintf ('No message with key "%s" was found. ' , $ messageId ));
134
146
}
135
147
$ message = SfProfilerMessage::create ($ collectorMessages [$ messageId ]);
136
148
137
149
if (DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK === $ message ->getState ()) {
138
150
/** @var \Symfony\Component\HttpKernel\DataCollector\RequestDataCollector */
139
- $ requestCollector = $ this ->profiler ->loadProfile ($ token )->getCollector ('request ' );
151
+ $ requestCollector = $ this ->getProfiler () ->loadProfile ($ token )->getCollector ('request ' );
140
152
141
153
$ message
142
154
->setLocale ($ requestCollector ->getLocale ())
@@ -152,7 +164,7 @@ private function getMessage(Request $request, string $token): SfProfilerMessage
152
164
*/
153
165
protected function getSelectedMessages (Request $ request , string $ token ): array
154
166
{
155
- $ this ->profiler ->disable ();
167
+ $ this ->getProfiler () ->disable ();
156
168
157
169
/** @var string[] $selected */
158
170
$ selected = (array ) $ request ->request ->get ('selected ' );
@@ -172,13 +184,13 @@ protected function getSelectedMessages(Request $request, string $token): array
172
184
173
185
private function getMessages (string $ token , string $ profileName = 'translation ' ): array
174
186
{
175
- $ profile = $ this ->profiler ->loadProfile ($ token );
187
+ $ profile = $ this ->getProfiler () ->loadProfile ($ token );
176
188
177
189
if (null === $ dataCollector = $ profile ->getCollector ($ profileName )) {
178
- throw $ this -> createNotFoundException ("No collector with name \"$ profileName \" was found. " );
190
+ throw new NotFoundHttpException ("No collector with name \"$ profileName \" was found. " );
179
191
}
180
192
if (!$ dataCollector instanceof TranslationDataCollector) {
181
- throw $ this -> createNotFoundException ("Collector with name \"$ profileName \" is not an instance of TranslationDataCollector. " );
193
+ throw new NotFoundHttpException ("Collector with name \"$ profileName \" is not an instance of TranslationDataCollector. " );
182
194
}
183
195
184
196
$ messages = $ dataCollector ->getMessages ();
@@ -189,4 +201,29 @@ private function getMessages(string $token, string $profileName = 'translation')
189
201
190
202
return $ messages ;
191
203
}
204
+
205
+ public function setProfiler (Profiler $ profiler ): void
206
+ {
207
+ $ this ->profiler = $ profiler ;
208
+ }
209
+
210
+ private function getProfiler (): Profiler
211
+ {
212
+ if (!$ this ->profiler ) {
213
+ throw new \Exception ('The "profiler" service is missing. Please, run "composer require symfony/web-profiler-bundle" first to use this feature. ' );
214
+ }
215
+
216
+ return $ this ->profiler ;
217
+ }
218
+
219
+ private function redirectToProfiler (string $ token ): RedirectResponse
220
+ {
221
+ try {
222
+ $ targetUrl = $ this ->router ->generate ('_profiler ' , ['token ' => $ token ]);
223
+
224
+ return new RedirectResponse ($ targetUrl );
225
+ } catch (RouteNotFoundException $ e ) {
226
+ throw new \Exception ('Route to profiler page not found. Please, run "composer require symfony/web-profiler-bundle" first to use this feature. ' );
227
+ }
228
+ }
192
229
}
0 commit comments