Skip to content

Correct / improve SymfonyProfilerController #370

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 1 commit into from
Dec 29, 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
57 changes: 32 additions & 25 deletions Controller/SymfonyProfilerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\Translation\DataCollector\TranslationDataCollector;
use Symfony\Component\Translation\DataCollectorTranslator;
use Symfony\Component\VarDumper\Cloner\Data;
use Translation\Bundle\Model\SfProfilerMessage;
Expand Down Expand Up @@ -122,30 +123,25 @@ 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);
if (null === $dataCollector = $profile->getCollector('translation')) {
throw $this->createNotFoundException('No collector with name "translation" was found.');
}

$collectorMessages = $dataCollector->getMessages();

if ($collectorMessages instanceof Data) {
$collectorMessages = $collectorMessages->getValue(true);
}
$collectorMessages = $this->getMessages($token);

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()) {
$message->setLocale($profile->getCollector('request')->getLocale())
->setTranslation(\sprintf('[%s]', $message->getTranslation()));
/** @var \Symfony\Component\HttpKernel\DataCollector\RequestDataCollector */
$requestCollector = $this->profiler->loadProfile($token)->getCollector('request');

$message
Copy link
Member

Choose a reason for hiding this comment

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

What about reducing fluent usage? That way you don't need to rely on the message returning itself.

$message->setLocale()
$message->setTranslation()

Copy link
Member Author

Choose a reason for hiding this comment

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

I kept the original syntax.

->setLocale($requestCollector->getLocale())
->setTranslation(\sprintf('[%s]', $message->getTranslation()))
;
}

return $message;
Expand All @@ -156,23 +152,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);
$dataCollector = $profile->getCollector('translation');
$messages = $dataCollector->getMessages();

if ($messages instanceof Data) {
$messages = $messages->getValue(true);
}

$toSave = \array_intersect_key($messages, \array_flip($selected));
$toSave = \array_intersect_key($this->getMessages($token), \array_flip($selected));

$messages = [];
foreach ($toSave as $data) {
Expand All @@ -181,4 +168,24 @@ protected function getSelectedMessages(Request $request, string $token): array

return $messages;
}

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

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

$messages = $dataCollector->getMessages();

if (\class_exists(Data::class) && $messages instanceof Data) {
return $messages->getValue(true);
}

return $messages;
}
}
1 change: 1 addition & 0 deletions Resources/config/symfony_profiler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:
- { name: 'data_collector', template: "@Translation/SymfonyProfiler/translation.html.twig", id: "translation", priority: 200 }

Translation\Bundle\Controller\SymfonyProfilerController:
autowire: true
Copy link
Member

Choose a reason for hiding this comment

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

What about moving this to a _defaults section?

_defaults:
    autowire: true
    autoconfigure: true

public: true
tags: ['container.service_subscriber']
arguments:
Expand Down
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,8 @@ parameters:
message: "#^Call to an undefined method Symfony\\\\Component\\\\Translation\\\\TranslatorBagInterface\\|Symfony\\\\Contracts\\\\Translation\\\\TranslatorInterface\\:\\:transChoice\\(\\)\\.$#"
count: 2
path: Twig/TranslationExtension.php

-
message: "#^Call to method getValue\\(\\) on an unknown class Symfony\\\\Component\\\\Translation\\\\DataCollector\\\\Data\\.$#"
Copy link
Member Author

Choose a reason for hiding this comment

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

Will become useless once missing use will be added in symfony
See: symfony/symfony#35122

Copy link
Member

Choose a reason for hiding this comment

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

PHPStan will also give an error when a message is not used anymore 👍 Great way of gradually improving code quality

compliments to @ondrejmirtes 👏

Choose a reason for hiding this comment

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

Thank you 😊

count: 1
path: Controller/SymfonyProfilerController.php