Last active
June 24, 2021 10:14
-
-
Save zabaala/8b512f199c5b7d071eb85e1cac27d110 to your computer and use it in GitHub Desktop.
Debug all Symfony Queries
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
<?php | |
namespace Doctrine\DBAL\Logging; | |
use Doctrine\Common\Collections\ArrayCollection; | |
use Symfony\Component\HttpKernel\KernelInterface; | |
use function microtime; | |
use function exec; | |
/** | |
* Includes executed SQLs in a Debug Stack. | |
*/ | |
class DebugStack implements SQLLogger | |
{ | |
/** | |
* Executed SQL queries. | |
* | |
* @var mixed[][] | |
*/ | |
public $queries = []; | |
/** @var float|null */ | |
public $start = null; | |
/** @var int */ | |
public $currentQuery = 0; | |
/** | |
* {@inheritdoc} | |
*/ | |
public function startQuery($sql, ?array $params = null, ?array $types = null) | |
{ | |
/** | |
* To start debug, comment return below... | |
*/ | |
return; | |
/** | |
* Set full path to log file. | |
* Full path definition improve better performance to loggin. | |
* At least 8 times faster. | |
*/ | |
$rootDir = '/Users/mauriciorodrigues/_dev/'; | |
$file = "{$rootDir}/dump.log"; | |
$this->start = microtime(true); | |
$this->queries[++$this->currentQuery] = ['sql' => $sql, 'params' => $params, 'types' => $types, 'executionMS' => 0]; | |
$content = sprintf('{"sql": %s, "params":[%s]}', | |
($sql ?? null), | |
is_array($params) ? join(',', $params) : $params | |
); | |
file_put_contents($file, $content . "\n", FILE_APPEND); | |
} | |
public function stopQuery() | |
{ | |
// TODO: Implement stopQuery() method. | |
} | |
} |
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
<?php | |
namespace Doctrine\DBAL\Logging; | |
use Doctrine\Common\Collections\ArrayCollection; | |
use function microtime; | |
/** | |
* Includes executed SQLs in a Debug Stack. | |
*/ | |
class DebugStack implements SQLLogger | |
{ | |
/** | |
* Executed SQL queries. | |
* | |
* @var mixed[][] | |
*/ | |
public $queries = []; | |
public $distinctedQueries = []; | |
/** | |
* If Debug Stack is enabled (log queries) or not. | |
* | |
* @var bool | |
*/ | |
public $enabled = true; | |
/** @var float|null */ | |
public $start = null; | |
/** @var int */ | |
public $currentQuery = 0; | |
/** | |
* {@inheritdoc} | |
*/ | |
public function startQuery($sql, ?array $params = null, ?array $types = null) | |
{ | |
if (! $this->enabled) { | |
return; | |
} | |
$this->start = microtime(true); | |
$this->queries[++$this->currentQuery] = ['sql' => $sql, 'params' => $params, 'types' => $types, 'executionMS' => 0]; | |
if (isset($params[0])) { | |
$this->distinctedQueries[$sql]['params'][] = $params[0]; | |
} else { | |
$this->distinctedQueries[$sql]['params'] = null; | |
} | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function stopQuery() | |
{ | |
if (! $this->enabled) { | |
return; | |
} | |
$this->queries[$this->currentQuery]['executionMS'] = microtime(true) - $this->start; | |
} | |
public function __destruct() | |
{ | |
$queries = []; | |
foreach ($this->distinctedQueries as $query => $params) { | |
if (is_array($params["params"])) { | |
$query = str_replace( | |
'= ?', | |
'in (' . implode(', ', $params["params"]) . ');', | |
$query | |
); | |
} | |
$queries[] = $query; | |
} | |
dump($queries); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment