Skip to content

SF 5.x compatibility #356

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

Merged
merged 6 commits into from
Dec 26, 2019
Merged
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
15 changes: 1 addition & 14 deletions Catalogue/CatalogueFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@
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;
use Translation\SymfonyStorage\LegacyTranslationReader;
use Translation\SymfonyStorage\TranslationLoader;

/**
* Fetches catalogues from source files. This will only work with local file storage
Expand All @@ -29,20 +26,10 @@
*/
final class CatalogueFetcher
{
/**
* @var TranslationReaderInterface
*/
private $reader;

/**
* @param SymfonyTranslationLoader|TranslationLoader|TranslationReaderInterface $reader
*/
public function __construct($reader)
public function __construct(TranslationReaderInterface $reader)
{
if (!$reader instanceof TranslationReaderInterface) {
$reader = new LegacyTranslationReader($reader);
}

$this->reader = $reader;
}

Expand Down
15 changes: 1 addition & 14 deletions Catalogue/CatalogueWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
namespace Translation\Bundle\Catalogue;

use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Writer\TranslationWriter;
use Symfony\Component\Translation\Writer\TranslationWriterInterface;
use Translation\Bundle\Model\Configuration;
use Translation\SymfonyStorage\LegacyTranslationWriter;

/**
* Write catalogues back to disk.
Expand All @@ -26,22 +24,11 @@
*/
final class CatalogueWriter
{
/**
* @var TranslationWriterInterface
*/
private $writer;

/**
* @var string
*/
private $defaultLocale;

public function __construct(TranslationWriter $writer, string $defaultLocale)
public function __construct(TranslationWriterInterface $writer, string $defaultLocale)
{
if (!$writer instanceof TranslationWriterInterface) {
$writer = new LegacyTranslationWriter($writer);
}

$this->writer = $writer;
$this->defaultLocale = $defaultLocale;
}
Expand Down
26 changes: 19 additions & 7 deletions Controller/EditInPlaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,33 @@

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
{
private $storageManager;
private $cacheClearer;
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 +47,12 @@ 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 +69,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 = $validator->validate($message, null, $validationGroups);
$errors = $this->validator->validate($message, null, $validationGroups);
if (\count($errors) > 0) {
throw MessageValidationException::create();
}
Expand Down
36 changes: 20 additions & 16 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,11 +24,22 @@
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class SymfonyProfilerController extends Controller
class SymfonyProfilerController extends AbstractController
{
private $storage;
private $profiler;
private $isToolbarAllowEdit;

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

public function editAction(Request $request, string $token): Response
{
if (!$this->getParameter('php_translation.toolbar.allow_edit')) {
if (!$this->isToolbarAllowEdit) {
return new Response('You are not allowed to edit the translations.');
}

Expand All @@ -36,11 +48,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->storage->syncAndFetchMessage($message->getLocale(), $message->getDomain(), $message->getKey());

return $this->render('@Translation/SymfonyProfiler/edit.html.twig', [
'message' => $translation,
Expand All @@ -50,7 +60,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->storage->update($message->convertToMessage());

return new Response($message->getTranslation());
}
Expand All @@ -61,10 +71,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->storage->syncAndFetchMessage($sfMessage->getLocale(), $sfMessage->getDomain(), $sfMessage->getKey());

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

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

return new Response('Started synchronization of all translations');
}
Expand All @@ -106,10 +112,8 @@ 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->storage->create($message);
$uploaded[] = $message;
}

Expand Down
Loading