Skip to content

Commit 874cf43

Browse files
authored
Stop extending AbstractController to fix some deprecations (#460)
* Stop extending AbstractController to fix some deprecations * Remove container.service_subscriber tag
1 parent 85806a7 commit 874cf43

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

Controller/SymfonyProfilerController.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,40 @@
1111

1212
namespace Translation\Bundle\Controller;
1313

14-
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1514
use Symfony\Component\HttpFoundation\RedirectResponse;
1615
use Symfony\Component\HttpFoundation\Request;
1716
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1818
use Symfony\Component\HttpKernel\Profiler\Profiler;
1919
use Symfony\Component\Routing\Exception\RouteNotFoundException;
20+
use Symfony\Component\Routing\RouterInterface;
2021
use Symfony\Component\Translation\DataCollector\TranslationDataCollector;
2122
use Symfony\Component\Translation\DataCollectorTranslator;
2223
use Symfony\Component\VarDumper\Cloner\Data;
2324
use Translation\Bundle\Model\SfProfilerMessage;
2425
use Translation\Bundle\Service\StorageService;
2526
use Translation\Common\Model\MessageInterface;
27+
use Twig\Environment;
2628

2729
/**
2830
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
2931
*/
30-
class SymfonyProfilerController extends AbstractController
32+
class SymfonyProfilerController
3133
{
32-
private $storage;
3334
/**
3435
* @var Profiler An optional dependency
3536
*/
3637
private $profiler;
38+
private $storage;
39+
private $twig;
40+
private $router;
3741
private $isToolbarAllowEdit;
3842

39-
public function __construct(StorageService $storage, bool $isToolbarAllowEdit)
43+
public function __construct(StorageService $storage, Environment $twig, RouterInterface $router, bool $isToolbarAllowEdit)
4044
{
4145
$this->storage = $storage;
46+
$this->twig = $twig;
47+
$this->router = $router;
4248
$this->isToolbarAllowEdit = $isToolbarAllowEdit;
4349
}
4450

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

60-
return $this->render('@Translation/SymfonyProfiler/edit.html.twig', [
66+
$content = $this->twig->render('@Translation/SymfonyProfiler/edit.html.twig', [
6167
'message' => $translation,
6268
'key' => $request->query->get('message_id'),
6369
]);
70+
71+
return new Response($content);
6472
}
6573

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

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

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

181189
if (null === $dataCollector = $profile->getCollector($profileName)) {
182-
throw $this->createNotFoundException("No collector with name \"$profileName\" was found.");
190+
throw new NotFoundHttpException("No collector with name \"$profileName\" was found.");
183191
}
184192
if (!$dataCollector instanceof TranslationDataCollector) {
185-
throw $this->createNotFoundException("Collector with name \"$profileName\" is not an instance of TranslationDataCollector.");
193+
throw new NotFoundHttpException("Collector with name \"$profileName\" is not an instance of TranslationDataCollector.");
186194
}
187195

188196
$messages = $dataCollector->getMessages();
@@ -211,7 +219,9 @@ private function getProfiler(): Profiler
211219
private function redirectToProfiler(string $token): RedirectResponse
212220
{
213221
try {
214-
return $this->redirectToRoute('_profiler', ['token' => $token]);
222+
$targetUrl = $this->router->generate('_profiler', ['token' => $token]);
223+
224+
return new RedirectResponse($targetUrl);
215225
} catch (RouteNotFoundException $e) {
216226
throw new \Exception('Route to profiler page not found. Please, run "composer require symfony/web-profiler-bundle" first to use this feature.');
217227
}

Controller/WebUIController.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Translation\Bundle\Controller;
1313

14-
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1514
use Symfony\Component\HttpFoundation\Request;
1615
use Symfony\Component\HttpFoundation\Response;
1716
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@@ -29,17 +28,19 @@
2928
use Translation\Common\Exception\StorageException;
3029
use Translation\Common\Model\Message;
3130
use Translation\Common\Model\MessageInterface;
31+
use Twig\Environment;
3232

3333
/**
3434
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
3535
*/
36-
class WebUIController extends AbstractController
36+
class WebUIController
3737
{
3838
private $configurationManager;
3939
private $catalogueFetcher;
4040
private $catalogueManager;
4141
private $storageManager;
4242
private $validator;
43+
private $twig;
4344
private $locales;
4445
private $isWebUIEnabled;
4546
private $isWebUIAllowCreate;
@@ -52,6 +53,7 @@ public function __construct(
5253
CatalogueManager $catalogueManager,
5354
StorageManager $storageManager,
5455
ValidatorInterface $validator,
56+
Environment $twig,
5557
array $locales,
5658
bool $isWebUIEnabled,
5759
bool $isWebUIAllowCreate,
@@ -63,6 +65,7 @@ public function __construct(
6365
$this->catalogueManager = $catalogueManager;
6466
$this->storageManager = $storageManager;
6567
$this->validator = $validator;
68+
$this->twig = $twig;
6669
$this->locales = $locales;
6770
$this->isWebUIEnabled = $isWebUIEnabled;
6871
$this->isWebUIAllowCreate = $isWebUIAllowCreate;
@@ -107,7 +110,7 @@ public function indexAction(?string $configName = null): Response
107110
}
108111
}
109112

110-
return $this->render('@Translation/WebUI/index.html.twig', [
113+
$content = $this->twig->render('@Translation/WebUI/index.html.twig', [
111114
'catalogues' => $catalogues,
112115
'catalogueSize' => $catalogueSize,
113116
'maxDomainSize' => $maxDomainSize,
@@ -116,6 +119,8 @@ public function indexAction(?string $configName = null): Response
116119
'configName' => $config->getName(),
117120
'configNames' => $this->configurationManager->getNames(),
118121
]);
122+
123+
return new Response($content);
119124
}
120125

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

140-
return $this->render('@Translation/WebUI/show.html.twig', [
145+
$content = $this->twig->render('@Translation/WebUI/show.html.twig', [
141146
'messages' => $messages,
142147
'domains' => $this->catalogueManager->getDomains(),
143148
'currentDomain' => $domain,
@@ -149,6 +154,8 @@ public function showAction(string $configName, string $locale, string $domain):
149154
'allow_delete' => $this->isWebUIAllowDelete,
150155
'file_base_path' => $this->fileBasePath,
151156
]);
157+
158+
return new Response($content);
152159
}
153160

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

180-
return $this->render('@Translation/WebUI/create.html.twig', [
187+
$content = $this->twig->render('@Translation/WebUI/create.html.twig', [
181188
'message' => $message,
182189
]);
190+
191+
return new Response($content);
183192
}
184193

185194
public function editAction(Request $request, string $configName, string $locale, string $domain): Response

Resources/config/symfony_profiler.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ services:
88
Translation\Bundle\Controller\SymfonyProfilerController:
99
autowire: true
1010
public: true
11-
tags: ['container.service_subscriber']
1211
arguments:
1312
- '@Translation\Bundle\Service\StorageService'
13+
- '@twig'
14+
- '@router'
1415
- '%php_translation.toolbar.allow_edit%'
1516
calls:
1617
- setProfiler: ['@?profiler']

Resources/config/webui.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ services:
99
- '@Translation\Bundle\Catalogue\CatalogueManager'
1010
- '@Translation\Bundle\Service\StorageManager'
1111
- '@Symfony\Component\Validator\Validator\ValidatorInterface'
12+
- '@twig'
1213
- '%php_translation.locales%'
1314
- '%php_translation.webui.enabled%'
1415
- '%php_translation.webui.allow_create%'

0 commit comments

Comments
 (0)