Skip to content

Commit d82d940

Browse files
author
Olivier Dolbeau
committed
SF 5.x compatibility
1 parent ceccbde commit d82d940

35 files changed

+512
-133
lines changed

Controller/EditInPlaceController.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,33 @@
1111

1212
namespace Translation\Bundle\Controller;
1313

14-
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
14+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\Validator\Validator\ValidatorInterface;
1718
use Translation\Bundle\Exception\MessageValidationException;
19+
use Translation\Bundle\Service\CacheClearer;
20+
use Translation\Bundle\Service\StorageManager;
1821
use Translation\Bundle\Service\StorageService;
1922
use Translation\Common\Model\Message;
2023
use Translation\Common\Model\MessageInterface;
2124

2225
/**
2326
* @author Damien Alexandre <dalexandre@jolicode.com>
2427
*/
25-
class EditInPlaceController extends Controller
28+
class EditInPlaceController extends AbstractController
2629
{
30+
private $storageManager;
31+
private $cacheClearer;
32+
private $validator;
33+
34+
public function __construct(StorageManager $storageManager, CacheClearer $cacheClearer, ValidatorInterface $validator)
35+
{
36+
$this->storageManager = $storageManager;
37+
$this->cacheClearer = $cacheClearer;
38+
$this->validator = $validator;
39+
}
40+
2741
public function editAction(Request $request, string $configName, string $locale): Response
2842
{
2943
try {
@@ -33,13 +47,12 @@ public function editAction(Request $request, string $configName, string $locale)
3347
}
3448

3549
/** @var StorageService $storage */
36-
$storage = $this->get('php_translation.storage_manager')->getStorage($configName);
50+
$storage = $this->storageManager->getStorage($configName);
3751
foreach ($messages as $message) {
3852
$storage->update($message);
3953
}
4054

41-
$cacheClearer = $this->get('php_translation.cache_clearer');
42-
$cacheClearer->clearAndWarmUp($locale);
55+
$this->cacheClearer->clearAndWarmUp($locale);
4356

4457
return new Response();
4558
}
@@ -56,14 +69,13 @@ private function getMessages(Request $request, string $locale, array $validation
5669
$json = $request->getContent();
5770
$data = \json_decode($json, true);
5871
$messages = [];
59-
$validator = $this->get('validator');
6072

6173
foreach ($data as $key => $value) {
6274
[$domain, $translationKey] = \explode('|', $key);
6375

6476
$message = new Message($translationKey, $domain, $locale, $value);
6577

66-
$errors = $validator->validate($message, null, $validationGroups);
78+
$errors = $this->validator->validate($message, null, $validationGroups);
6779
if (\count($errors) > 0) {
6880
throw MessageValidationException::create();
6981
}

Controller/SymfonyProfilerController.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111

1212
namespace Translation\Bundle\Controller;
1313

14-
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
14+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\HttpKernel\Profiler\Profiler;
1718
use Symfony\Component\Translation\DataCollectorTranslator;
1819
use Symfony\Component\VarDumper\Cloner\Data;
1920
use Translation\Bundle\Model\SfProfilerMessage;
@@ -23,11 +24,22 @@
2324
/**
2425
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
2526
*/
26-
class SymfonyProfilerController extends Controller
27+
class SymfonyProfilerController extends AbstractController
2728
{
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+
2840
public function editAction(Request $request, string $token): Response
2941
{
30-
if (!$this->getParameter('php_translation.toolbar.allow_edit')) {
42+
if (!$this->isToolbarAllowEdit) {
3143
return new Response('You are not allowed to edit the translations.');
3244
}
3345

@@ -36,11 +48,9 @@ public function editAction(Request $request, string $token): Response
3648
}
3749

3850
$message = $this->getMessage($request, $token);
39-
/** @var StorageService $storage */
40-
$storage = $this->get('php_translation.storage');
4151

4252
if ($request->isMethod('GET')) {
43-
$translation = $storage->syncAndFetchMessage($message->getLocale(), $message->getDomain(), $message->getKey());
53+
$translation = $this->storage->syncAndFetchMessage($message->getLocale(), $message->getDomain(), $message->getKey());
4454

4555
return $this->render('@Translation/SymfonyProfiler/edit.html.twig', [
4656
'message' => $translation,
@@ -50,7 +60,7 @@ public function editAction(Request $request, string $token): Response
5060

5161
//Assert: This is a POST request
5262
$message->setTranslation($request->request->get('translation'));
53-
$storage->update($message->convertToMessage());
63+
$this->storage->update($message->convertToMessage());
5464

5565
return new Response($message->getTranslation());
5666
}
@@ -61,10 +71,8 @@ public function syncAction(Request $request, string $token): Response
6171
return $this->redirectToRoute('_profiler', ['token' => $token]);
6272
}
6373

64-
/** @var StorageService $storage */
65-
$storage = $this->get('php_translation.storage');
6674
$sfMessage = $this->getMessage($request, $token);
67-
$message = $storage->syncAndFetchMessage($sfMessage->getLocale(), $sfMessage->getDomain(), $sfMessage->getKey());
75+
$message = $this->storage->syncAndFetchMessage($sfMessage->getLocale(), $sfMessage->getDomain(), $sfMessage->getKey());
6876

6977
if (null !== $message) {
7078
return new Response($message->getTranslation());
@@ -82,9 +90,7 @@ public function syncAllAction(Request $request, string $token): Response
8290
return $this->redirectToRoute('_profiler', ['token' => $token]);
8391
}
8492

85-
/** @var StorageService $storage */
86-
$storage = $this->get('php_translation.storage');
87-
$storage->sync();
93+
$this->storage->sync();
8894

8995
return new Response('Started synchronization of all translations');
9096
}
@@ -106,10 +112,8 @@ public function createAssetsAction(Request $request, string $token): Response
106112
}
107113

108114
$uploaded = [];
109-
/** @var StorageService $storage */
110-
$storage = $this->get('php_translation.storage');
111115
foreach ($messages as $message) {
112-
$storage->create($message);
116+
$this->storage->create($message);
113117
$uploaded[] = $message;
114118
}
115119

Controller/WebUIController.php

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,20 @@
1111

1212
namespace Translation\Bundle\Controller;
1313

14-
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
14+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Response;
1717
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
1818
use Symfony\Component\Intl\Intl;
1919
use Symfony\Component\Intl\Locales;
2020
use Symfony\Component\Translation\MessageCatalogue;
21+
use Symfony\Component\Validator\Validator\ValidatorInterface;
22+
use Translation\Bundle\Catalogue\CatalogueFetcher;
23+
use Translation\Bundle\Catalogue\CatalogueManager;
2124
use Translation\Bundle\Exception\MessageValidationException;
2225
use Translation\Bundle\Model\CatalogueMessage;
26+
use Translation\Bundle\Service\ConfigurationManager;
27+
use Translation\Bundle\Service\StorageManager;
2328
use Translation\Bundle\Service\StorageService;
2429
use Translation\Common\Exception\StorageException;
2530
use Translation\Common\Model\Message;
@@ -28,21 +33,55 @@
2833
/**
2934
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
3035
*/
31-
class WebUIController extends Controller
36+
class WebUIController extends AbstractController
3237
{
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+
3373
/**
3474
* Show a dashboard for the configuration.
3575
*/
3676
public function indexAction(?string $configName = null): Response
3777
{
38-
if (!$this->getParameter('php_translation.webui.enabled')) {
78+
if (!$this->isWebUIEnabled) {
3979
return new Response('You are not allowed here. Check you config. ', 400);
4080
}
4181

42-
$configManager = $this->get('php_translation.configuration_manager');
43-
$config = $configManager->getConfiguration($configName);
82+
$config = $this->configurationManager->getConfiguration($configName);
4483
$localeMap = $this->getLocale2LanguageMap();
45-
$catalogues = $this->get('php_translation.catalogue_fetcher')->getCatalogues($config);
84+
$catalogues = $this->catalogueFetcher->getCatalogues($config);
4685

4786
$catalogueSize = [];
4887
$maxDomainSize = [];
@@ -75,7 +114,7 @@ public function indexAction(?string $configName = null): Response
75114
'maxCatalogueSize' => $maxCatalogueSize,
76115
'localeMap' => $localeMap,
77116
'configName' => $config->getName(),
78-
'configNames' => $configManager->getNames(),
117+
'configNames' => $this->configurationManager->getNames(),
79118
]);
80119
}
81120

@@ -84,44 +123,42 @@ public function indexAction(?string $configName = null): Response
84123
*/
85124
public function showAction(string $configName, string $locale, string $domain): Response
86125
{
87-
if (!$this->getParameter('php_translation.webui.enabled')) {
126+
if (!$this->isWebUIEnabled) {
88127
return new Response('You are not allowed here. Check you config. ', 400);
89128
}
90-
$configManager = $this->get('php_translation.configuration_manager');
91-
$config = $configManager->getConfiguration($configName);
129+
$config = $this->configurationManager->getConfiguration($configName);
92130

93131
// 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));
96133

97134
/** @var CatalogueMessage[] $messages */
98-
$messages = $catalogueManager->getMessages($locale, $domain);
135+
$messages = $this->catalogueManager->getMessages($locale, $domain);
99136
\usort($messages, function (CatalogueMessage $a, CatalogueMessage $b) {
100137
return \strcmp($a->getKey(), $b->getKey());
101138
});
102139

103140
return $this->render('@Translation/WebUI/show.html.twig', [
104141
'messages' => $messages,
105-
'domains' => $catalogueManager->getDomains(),
142+
'domains' => $this->catalogueManager->getDomains(),
106143
'currentDomain' => $domain,
107-
'locales' => $this->getParameter('php_translation.locales'),
144+
'locales' => $this->locales,
108145
'currentLocale' => $locale,
109146
'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,
114151
]);
115152
}
116153

117154
public function createAction(Request $request, string $configName, string $locale, string $domain): Response
118155
{
119-
if (!$this->getParameter('php_translation.webui.enabled') || !$this->getParameter('php_translation.webui.allow_create')) {
156+
if (!$this->isWebUIEnabled || !$this->isWebUIAllowCreate) {
120157
return new Response('You are not allowed to create. Check you config. ', 400);
121158
}
122159

123160
/** @var StorageService $storage */
124-
$storage = $this->get('php_translation.storage_manager')->getStorage($configName);
161+
$storage = $this->storageManager->getStorage($configName);
125162

126163
try {
127164
$message = $this->getMessageFromRequest($request);
@@ -145,7 +182,7 @@ public function createAction(Request $request, string $configName, string $local
145182

146183
public function editAction(Request $request, string $configName, string $locale, string $domain): Response
147184
{
148-
if (!$this->getParameter('php_translation.webui.enabled')) {
185+
if (!$this->isWebUIEnabled) {
149186
return new Response('You are not allowed here. Check you config. ', 400);
150187
}
151188

@@ -159,15 +196,15 @@ public function editAction(Request $request, string $configName, string $locale,
159196
}
160197

161198
/** @var StorageService $storage */
162-
$storage = $this->get('php_translation.storage_manager')->getStorage($configName);
199+
$storage = $this->storageManager->getStorage($configName);
163200
$storage->update($message);
164201

165202
return new Response('Translation updated');
166203
}
167204

168205
public function deleteAction(Request $request, string $configName, string $locale, string $domain): Response
169206
{
170-
if (!$this->getParameter('php_translation.webui.enabled') || !$this->getParameter('php_translation.webui.allow_delete')) {
207+
if (!$this->isWebUIEnabled || !$this->isWebUIAllowDelete) {
171208
return new Response('You are not allowed to create. Check you config. ', 400);
172209
}
173210

@@ -181,7 +218,7 @@ public function deleteAction(Request $request, string $configName, string $local
181218
}
182219

183220
/** @var StorageService $storage */
184-
$storage = $this->get('php_translation.storage_manager')->getStorage($configName);
221+
$storage = $this->storageManager->getStorage($configName);
185222
$storage->delete($locale, $domain, $message->getKey());
186223

187224
return new Response('Message was deleted');
@@ -206,12 +243,11 @@ private function getMessageFromRequest(Request $request): Message
206243
*/
207244
private function getLocale2LanguageMap(): array
208245
{
209-
$configuredLocales = $this->getParameter('php_translation.locales');
210246
$names = \class_exists(Locales::class)
211247
? Locales::getNames('en')
212248
: Intl::getLocaleBundle()->getLocaleNames('en');
213249
$map = [];
214-
foreach ($configuredLocales as $l) {
250+
foreach ($this->locales as $l) {
215251
$map[$l] = $names[$l] ?? $l;
216252
}
217253

@@ -223,7 +259,7 @@ private function getLocale2LanguageMap(): array
223259
*/
224260
private function validateMessage(MessageInterface $message, array $validationGroups): void
225261
{
226-
$errors = $this->get('validator')->validate($message, null, $validationGroups);
262+
$errors = $this->validator->validate($message, null, $validationGroups);
227263
if (\count($errors) > 0) {
228264
throw MessageValidationException::create();
229265
}

0 commit comments

Comments
 (0)