Skip to content

Commit

Permalink
Support multiple entity managers
Browse files Browse the repository at this point in the history
Injecting the ManagerRegistry gives us access to the entire set of
entity managers; this means we can loop through them and collect all
available data.
  • Loading branch information
NanoSector authored and debesha committed Jun 28, 2024
1 parent 834c31f commit 7def9ca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
36 changes: 25 additions & 11 deletions DataCollector/HydrationDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,40 @@

namespace Debesha\DoctrineProfileExtraBundle\DataCollector;

use Debesha\DoctrineProfileExtraBundle\ORM\HydrationLogger;
use Doctrine\ORM\EntityManagerInterface;
use Debesha\DoctrineProfileExtraBundle\ORM\LoggingConfiguration;
use Debesha\DoctrineProfileExtraBundle\ORM\LoggingEntityManager;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;

class HydrationDataCollector extends DataCollector
{
private HydrationLogger $hydrationLogger;

public function __construct(EntityManagerInterface $manager)
{
$this->hydrationLogger = $manager->getConfiguration()->getHydrationLogger();
public function __construct(
private ManagerRegistry $managerRegistry,
) {
}

public function collect(Request $request, Response $response, \Throwable $exception = null): void
{
$this->data['hydrations'] = $this->hydrationLogger->hydrations;
$hydrationsPerEntityManager = array_map(
static function (ObjectManager $manager): array {
if (!$manager instanceof LoggingEntityManager) {
return [];
}

$configuration = $manager->getConfiguration();
if (!$configuration instanceof LoggingConfiguration) {
return [];
}

return $configuration->getHydrationLogger()->hydrations;
},
$this->managerRegistry->getManagers(),
);

$this->data['hydrations'] = array_merge(...array_values($hydrationsPerEntityManager));
}

public function getHydrations()
Expand All @@ -43,7 +59,7 @@ public function getHydrationsCount(): int
return count($this->data['hydrations']);
}

public function getTime(): int
public function getTime(): float
{
$time = 0;
foreach ($this->data['hydrations'] as $hydration) {
Expand All @@ -63,7 +79,5 @@ public function getName(): string
public function reset(): void
{
$this->data = [];
$this->hydrationLogger->hydrations = [];
$this->hydrationLogger->currentHydration = 0;
}
}
2 changes: 1 addition & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<services>
<service id="debesha.doctrine_extra_profiler.data_collector"
class="Debesha\DoctrineProfileExtraBundle\DataCollector\HydrationDataCollector" public="false">
<argument type="service" id="doctrine.orm.entity_manager"/>
<argument type="service" id="doctrine"/>
<tag name="data_collector" template="@DebeshaDoctrineProfileExtra/Collector/hydrations.html.twig"
id="hydrations"/>
</service>
Expand Down

0 comments on commit 7def9ca

Please sign in to comment.