Skip to content

Added Symfony 4 compatibility and refactored code #345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Catalogue/CatalogueFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Translation\Bundle\Catalogue;

use Nyholm\NSA;
use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader as SymfonyTranslationLoader;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Reader\TranslationReaderInterface;
use Translation\Bundle\Model\Configuration;
Expand All @@ -35,7 +34,7 @@ final class CatalogueFetcher
private $reader;

/**
* @param SymfonyTranslationLoader|TranslationLoader|TranslationReaderInterface $reader
* @param TranslationLoader|TranslationReaderInterface $reader
*/
public function __construct($reader)
{
Expand Down
17 changes: 9 additions & 8 deletions Catalogue/CatalogueManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Translation\Bundle\Model\Metadata;

/**
* A manager that handle loaded catalogues.
* A manager that handles loaded catalogues.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
Expand Down Expand Up @@ -123,11 +123,7 @@ public function findMessages(array $config = []): array
return $messages;
}

/**
* @param string $domain
* @param string $key
*/
public function getTranslations($domain, $key): array
public function getTranslations(string $domain, string $key): array
{
$translations = [];
foreach ($this->catalogues as $locale => $catalogue) {
Expand All @@ -139,8 +135,13 @@ public function getTranslations($domain, $key): array
return $translations;
}

private function createMessage(MessageCatalogueInterface $catalogue, string $locale, string $domain, string $key, string $text): CatalogueMessage
{
private function createMessage(
MessageCatalogueInterface $catalogue,
string $locale,
string $domain,
string $key,
string $text
): CatalogueMessage {
$catalogueMessage = new CatalogueMessage($this, $locale, $domain, $key, $text);

if ($catalogue instanceof MetadataAwareInterface) {
Expand Down
38 changes: 31 additions & 7 deletions Controller/EditInPlaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,44 @@

namespace Translation\Bundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Translation\Bundle\Exception\MessageValidationException;
use Translation\Bundle\Service\CacheClearer;
use Translation\Bundle\Service\StorageManager;
use Translation\Bundle\Service\StorageService;
use Translation\Common\Model\Message;
use Translation\Common\Model\MessageInterface;

/**
* @author Damien Alexandre <dalexandre@jolicode.com>
*/
class EditInPlaceController extends Controller
class EditInPlaceController extends AbstractController
{
/**
* @var StorageManager
*/
private $storageManager;

/**
* @var CacheClearer
*/
private $cacheClearer;

/**
* @var ValidatorInterface
*/
private $validator;

public function __construct(StorageManager $storageManager, CacheClearer $cacheClearer, ValidatorInterface $validator)
{
$this->storageManager = $storageManager;
$this->cacheClearer = $cacheClearer;
$this->validator = $validator;
}

public function editAction(Request $request, string $configName, string $locale): Response
{
try {
Expand All @@ -33,13 +58,13 @@ public function editAction(Request $request, string $configName, string $locale)
}

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

foreach ($messages as $message) {
$storage->update($message);
}

$cacheClearer = $this->get('php_translation.cache_clearer');
$cacheClearer->clearAndWarmUp($locale);
$this->cacheClearer->clearAndWarmUp($locale);

return new Response();
}
Expand All @@ -56,14 +81,13 @@ private function getMessages(Request $request, string $locale, array $validation
$json = $request->getContent();
$data = \json_decode($json, true);
$messages = [];
$validator = $this->get('validator');

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

$message = new Message($translationKey, $domain, $locale, $value);
$errors = $this->validator->validate($message, null, $validationGroups);

$errors = $validator->validate($message, null, $validationGroups);
if (\count($errors) > 0) {
throw MessageValidationException::create();
}
Expand Down
55 changes: 31 additions & 24 deletions Controller/SymfonyProfilerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@

namespace Translation\Bundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\Translation\DataCollectorTranslator;
use Symfony\Component\VarDumper\Cloner\Data;
use Translation\Bundle\Model\SfProfilerMessage;
Expand All @@ -23,8 +24,24 @@
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class SymfonyProfilerController extends Controller
class SymfonyProfilerController extends AbstractController
{
/**
* @var StorageService
*/
private $storageService;

/**
* @var Profiler
*/
private $profiler;

public function __construct(StorageService $storageService, Profiler $profiler)
{
$this->storageService = $storageService;
$this->profiler = $profiler;
}

public function editAction(Request $request, string $token): Response
{
if (!$this->getParameter('php_translation.toolbar.allow_edit')) {
Expand All @@ -36,11 +53,9 @@ public function editAction(Request $request, string $token): Response
}

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

if ($request->isMethod('GET')) {
$translation = $storage->syncAndFetchMessage($message->getLocale(), $message->getDomain(), $message->getKey());
$translation = $this->storageService->syncAndFetchMessage($message->getLocale(), $message->getDomain(), $message->getKey());

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

//Assert: This is a POST request
$message->setTranslation($request->request->get('translation'));
$storage->update($message->convertToMessage());
$this->storageService->update($message->convertToMessage());

return new Response($message->getTranslation());
}
Expand All @@ -61,10 +76,8 @@ public function syncAction(Request $request, string $token): Response
return $this->redirectToRoute('_profiler', ['token' => $token]);
}

/** @var StorageService $storage */
$storage = $this->get('php_translation.storage');
$sfMessage = $this->getMessage($request, $token);
$message = $storage->syncAndFetchMessage($sfMessage->getLocale(), $sfMessage->getDomain(), $sfMessage->getKey());
$message = $this->storageService->syncAndFetchMessage($sfMessage->getLocale(), $sfMessage->getDomain(), $sfMessage->getKey());

if (null !== $message) {
return new Response($message->getTranslation());
Expand All @@ -73,18 +86,13 @@ public function syncAction(Request $request, string $token): Response
return new Response('Asset not found', 404);
}

/**
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
*/
public function syncAllAction(Request $request, string $token): Response
{
if (!$request->isXmlHttpRequest()) {
return $this->redirectToRoute('_profiler', ['token' => $token]);
}

/** @var StorageService $storage */
$storage = $this->get('php_translation.storage');
$storage->sync();
$this->storageService->sync();

return new Response('Started synchronization of all translations');
}
Expand All @@ -106,10 +114,9 @@ public function createAssetsAction(Request $request, string $token): Response
}

$uploaded = [];
/** @var StorageService $storage */
$storage = $this->get('php_translation.storage');

foreach ($messages as $message) {
$storage->create($message);
$this->storageService->create($message);
$uploaded[] = $message;
}

Expand All @@ -118,12 +125,12 @@ public function createAssetsAction(Request $request, string $token): Response

private function getMessage(Request $request, string $token): SfProfilerMessage
{
$profiler = $this->get('profiler');
$profiler->disable();
$this->profiler->disable();

$messageId = $request->request->get('message_id', $request->query->get('message_id'));

$profile = $profiler->loadProfile($token);
$profile = $this->profiler->loadProfile($token);

if (null === $dataCollector = $profile->getCollector('translation')) {
throw $this->createNotFoundException('No collector with name "translation" was found.');
}
Expand All @@ -137,6 +144,7 @@ private function getMessage(Request $request, string $token): SfProfilerMessage
if (!isset($collectorMessages[$messageId])) {
throw $this->createNotFoundException(\sprintf('No message with key "%s" was found.', $messageId));
}

$message = SfProfilerMessage::create($collectorMessages[$messageId]);

if (DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK === $message->getState()) {
Expand All @@ -152,15 +160,14 @@ private function getMessage(Request $request, string $token): SfProfilerMessage
*/
protected function getSelectedMessages(Request $request, string $token): array
{
$profiler = $this->get('profiler');
$profiler->disable();
$this->profiler->disable();

$selected = $request->request->get('selected');
if (!$selected || 0 == \count($selected)) {
return [];
}

$profile = $profiler->loadProfile($token);
$profile = $this->profiler->loadProfile($token);
$dataCollector = $profile->getCollector('translation');
$messages = $dataCollector->getMessages();

Expand Down
Loading