Skip to content

Commit

Permalink
Merge pull request #19854 from eternoendless/improve-crqs-collector
Browse files Browse the repository at this point in the history
Add trace information to Command/Query debugger
  • Loading branch information
Progi1984 committed Jun 19, 2020
2 parents 7f37c46 + 5c7ffb6 commit 1f47005
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
40 changes: 37 additions & 3 deletions src/Core/CommandBus/ExecutedCommandRegistry.php
Expand Up @@ -24,6 +24,8 @@
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

declare(strict_types=1);

namespace PrestaShop\PrestaShop\Core\CommandBus;

use PrestaShop\PrestaShop\Core\CommandBus\Parser\CommandTypeParser;
Expand All @@ -33,6 +35,8 @@
*/
final class ExecutedCommandRegistry
{
private const BACKTRACE_LIMIT = 10;

/**
* @var array
*/
Expand All @@ -58,24 +62,28 @@ public function __construct(CommandTypeParser $commandTypeParser)
* @param object $command
* @param object $handler
*/
public function register($command, $handler)
public function register($command, $handler): void
{
$commandClass = get_class($command);
$handlerClass = get_class($handler);

$type = $this->commandTypeParser->parse($commandClass);

$trace = $this->getTrace();

switch ($type) {
case 'Command':
$this->registry['commands'][] = [
'command' => $commandClass,
'command_handler' => $handlerClass,
'trace' => $trace,
];
break;
case 'Query':
$this->registry['queries'][] = [
'query' => $commandClass,
'query_handler' => $handlerClass,
'trace' => $trace,
];
break;
}
Expand All @@ -84,16 +92,42 @@ public function register($command, $handler)
/**
* @return array
*/
public function getExecutedCommands()
public function getExecutedCommands(): array
{
return $this->registry['commands'];
}

/**
* @return array
*/
public function getExecutedQueries()
public function getExecutedQueries(): array
{
return $this->registry['queries'];
}

/**
* Returns the file and line that invoked the handle method
*
* @return array
*/
private function getTrace(): array
{
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, self::BACKTRACE_LIMIT);

foreach ($trace as $step) {
if ($step['class'] === TacticianCommandBusAdapter::class
&& $step['function'] === 'handle'
) {
return [
'file' => $step['file'],
'line' => $step['line'],
];
}
}

return [
'file' => 'Unknown',
'line' => 0,
];
}
}
Expand Up @@ -64,50 +64,56 @@
{% endblock %}

{% block panel %}
<h2>Commands / Queries</h2>
<h2>Commands</h2>

<div class="sf-toolbar-info-piece">
<table class="sf-toolbar-ajax-requests">
<table id="cqrs-commands-log">
<thead>
<tr>
<th>Command</th>
<th>Command handler</th>
<th>Command Handler</th>
<th>Called from</th>
</tr>
</thead>
<tbody class="sf-toolbar-ajax-request-list">
{% if collector.executedCommands is not empty %}
{% for command in collector.executedCommands %}
<tr>
<td>{{ command.command }}</td>
<td>{{ command.command_handler }}</td>
<td class="text-small">{{ command.command }}</td>
<td class="text-small">{{ command.command_handler }}</td>
<td class="text-small">{{ command.trace.file }}:{{ command.trace.line }}</td>
</tr>
{% endfor %}
{% else %}
<tr>
<td colspan="2">No Commands where executed during request.</td>
<td colspan="3" class="font-normal">No Commands where executed during request.</td>
</tr>
{% endif %}
</tbody>
</table>

<table class="sf-toolbar-ajax-requests">
<h2>Queries</h2>

<table id="cqrs-queries-log">
<thead>
<tr>
<th>Query</th>
<th>Query Handler</th>
<th>Called from</th>
</tr>
</thead>
<tbody class="sf-toolbar-ajax-request-list">
{% if collector.executedQueries is not empty %}
{% for query in collector.executedQueries %}
<tr>
<td>{{ query.query }}</td>
<td>{{ query.query_handler }}</td>
<td class="text-small">{{ query.query }}</td>
<td class="text-small">{{ query.query_handler }}</td>
<td class="text-small">{{ query.trace.file }}:{{ query.trace.line }}</td>
</tr>
{% endfor %}
{% else %}
<tr>
<td colspan="2">No Queries where executed during request.</td>
<td colspan="3" class="font-normal">No Queries where executed during request.</td>
</tr>
{% endif %}
</tbody>
Expand Down

0 comments on commit 1f47005

Please sign in to comment.