11
11
12
12
namespace Translation \Bundle \Controller ;
13
13
14
- use Symfony \Bundle \FrameworkBundle \Controller \Controller ;
14
+ use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
15
15
use Symfony \Component \HttpFoundation \Request ;
16
16
use Symfony \Component \HttpFoundation \Response ;
17
17
use Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
18
18
use Symfony \Component \Intl \Intl ;
19
19
use Symfony \Component \Intl \Locales ;
20
20
use Symfony \Component \Translation \MessageCatalogue ;
21
+ use Symfony \Component \Validator \Validator \ValidatorInterface ;
22
+ use Translation \Bundle \Catalogue \CatalogueFetcher ;
23
+ use Translation \Bundle \Catalogue \CatalogueManager ;
21
24
use Translation \Bundle \Exception \MessageValidationException ;
22
25
use Translation \Bundle \Model \CatalogueMessage ;
26
+ use Translation \Bundle \Service \ConfigurationManager ;
27
+ use Translation \Bundle \Service \StorageManager ;
23
28
use Translation \Bundle \Service \StorageService ;
24
29
use Translation \Common \Exception \StorageException ;
25
30
use Translation \Common \Model \Message ;
28
33
/**
29
34
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
30
35
*/
31
- class WebUIController extends Controller
36
+ class WebUIController extends AbstractController
32
37
{
38
+ private $ configurationManager ;
39
+ private $ catalogueFetcher ;
40
+ private $ catalogueManager ;
41
+ private $ storageManager ;
42
+ private $ validator ;
43
+ private $ locales ;
44
+ private $ isWebUIEnabled ;
45
+ private $ isWebUIAllowCreate ;
46
+ private $ isWebUIAllowDelete ;
47
+ private $ fileBasePath ;
48
+
49
+ public function __construct (
50
+ ConfigurationManager $ configurationManager ,
51
+ CatalogueFetcher $ catalogueFetcher ,
52
+ CatalogueManager $ catalogueManager ,
53
+ StorageManager $ storageManager ,
54
+ ValidatorInterface $ validator ,
55
+ array $ locales ,
56
+ bool $ isWebUIEnabled ,
57
+ bool $ isWebUIAllowCreate ,
58
+ bool $ isWebUIAllowDelete ,
59
+ string $ fileBasePath
60
+ ) {
61
+ $ this ->configurationManager = $ configurationManager ;
62
+ $ this ->catalogueFetcher = $ catalogueFetcher ;
63
+ $ this ->catalogueManager = $ catalogueManager ;
64
+ $ this ->storageManager = $ storageManager ;
65
+ $ this ->validator = $ validator ;
66
+ $ this ->locales = $ locales ;
67
+ $ this ->isWebUIEnabled = $ isWebUIEnabled ;
68
+ $ this ->isWebUIAllowCreate = $ isWebUIAllowCreate ;
69
+ $ this ->isWebUIAllowDelete = $ isWebUIAllowDelete ;
70
+ $ this ->fileBasePath = $ fileBasePath ;
71
+ }
72
+
33
73
/**
34
74
* Show a dashboard for the configuration.
35
75
*/
36
76
public function indexAction (?string $ configName = null ): Response
37
77
{
38
- if (!$ this ->getParameter ( ' php_translation.webui.enabled ' ) ) {
78
+ if (!$ this ->isWebUIEnabled ) {
39
79
return new Response ('You are not allowed here. Check you config. ' , 400 );
40
80
}
41
81
42
- $ configManager = $ this ->get ('php_translation.configuration_manager ' );
43
- $ config = $ configManager ->getConfiguration ($ configName );
82
+ $ config = $ this ->configurationManager ->getConfiguration ($ configName );
44
83
$ localeMap = $ this ->getLocale2LanguageMap ();
45
- $ catalogues = $ this ->get ( ' php_translation.catalogue_fetcher ' ) ->getCatalogues ($ config );
84
+ $ catalogues = $ this ->catalogueFetcher ->getCatalogues ($ config );
46
85
47
86
$ catalogueSize = [];
48
87
$ maxDomainSize = [];
@@ -75,7 +114,7 @@ public function indexAction(?string $configName = null): Response
75
114
'maxCatalogueSize ' => $ maxCatalogueSize ,
76
115
'localeMap ' => $ localeMap ,
77
116
'configName ' => $ config ->getName (),
78
- 'configNames ' => $ configManager ->getNames (),
117
+ 'configNames ' => $ this -> configurationManager ->getNames (),
79
118
]);
80
119
}
81
120
@@ -84,44 +123,42 @@ public function indexAction(?string $configName = null): Response
84
123
*/
85
124
public function showAction (string $ configName , string $ locale , string $ domain ): Response
86
125
{
87
- if (!$ this ->getParameter ( ' php_translation.webui.enabled ' ) ) {
126
+ if (!$ this ->isWebUIEnabled ) {
88
127
return new Response ('You are not allowed here. Check you config. ' , 400 );
89
128
}
90
- $ configManager = $ this ->get ('php_translation.configuration_manager ' );
91
- $ config = $ configManager ->getConfiguration ($ configName );
129
+ $ config = $ this ->configurationManager ->getConfiguration ($ configName );
92
130
93
131
// Get a catalogue manager and load it with all the catalogues
94
- $ catalogueManager = $ this ->get ('php_translation.catalogue_manager ' );
95
- $ catalogueManager ->load ($ this ->get ('php_translation.catalogue_fetcher ' )->getCatalogues ($ config ));
132
+ $ this ->catalogueManager ->load ($ this ->catalogueFetcher ->getCatalogues ($ config ));
96
133
97
134
/** @var CatalogueMessage[] $messages */
98
- $ messages = $ catalogueManager ->getMessages ($ locale , $ domain );
135
+ $ messages = $ this -> catalogueManager ->getMessages ($ locale , $ domain );
99
136
\usort ($ messages , function (CatalogueMessage $ a , CatalogueMessage $ b ) {
100
137
return \strcmp ($ a ->getKey (), $ b ->getKey ());
101
138
});
102
139
103
140
return $ this ->render ('@Translation/WebUI/show.html.twig ' , [
104
141
'messages ' => $ messages ,
105
- 'domains ' => $ catalogueManager ->getDomains (),
142
+ 'domains ' => $ this -> catalogueManager ->getDomains (),
106
143
'currentDomain ' => $ domain ,
107
- 'locales ' => $ this ->getParameter ( ' php_translation. locales' ) ,
144
+ 'locales ' => $ this ->locales ,
108
145
'currentLocale ' => $ locale ,
109
146
'configName ' => $ config ->getName (),
110
- 'configNames ' => $ configManager ->getNames (),
111
- 'allow_create ' => $ this ->getParameter ( ' php_translation.webui.allow_create ' ) ,
112
- 'allow_delete ' => $ this ->getParameter ( ' php_translation.webui.allow_delete ' ) ,
113
- 'file_base_path ' => $ this ->getParameter ( ' php_translation.webui.file_base_path ' ) ,
147
+ 'configNames ' => $ this -> configurationManager ->getNames (),
148
+ 'allow_create ' => $ this ->isWebUIAllowCreate ,
149
+ 'allow_delete ' => $ this ->isWebUIAllowDelete ,
150
+ 'file_base_path ' => $ this ->fileBasePath ,
114
151
]);
115
152
}
116
153
117
154
public function createAction (Request $ request , string $ configName , string $ locale , string $ domain ): Response
118
155
{
119
- if (!$ this ->getParameter ( ' php_translation.webui.enabled ' ) || !$ this ->getParameter ( ' php_translation.webui.allow_create ' ) ) {
156
+ if (!$ this ->isWebUIEnabled || !$ this ->isWebUIAllowCreate ) {
120
157
return new Response ('You are not allowed to create. Check you config. ' , 400 );
121
158
}
122
159
123
160
/** @var StorageService $storage */
124
- $ storage = $ this ->get ( ' php_translation.storage_manager ' ) ->getStorage ($ configName );
161
+ $ storage = $ this ->storageManager ->getStorage ($ configName );
125
162
126
163
try {
127
164
$ message = $ this ->getMessageFromRequest ($ request );
@@ -145,7 +182,7 @@ public function createAction(Request $request, string $configName, string $local
145
182
146
183
public function editAction (Request $ request , string $ configName , string $ locale , string $ domain ): Response
147
184
{
148
- if (!$ this ->getParameter ( ' php_translation.webui.enabled ' ) ) {
185
+ if (!$ this ->isWebUIEnabled ) {
149
186
return new Response ('You are not allowed here. Check you config. ' , 400 );
150
187
}
151
188
@@ -159,15 +196,15 @@ public function editAction(Request $request, string $configName, string $locale,
159
196
}
160
197
161
198
/** @var StorageService $storage */
162
- $ storage = $ this ->get ( ' php_translation.storage_manager ' ) ->getStorage ($ configName );
199
+ $ storage = $ this ->storageManager ->getStorage ($ configName );
163
200
$ storage ->update ($ message );
164
201
165
202
return new Response ('Translation updated ' );
166
203
}
167
204
168
205
public function deleteAction (Request $ request , string $ configName , string $ locale , string $ domain ): Response
169
206
{
170
- if (!$ this ->getParameter ( ' php_translation.webui.enabled ' ) || !$ this ->getParameter ( ' php_translation.webui.allow_delete ' ) ) {
207
+ if (!$ this ->isWebUIEnabled || !$ this ->isWebUIAllowDelete ) {
171
208
return new Response ('You are not allowed to create. Check you config. ' , 400 );
172
209
}
173
210
@@ -181,7 +218,7 @@ public function deleteAction(Request $request, string $configName, string $local
181
218
}
182
219
183
220
/** @var StorageService $storage */
184
- $ storage = $ this ->get ( ' php_translation.storage_manager ' ) ->getStorage ($ configName );
221
+ $ storage = $ this ->storageManager ->getStorage ($ configName );
185
222
$ storage ->delete ($ locale , $ domain , $ message ->getKey ());
186
223
187
224
return new Response ('Message was deleted ' );
@@ -206,12 +243,11 @@ private function getMessageFromRequest(Request $request): Message
206
243
*/
207
244
private function getLocale2LanguageMap (): array
208
245
{
209
- $ configuredLocales = $ this ->getParameter ('php_translation.locales ' );
210
246
$ names = \class_exists (Locales::class)
211
247
? Locales::getNames ('en ' )
212
248
: Intl::getLocaleBundle ()->getLocaleNames ('en ' );
213
249
$ map = [];
214
- foreach ($ configuredLocales as $ l ) {
250
+ foreach ($ this -> locales as $ l ) {
215
251
$ map [$ l ] = $ names [$ l ] ?? $ l ;
216
252
}
217
253
@@ -223,7 +259,7 @@ private function getLocale2LanguageMap(): array
223
259
*/
224
260
private function validateMessage (MessageInterface $ message , array $ validationGroups ): void
225
261
{
226
- $ errors = $ this ->get ( ' validator ' ) ->validate ($ message , null , $ validationGroups );
262
+ $ errors = $ this ->validator ->validate ($ message , null , $ validationGroups );
227
263
if (\count ($ errors ) > 0 ) {
228
264
throw MessageValidationException::create ();
229
265
}
0 commit comments