Skip to content

Commit

Permalink
Drop usage of ContainerAwareInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean85 committed Jul 16, 2024
1 parent 8cd66b3 commit 1c2adda
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
42 changes: 19 additions & 23 deletions src/Controller/ProfilerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
7 changes: 7 additions & 0 deletions src/Resources/config/profiler.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,12 @@
<tag name="twig.extension"/>
</service>

<!-- controller -->

<service id="facile_mongo_db.profiler_controller" class="Facile\MongoDbBundle\Controller\ProfilerController">
<argument type="service" id="mongo.explain_query_service" />
<argument type="service" id="profiler" on-invalid="null" />
</service>

</services>
</container>

0 comments on commit 1c2adda

Please sign in to comment.