Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions Controller/SymfonyProfilerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
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\Profiler\Profiler;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Translation\DataCollector\TranslationDataCollector;
use Symfony\Component\Translation\DataCollectorTranslator;
use Symfony\Component\VarDumper\Cloner\Data;
Expand All @@ -28,13 +30,15 @@
class SymfonyProfilerController extends AbstractController
{
private $storage;
/**
* @var Profiler An optional dependency
*/
private $profiler;
private $isToolbarAllowEdit;

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

Expand All @@ -45,7 +49,7 @@ public function editAction(Request $request, string $token): Response
}

if (!$request->isXmlHttpRequest()) {
return $this->redirectToRoute('_profiler', ['token' => $token]);
return $this->redirectToProfiler($token);
}

$message = $this->getMessage($request, $token);
Expand All @@ -69,7 +73,7 @@ public function editAction(Request $request, string $token): Response
public function syncAction(Request $request, string $token): Response
{
if (!$request->isXmlHttpRequest()) {
return $this->redirectToRoute('_profiler', ['token' => $token]);
return $this->redirectToProfiler($token);
}

$sfMessage = $this->getMessage($request, $token);
Expand All @@ -88,7 +92,7 @@ public function syncAction(Request $request, string $token): Response
public function syncAllAction(Request $request, string $token): Response
{
if (!$request->isXmlHttpRequest()) {
return $this->redirectToRoute('_profiler', ['token' => $token]);
return $this->redirectToProfiler($token);
}

$this->storage->sync();
Expand All @@ -104,7 +108,7 @@ public function syncAllAction(Request $request, string $token): Response
public function createAssetsAction(Request $request, string $token): Response
{
if (!$request->isXmlHttpRequest()) {
return $this->redirectToRoute('_profiler', ['token' => $token]);
return $this->redirectToProfiler($token);
}

$messages = $this->getSelectedMessages($request, $token);
Expand All @@ -123,7 +127,7 @@ public function createAssetsAction(Request $request, string $token): Response

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

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

Expand All @@ -136,7 +140,7 @@ private function getMessage(Request $request, string $token): SfProfilerMessage

if (DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK === $message->getState()) {
/** @var \Symfony\Component\HttpKernel\DataCollector\RequestDataCollector */
$requestCollector = $this->profiler->loadProfile($token)->getCollector('request');
$requestCollector = $this->getProfiler()->loadProfile($token)->getCollector('request');

$message
->setLocale($requestCollector->getLocale())
Expand All @@ -152,7 +156,7 @@ private function getMessage(Request $request, string $token): SfProfilerMessage
*/
protected function getSelectedMessages(Request $request, string $token): array
{
$this->profiler->disable();
$this->getProfiler()->disable();

/** @var string[] $selected */
$selected = (array) $request->request->get('selected');
Expand All @@ -172,7 +176,7 @@ protected function getSelectedMessages(Request $request, string $token): array

private function getMessages(string $token, string $profileName = 'translation'): array
{
$profile = $this->profiler->loadProfile($token);
$profile = $this->getProfiler()->loadProfile($token);

if (null === $dataCollector = $profile->getCollector($profileName)) {
throw $this->createNotFoundException("No collector with name \"$profileName\" was found.");
Expand All @@ -189,4 +193,27 @@ private function getMessages(string $token, string $profileName = 'translation')

return $messages;
}

public function setProfiler(Profiler $profiler): void
{
$this->profiler = $profiler;
}

private function getProfiler(): Profiler
{
if (!$this->profiler) {
throw new \Exception('The "profiler" service is missing. Please, run "composer require symfony/web-profiler-bundle" first to use this feature.');
}

return $this->profiler;
}

private function redirectToProfiler(string $token): RedirectResponse
{
try {
return $this->redirectToRoute('_profiler', ['token' => $token]);
} 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.');
}
}
}
3 changes: 2 additions & 1 deletion Resources/config/symfony_profiler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ services:
tags: ['container.service_subscriber']
arguments:
- '@Translation\Bundle\Service\StorageService'
- '@profiler'
- '%php_translation.toolbar.allow_edit%'
calls:
- setProfiler: ['@?profiler']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this throw an exception in combination with the non-nullable typehint on the setter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! I tried this when was coding and no exception was thrown. It looks like Symfony just does not call that setProfiler() method at all if the @profiler service unavailable