Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trace information to Command/Query debugger #19854

Merged
merged 3 commits into from Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 37 additions & 3 deletions src/Core/CommandBus/ExecutedCommandRegistry.php
Expand Up @@ -24,6 +24,8 @@
* International Registered Trademark & Property of PrestaShop SA
*/

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
PierreRambaud marked this conversation as resolved.
Show resolved Hide resolved
&& $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