diff --git a/Controller/SymfonyProfilerController.php b/Controller/SymfonyProfilerController.php index da003f54..306cfe2e 100644 --- a/Controller/SymfonyProfilerController.php +++ b/Controller/SymfonyProfilerController.php @@ -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; @@ -122,21 +123,11 @@ 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)); @@ -144,8 +135,13 @@ private function getMessage(Request $request, string $token): SfProfilerMessage $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 + ->setLocale($requestCollector->getLocale()) + ->setTranslation(\sprintf('[%s]', $message->getTranslation())) + ; } return $message; @@ -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) { @@ -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; + } } diff --git a/Resources/config/symfony_profiler.yaml b/Resources/config/symfony_profiler.yaml index 71e93e34..7420a6d5 100644 --- a/Resources/config/symfony_profiler.yaml +++ b/Resources/config/symfony_profiler.yaml @@ -6,6 +6,7 @@ services: - { name: 'data_collector', template: "@Translation/SymfonyProfiler/translation.html.twig", id: "translation", priority: 200 } Translation\Bundle\Controller\SymfonyProfilerController: + autowire: true public: true tags: ['container.service_subscriber'] arguments: diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index f36a783f..a8f8fdcd 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -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\\.$#" + count: 1 + path: Controller/SymfonyProfilerController.php