Skip to content

Commit 84674b7

Browse files
authored
Major fixing, refactoring and making sure we can use the web profiler (#21)
* Adding Symfony Profiler integration * Added a StorageService * Work on the storage service * Added tags to the file storage * Update to latest Storage id * Bugfixes * Make sure the profiler page is usable * Added comment * bugfix * code style * Bugfixes
1 parent 4d50ae9 commit 84674b7

21 files changed

+908
-257
lines changed

Catalogue/CatalogueManager.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Translation\Bundle\Catalogue;
1313

1414
use Symfony\Component\Translation\MessageCatalogue;
15-
use Translation\Bundle\Model\Message;
15+
use Translation\Bundle\Model\CatalogueMessage;
1616

1717
/**
1818
* A manager that handle loaded catalogues.
@@ -65,7 +65,7 @@ public function getDomains()
6565
* @param string $locale
6666
* @param string $domain
6767
*
68-
* @return Message[]
68+
* @return CatalogueMessage[]
6969
*/
7070
public function getMessages($locale, $domain)
7171
{
@@ -75,7 +75,7 @@ public function getMessages($locale, $domain)
7575
}
7676

7777
foreach ($this->catalogues[$locale]->all($domain) as $key => $text) {
78-
$messages[] = new Message($this, $locale, $domain, $key, $text);
78+
$messages[] = new CatalogueMessage($this, $locale, $domain, $key, $text);
7979
}
8080

8181
return $messages;

Controller/SymfonyProfilerController.php

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111

1212
namespace Translation\Bundle\Controller;
1313

14-
use Happyr\TranslationBundle\Model\Message;
15-
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
16-
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
1714
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
1815
use Symfony\Component\HttpFoundation\Request;
1916
use Symfony\Component\HttpFoundation\Response;
2017
use Symfony\Component\Translation\DataCollectorTranslator;
18+
use Translation\Bundle\Model\SfProfilerMessage;
19+
use Translation\Common\Model\Message;
2120

2221
/**
2322
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
@@ -28,13 +27,11 @@ class SymfonyProfilerController extends Controller
2827
* @param Request $request
2928
* @param string $token
3029
*
31-
* @Route("/{token}/translation/edit", name="_profiler_translations_edit")
32-
*
3330
* @return Response
3431
*/
3532
public function editAction(Request $request, $token)
3633
{
37-
if (!$this->getParameter('translation.toolbar.allow_edit')) {
34+
if (!$this->getParameter('php_translation.toolbar.allow_edit')) {
3835
return new Response('You are not allowed to edit the translations.');
3936
}
4037

@@ -43,20 +40,20 @@ public function editAction(Request $request, $token)
4340
}
4441

4542
$message = $this->getMessage($request, $token);
46-
$trans = $this->get('happyr.translation');
43+
$storage = $this->get('php_translation.storage');
4744

4845
if ($request->isMethod('GET')) {
49-
$trans->fetchTranslation($message);
46+
$translation = $storage->syncAndFetchMessage($message->getLocale(), $message->getDomain(), $message->getKey());
5047

51-
return $this->render('HappyrTranslationBundle:Profiler:edit.html.twig', [
52-
'message' => $message,
53-
'key' => $request->query->get('message_id'),
48+
return $this->render('TranslationBundle:SymfonyProfiler:edit.html.twig', [
49+
'message' => $translation,
50+
'key' => $message->getLocale().$message->getDomain().$message->getKey(),
5451
]);
5552
}
5653

5754
//Assert: This is a POST request
5855
$message->setTranslation($request->request->get('translation'));
59-
$trans->updateTranslation($message);
56+
$storage->update($message->convertToMessage());
6057

6158
return new Response($message->getTranslation());
6259
}
@@ -65,9 +62,6 @@ public function editAction(Request $request, $token)
6562
* @param Request $request
6663
* @param string $token
6764
*
68-
* @Route("/{token}/translation/flag", name="_profiler_translations_flag")
69-
* @Method("POST")
70-
*
7165
* @return Response
7266
*/
7367
public function flagAction(Request $request, $token)
@@ -78,7 +72,8 @@ public function flagAction(Request $request, $token)
7872

7973
$message = $this->getMessage($request, $token);
8074

81-
$saved = $this->get('happyr.translation')->flagTranslation($message);
75+
// TODO
76+
$saved = false;
8277

8378
return new Response($saved ? 'OK' : 'ERROR');
8479
}
@@ -87,9 +82,6 @@ public function flagAction(Request $request, $token)
8782
* @param Request $request
8883
* @param string $token
8984
*
90-
* @Route("/{token}/translation/sync", name="_profiler_translations_sync")
91-
* @Method("POST")
92-
*
9385
* @return Response
9486
*/
9587
public function syncAction(Request $request, $token)
@@ -98,11 +90,11 @@ public function syncAction(Request $request, $token)
9890
return $this->redirectToRoute('_profiler', ['token' => $token]);
9991
}
10092

101-
$message = $this->getMessage($request, $token);
102-
$translation = $this->get('happyr.translation')->fetchTranslation($message, true);
93+
$sfMessage = $this->getMessage($request, $token);
94+
$message = $this->get('php_translation.storage')->syncAndFetchMessage($sfMessage->getLocale(), $sfMessage->getDomain(), $sfMessage->getKey());
10395

104-
if ($translation !== null) {
105-
return new Response($translation);
96+
if ($message !== null) {
97+
return new Response($message->getTranslation());
10698
}
10799

108100
return new Response('Asset not found', 404);
@@ -112,8 +104,6 @@ public function syncAction(Request $request, $token)
112104
* @param Request $request
113105
* @param $token
114106
*
115-
* @Route("/{token}/translation/sync-all", name="_profiler_translations_sync_all")
116-
*
117107
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
118108
*/
119109
public function syncAllAction(Request $request, $token)
@@ -122,7 +112,7 @@ public function syncAllAction(Request $request, $token)
122112
return $this->redirectToRoute('_profiler', ['token' => $token]);
123113
}
124114

125-
$this->get('happyr.translation')->synchronizeAllTranslations();
115+
$this->get('php_translation.storage')->sync();
126116

127117
return new Response('Started synchronization of all translations');
128118
}
@@ -135,9 +125,6 @@ public function syncAllAction(Request $request, $token)
135125
* @param Request $request
136126
* @param string $token
137127
*
138-
* @Route("/{token}/translation/create-asset", name="_profiler_translations_create_assets")
139-
* @Method("POST")
140-
*
141128
* @return Response
142129
*/
143130
public function createAssetsAction(Request $request, $token)
@@ -152,26 +139,21 @@ public function createAssetsAction(Request $request, $token)
152139
}
153140

