From 1c2addaeb52ce7d5248cf65f54785a84bf8f0d07 Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Tue, 16 Jul 2024 11:46:48 +0200 Subject: [PATCH] Drop usage of ContainerAwareInterface --- src/Controller/ProfilerController.php | 42 ++++++++++++--------------- src/Resources/config/profiler.xml | 7 +++++ 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/Controller/ProfilerController.php b/src/Controller/ProfilerController.php index b5a7b025..57125582 100644 --- a/src/Controller/ProfilerController.php +++ b/src/Controller/ProfilerController.php @@ -6,49 +6,45 @@ use Facile\MongoDbBundle\DataCollector\MongoDbDataCollector; use Facile\MongoDbBundle\DataCollector\MongoQuerySerializer; +use Facile\MongoDbBundle\Services\Explain\ExplainQueryService; use MongoDB\BSON\UTCDateTime; -use Symfony\Component\DependencyInjection\Container; -use Symfony\Component\DependencyInjection\ContainerAwareInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpKernel\Profiler\Profiler; -class ProfilerController implements ContainerAwareInterface +class ProfilerController { - private ?ContainerInterface $container = null; + private ExplainQueryService $explain; - /** - * Sets the container. - * - * @param ContainerInterface|null $container A ContainerInterface instance or null - */ - public function setContainer(ContainerInterface $container = null): void + private ?Profiler $profiler; + + public function __construct(ExplainQueryService $explain, ?Profiler $profiler) { - $this->container = $container; + $this->explain = $explain; + $this->profiler = $profiler; } - /** - * @throws \Exception - */ public function explainAction(string $token, $queryNumber): JsonResponse { - /** @var Profiler $profiler */ - $profiler = $this->container->get('profiler'); - $profiler->disable(); + $this->profiler->disable(); + + $profile = $this->profiler->loadProfile($token); + if (! $profile) { + throw new \RuntimeException('No profile found'); + } - $profile = $profiler->loadProfile($token); - /** @var MongoDbDataCollector $dataCollector */ $dataCollector = $profile->getCollector('mongodb'); + if (! $dataCollector instanceof MongoDbDataCollector) { + throw new \RuntimeException('MongoDb data collector not found'); + } + $queries = $dataCollector->getQueries(); $query = $queries[$queryNumber]; $query->setFilters($this->walkAndConvertToUTCDatetime($query->getFilters())); - $service = $this->container->get('mongo.explain_query_service'); - try { - $result = $service->execute($query); + $result = $this->explain->execute($query); } catch (\InvalidArgumentException $e) { return new JsonResponse([ 'err' => $e->getMessage(), diff --git a/src/Resources/config/profiler.xml b/src/Resources/config/profiler.xml index a77cff53..404773b2 100644 --- a/src/Resources/config/profiler.xml +++ b/src/Resources/config/profiler.xml @@ -42,5 +42,12 @@ + + + + + + +