Skip to content

Stop extending AbstractController to fix some deprecations #460

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
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
28 changes: 19 additions & 9 deletions Controller/SymfonyProfilerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,40 @@

namespace Translation\Bundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Translation\DataCollector\TranslationDataCollector;
use Symfony\Component\Translation\DataCollectorTranslator;
use Symfony\Component\VarDumper\Cloner\Data;
use Translation\Bundle\Model\SfProfilerMessage;
use Translation\Bundle\Service\StorageService;
use Translation\Common\Model\MessageInterface;
use Twig\Environment;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class SymfonyProfilerController extends AbstractController
class SymfonyProfilerController
{
private $storage;
/**
* @var Profiler An optional dependency
*/
private $profiler;
private $storage;
private $twig;
private $router;
private $isToolbarAllowEdit;

public function __construct(StorageService $storage, bool $isToolbarAllowEdit)
public function __construct(StorageService $storage, Environment $twig, RouterInterface $router, bool $isToolbarAllowEdit)
{
$this->storage = $storage;
$this->twig = $twig;
$this->router = $router;
$this->isToolbarAllowEdit = $isToolbarAllowEdit;
}

Expand All @@ -57,10 +63,12 @@ public function editAction(Request $request, string $token): Response
if ($request->isMethod('GET')) {
$translation = $this->storage->syncAndFetchMessage($message->getLocale(), $message->getDomain(), $message->getKey());

return $this->render('@Translation/SymfonyProfiler/edit.html.twig', [
$content = $this->twig->render('@Translation/SymfonyProfiler/edit.html.twig', [
'message' => $translation,
'key' => $request->query->get('message_id'),
]);

return new Response($content);
}

//Assert: This is a POST request
Expand Down Expand Up @@ -134,7 +142,7 @@ private function getMessage(Request $request, string $token): SfProfilerMessage
$collectorMessages = $this->getMessages($token);

if (!isset($collectorMessages[$messageId])) {
throw $this->createNotFoundException(\sprintf('No message with key "%s" was found.', $messageId));
throw new NotFoundHttpException(\sprintf('No message with key "%s" was found.', $messageId));
}
$message = SfProfilerMessage::create($collectorMessages[$messageId]);

Expand Down Expand Up @@ -179,10 +187,10 @@ private function getMessages(string $token, string $profileName = 'translation')
$profile = $this->getProfiler()->loadProfile($token);

if (null === $dataCollector = $profile->getCollector($profileName)) {
throw $this->createNotFoundException("No collector with name \"$profileName\" was found.");
throw new NotFoundHttpException("No collector with name \"$profileName\" was found.");
}
if (!$dataCollector instanceof TranslationDataCollector) {
throw $this->createNotFoundException("Collector with name \"$profileName\" is not an instance of TranslationDataCollector.");
throw new NotFoundHttpException("Collector with name \"$profileName\" is not an instance of TranslationDataCollector.");
}

$messages = $dataCollector->getMessages();
Expand Down Expand Up @@ -211,7 +219,9 @@ private function getProfiler(): Profiler
private function redirectToProfiler(string $token): RedirectResponse
{
try {
return $this->redirectToRoute('_profiler', ['token' => $token]);
$targetUrl = $this->router->generate('_profiler', ['token' => $token]);

return new RedirectResponse($targetUrl);
} catch (RouteNotFoundException $e) {
throw new \Exception('Route to profiler page not found. Please, run "composer require symfony/web-profiler-bundle" first to use this feature.');
}
Expand Down
19 changes: 14 additions & 5 deletions Controller/WebUIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Translation\Bundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
Expand All @@ -29,17 +28,19 @@
use Translation\Common\Exception\StorageException;
use Translation\Common\Model\Message;
use Translation\Common\Model\MessageInterface;
use Twig\Environment;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class WebUIController extends AbstractController
class WebUIController
{
private $configurationManager;
private $catalogueFetcher;
private $catalogueManager;
private $storageManager;
private $validator;
private $twig;
private $locales;
private $isWebUIEnabled;
private $isWebUIAllowCreate;
Expand All @@ -52,6 +53,7 @@ public function __construct(
CatalogueManager $catalogueManager,
StorageManager $storageManager,
ValidatorInterface $validator,
Environment $twig,
array $locales,
bool $isWebUIEnabled,
bool $isWebUIAllowCreate,
Expand All @@ -63,6 +65,7 @@ public function __construct(
$this->catalogueManager = $catalogueManager;
$this->storageManager = $storageManager;
$this->validator = $validator;
$this->twig = $twig;
$this->locales = $locales;
$this->isWebUIEnabled = $isWebUIEnabled;
$this->isWebUIAllowCreate = $isWebUIAllowCreate;
Expand Down Expand Up @@ -107,7 +110,7 @@ public function indexAction(?string $configName = null): Response
}
}

return $this->render('@Translation/WebUI/index.html.twig', [
$content = $this->twig->render('@Translation/WebUI/index.html.twig', [
'catalogues' => $catalogues,
'catalogueSize' => $catalogueSize,
'maxDomainSize' => $maxDomainSize,
Expand All @@ -116,6 +119,8 @@ public function indexAction(?string $configName = null): Response
'configName' => $config->getName(),
'configNames' => $this->configurationManager->getNames(),
]);

return new Response($content);
}

/**
Expand All @@ -137,7 +142,7 @@ public function showAction(string $configName, string $locale, string $domain):
return \strcmp($a->getKey(), $b->getKey());
});

return $this->render('@Translation/WebUI/show.html.twig', [
$content = $this->twig->render('@Translation/WebUI/show.html.twig', [
'messages' => $messages,
'domains' => $this->catalogueManager->getDomains(),
'currentDomain' => $domain,
Expand All @@ -149,6 +154,8 @@ public function showAction(string $configName, string $locale, string $domain):
'allow_delete' => $this->isWebUIAllowDelete,
'file_base_path' => $this->fileBasePath,
]);

return new Response($content);
}

public function createAction(Request $request, string $configName, string $locale, string $domain): Response
Expand Down Expand Up @@ -177,9 +184,11 @@ public function createAction(Request $request, string $configName, string $local
return new Response($e->getMessage(), Response::HTTP_BAD_REQUEST);
}

return $this->render('@Translation/WebUI/create.html.twig', [
$content = $this->twig->render('@Translation/WebUI/create.html.twig', [
'message' => $message,
]);

return new Response($content);
}

public function editAction(Request $request, string $configName, string $locale, string $domain): Response
Expand Down
3 changes: 2 additions & 1 deletion Resources/config/symfony_profiler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ services:
Translation\Bundle\Controller\SymfonyProfilerController:
autowire: true
public: true
tags: ['container.service_subscriber']
arguments:
- '@Translation\Bundle\Service\StorageService'
- '@twig'
- '@router'
- '%php_translation.toolbar.allow_edit%'
calls:
- setProfiler: ['@?profiler']
1 change: 1 addition & 0 deletions Resources/config/webui.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ services:
- '@Translation\Bundle\Catalogue\CatalogueManager'
- '@Translation\Bundle\Service\StorageManager'
- '@Symfony\Component\Validator\Validator\ValidatorInterface'
- '@twig'
- '%php_translation.locales%'
- '%php_translation.webui.enabled%'
- '%php_translation.webui.allow_create%'
Expand Down