154141
$uploaded = [];
155-
$trans = $this->get('happyr.translation');
142+
$trans = $this->get('php_translation.storage');
156143
foreach ($messages as $message) {
157-
if ($trans->createAsset($message)) {
144+
if ($trans->update($message)) {
158145
$uploaded[] = $message;
159146
}
160147
}
161148

162-
$saved = count($uploaded);
163-
if ($saved > 0) {
164-
$this->get('happyr.translation.filesystem')->updateMessageCatalog($uploaded);
165-
}
166-
167-
return new Response(sprintf('%s new assets created!', $saved));
149+
return new Response(sprintf('%s new assets created!', count($uploaded)));
168150
}
169151

170152
/**
171153
* @param Request $request
172154
* @param string $token
173155
*
174-
* @return Message
156+
* @return SfProfilerMessage
175157
*/
176158
protected function getMessage(Request $request, $token)
177159
{
@@ -185,7 +167,7 @@ protected function getMessage(Request $request, $token)
185167
if (!isset($messages[$messageId])) {
186168
throw $this->createNotFoundException(sprintf('No message with key "%s" was found.', $messageId));
187169
}
188-
$message = new Message($messages[$messageId]);
170+
$message = SfProfilerMessage::create($messages[$messageId]);
189171

190172
if ($message->getState() === DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK) {
191173
$message->setLocale($profile->getCollector('request')->getLocale())

Controller/WebUIController.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
use Symfony\Component\Translation\MessageCatalogue;
2020
use Symfony\Component\Translation\Translator;
2121
use Translation\Bundle\Exception\MessageValidationException;
22-
use Translation\Bundle\Model\GuiMessageRepresentation;
22+
use Translation\Bundle\Model\WebUiMessage;
2323
use Translation\Common\Exception\StorageException;
24-
use Translation\Bundle\Model\Message;
24+
use Translation\Bundle\Model\CatalogueMessage;
2525

2626
/**
2727
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
@@ -87,9 +87,9 @@ public function showAction($configName, $locale, $domain)
8787
$catalogueManager = $this->get('php_translation.catalogue_manager');
8888
$catalogueManager->load($catalogues);
8989

90-
/** @var Message[] $messages */
90+
/** @var CatalogueMessage[] $messages */
9191
$messages = $catalogueManager->getMessages($locale, $domain);
92-
usort($messages, function (Message $a, Message $b) {
92+
usort($messages, function (CatalogueMessage $a, CatalogueMessage $b) {
9393
return strcmp($a->getKey(), $b->getKey());
9494
});
9595

@@ -201,13 +201,13 @@ private function getConfiguration(&$configName)
201201
* @param Request $request
202202
* @param array $validationGroups
203203
*
204-
* @return GuiMessageRepresentation
204+
* @return WebUiMessage
205205
*/
206206
private function getMessage(Request $request, array $validationGroups = [])
207207
{
208208
$json = $request->getContent();
209209
$data = json_decode($json, true);
210-
$message = new GuiMessageRepresentation();
210+
$message = new WebUiMessage();
211211
if (isset($data['key'])) {
212212
$message->setKey($data['key']);
213213
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <tobias.nyholm@gmail.com>
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\DependencyInjection\CompilerPass;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Definition;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
19+
/**
20+
* Register all storages in the StorageService.
21+
*
22+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
23+
*/
24+
class StoragePass implements CompilerPassInterface
25+
{
26+
/**
27+
* @var Definition[]
28+
*/
29+
private $definitions;
30+
31+
public function process(ContainerBuilder $container)
32+
{
33+
$services = $container->findTaggedServiceIds('php_translation.storage');
34+
foreach ($services as $id => $tags) {
35+
foreach ($tags as $tag) {
36+
if (!isset($tag['name'])) {
37+
$tag['name'] = 'default';
38+
}
39+
if (!isset($tag['type'])) {
40+
throw new \LogicException('The tag "php_translation.storage" must have a "type".');
41+
}
42+
43+
$def = $this->getDefinition($container, $tag['name']);
44+
switch ($tag['type']) {
45+
case 'remote':
46+
$def->addMethodCall('addRemoteStorage', [new Reference($id)]);
47+
break;
48+
case 'local':
49+
$def->addMethodCall('addLocalStorage', [new Reference($id)]);
50+
break;
51+
default:
52+
throw new \LogicException(sprintf('The tag "php_translation.storage" must have a "type" of value "local" or "remote". Value "%s" was provided', $tag['type']));
53+
}
54+
}
55+
}
56+
}
57+
58+
/**
59+
* @param ContainerBuilder $container
60+
*
61+
* @return Definition
62+
*/
63+
private function getDefinition(ContainerBuilder $container, $name)
64+
{
65+
if (!isset($this->definitions[$name])) {
66+
$this->definitions[$name] = $container->getDefinition('php_translation.storage.'.$name);
67+
}
68+
69+
return $this->definitions[$name];
70+
}
71+
}

DependencyInjection/TranslationExtension.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\DependencyInjection\Reference;
1818
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1919
use Symfony\Component\DependencyInjection\Loader;
20+
use Translation\Bundle\Service\StorageService;
2021

2122
/**
2223
* This is the class that loads and manages your bundle configuration.
@@ -44,19 +45,38 @@ public function load(array $configs, ContainerBuilder $container)
4445
$this->enableWebUi($container, $config);
4546
}
4647

48+
if ($config['symfony_profiler']['enabled']) {
49+
$loader->load('symfony_profiler.yml');
50+
$this->enableSymfonyProfiler($container, $config);
51+
}
52+
4753
if ($config['fallback_translation']['enabled']) {
4854
$loader->load('auto_translation.yml');
4955
$this->enableFallbackAutoTranslator($container, $config);
5056
}
5157

58+
$first = null;
5259
foreach ($config['configs'] as $name => &$c) {
60+
if ($first === null || $name === 'default') {
61+
$first = $name;
62+
}
5363
if (empty($c['project_root'])) {
5464
$c['project_root'] = dirname($container->getParameter('kernel.root_dir'));
5565
}
5666

57-
$def = new DefinitionDecorator('php_translation.storage.file.abstract');
58-
$def->replaceArgument(2, $c['output_dir']);
59-
$container->setDefinition('php_translation.storage.file.'.$name, $def);
67+
$container->register('php_translation.storage.'.$name, StorageService::class);
68+
69+
// Register a file storage
70+
$def = new DefinitionDecorator('php_translation.single_storage.file.abstract');
71+
$def->replaceArgument(2, $c['output_dir'])
72+
->addTag('php_translation.storage', ['type' => 'local', 'name' => $name]);
73+
$container->setDefinition('php_translation.single_storage.file.'.$name, $def);
74+
}
75+
76+
if ($first !== null) {
77+
// Create some aliases for the default storage
78+
$container->setAlias('php_translation.storage', 'php_translation.storage.'.$first);
79+
$container->setAlias('php_translation.storage.default', 'php_translation.storage.'.$first);
6080
}
6181

6282
$container->getDefinition('php_translation.configuration_manager')
@@ -67,6 +87,11 @@ private function enableWebUi(ContainerBuilder $container, $config)
6787
{
6888
}
6989

90+
private function enableSymfonyProfiler(ContainerBuilder $container, $config)
91+
{
92+
$container->setParameter('php_translation.toolbar.allow_edit', $config['symfony_profiler']['allow_edit']);
93+
}
94+
7095
private function enableFallbackAutoTranslator(ContainerBuilder $container, $config)
7196
{
7297
$externalTranslatorId = 'php_translation.translator_service.'.$config['fallback_translation']['service'];
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313

1414
use Translation\Bundle\Catalogue\CatalogueManager;
1515

16-
class Message
16+
/**
17+
* A message representation for CatalogueManager.
18+
*
19+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
20+
*/
21+
class CatalogueMessage
1722
{
1823
/**
1924
* @var CatalogueManager

0 commit comments

Comments
 (0)