-
-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace DbalLogger by DebugMiddleware
- Loading branch information
1 parent
f9339b2
commit 9583584
Showing
10 changed files
with
704 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 $connWithBacktraces; | ||
|
||
/** @var array<string, array<string, mixed>[]> */ | ||
private $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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?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 | ||
{ | ||
/** @var DebugDataHolder */ | ||
private $debugDataHolder; | ||
|
||
/** @var Stopwatch|null */ | ||
private $stopwatch; | ||
|
||
/** @var string */ | ||
private $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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.