welcome back to dyb-tech

This commit is contained in:
Daniel Guzman
2024-05-18 02:28:01 +02:00
parent 9513cdba09
commit 9f30bc98c7
6149 changed files with 668407 additions and 0 deletions
@@ -0,0 +1,92 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Middleware;
use Symfony\Bridge\Doctrine\Middleware\Debug\DebugDataHolder;
use Symfony\Bridge\Doctrine\Middleware\Debug\Query;
use function array_slice;
use function debug_backtrace;
use function in_array;
use const DEBUG_BACKTRACE_IGNORE_ARGS;
/** @psalm-suppress MissingDependency */
class BacktraceDebugDataHolder extends DebugDataHolder
{
/** @var string[] */
private array $connWithBacktraces;
/** @var array<string, array<string, mixed>[]> */
private array $backtraces = [];
/** @param string[] $connWithBacktraces */
public function __construct(array $connWithBacktraces)
{
$this->connWithBacktraces = $connWithBacktraces;
}
public function reset(): void
{
parent::reset();
$this->backtraces = [];
}
public function addQuery(string $connectionName, Query $query): void
{
parent::addQuery($connectionName, $query);
if (! in_array($connectionName, $this->connWithBacktraces, true)) {
return;
}
// array_slice to skip middleware calls in the trace
$this->backtraces[$connectionName][] = array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 2);
}
/** @return array<string, array<string, mixed>[]> */
public function getData(): array
{
$dataWithBacktraces = [];
$data = parent::getData();
foreach ($data as $connectionName => $dataForConn) {
$dataWithBacktraces[$connectionName] = $this->getDataForConnection($connectionName, $dataForConn);
}
return $dataWithBacktraces;
}
/**
* @param mixed[][] $dataForConn
*
* @return mixed[][]
*/
private function getDataForConnection(string $connectionName, array $dataForConn): array
{
$data = [];
foreach ($dataForConn as $idx => $record) {
$data[] = $this->addBacktracesIfAvailable($connectionName, $record, $idx);
}
return $data;
}
/**
* @param mixed[] $record
*
* @return mixed[]
*/
private function addBacktracesIfAvailable(string $connectionName, array $record, int $idx): array
{
if (! isset($this->backtraces[$connectionName])) {
return $record;
}
$record['backtrace'] = $this->backtraces[$connectionName][$idx];
return $record;
}
}
@@ -0,0 +1,8 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Middleware;
interface ConnectionNameAwareInterface
{
public function setConnectionName(string $name): void;
}
@@ -0,0 +1,32 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Middleware;
use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\Middleware;
use Symfony\Bridge\Doctrine\Middleware\Debug\DebugDataHolder;
use Symfony\Bridge\Doctrine\Middleware\Debug\Driver;
use Symfony\Component\Stopwatch\Stopwatch;
class DebugMiddleware implements Middleware, ConnectionNameAwareInterface
{
private DebugDataHolder $debugDataHolder;
private ?Stopwatch $stopwatch;
private string $connectionName = 'default';
public function __construct(DebugDataHolder $debugDataHolder, ?Stopwatch $stopwatch)
{
$this->debugDataHolder = $debugDataHolder;
$this->stopwatch = $stopwatch;
}
public function setConnectionName(string $name): void
{
$this->connectionName = $name;
}
public function wrap(DriverInterface $driver): DriverInterface
{
return new Driver($driver, $this->debugDataHolder, $this->stopwatch, $this->connectionName);
}
}