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

[Workflow] Add a profiler #51204

Merged
merged 1 commit into from
Aug 1, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ public function load(array $configs, ContainerBuilder $container)
$this->readConfigEnabled('translator', $container, $config['translator']);
$this->readConfigEnabled('property_access', $container, $config['property_access']);
$this->readConfigEnabled('profiler', $container, $config['profiler']);
$this->readConfigEnabled('workflows', $container, $config['workflows']);

// A translator must always be registered (as support is included by
// default in the Form and Validator component). If disabled, an identity
Expand Down Expand Up @@ -876,6 +877,10 @@ private function registerProfilerConfiguration(array $config, ContainerBuilder $
$loader->load('mailer_debug.php');
}

if ($this->isInitializedConfigEnabled('workflows')) {
$loader->load('workflow_debug.php');
}

if ($this->isInitializedConfigEnabled('http_client')) {
$loader->load('http_client_debug.php');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\Workflow\DataCollector\WorkflowDataCollector;

return static function (ContainerConfigurator $container) {
$container->services()
->set('data_collector.workflow', WorkflowDataCollector::class)
->tag('data_collector', [
'template' => '@WebProfiler/Collector/workflow.html.twig',
'id' => 'workflow',
])
->args([
tagged_iterator('workflow', 'name'),
])
;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{% extends '@WebProfiler/Profiler/layout.html.twig' %}

{% block menu %}
<span class="label {{ collector.workflows|length == 0 ? 'disabled' }}">
<span class="icon">
{{ source('@WebProfiler/Icon/workflow.svg') }}
</span>
<strong>Workflow</strong>
</span>
{% endblock %}

{% block panel %}
<h2>Workflow</h2>

{% if collector.workflows|length == 0 %}
<div class="empty empty-panel">
<p>There are no workflows configured.</p>
</div>
{% else %}
<script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
mermaid.initialize({
flowchart: { useMaxWidth: false },
securityLevel: 'loose',
});
// We do not load all mermaid diagrams at once, but only when the tab is opened
// This is because mermaid diagrams are in a tab, and cannot be renderer with a
// "good size" if they are not visible
document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('.tab').forEach((el) => {
const observer = new MutationObserver(() => {
if (!el.classList.contains('block')) {
return;
}
const mEl = el.querySelector('.sf-mermaid');
if (mEl.dataset.processed) {
return;
}
mermaid.run({
nodes: [mEl],
});
});
observer.observe(el, { attributeFilter: ['class'] });
});
});
</script>

<h2>Definitions</h2>
<div class="sf-tabs js-tabs">
{% for name, data in collector.workflows %}
<div class="tab">
<h3 class="tab-title">{{ name }}</h3>
<div class="tab-content">
<pre class="sf-mermaid">
{{ data.dump|raw }}
</pre>
</div>
</div>
{% endfor %}
</div>
{% endif %}
{% endblock %}
lyrixx marked this conversation as resolved.
Show resolved Hide resolved
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/Symfony/Component/Workflow/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Add `with-metadata` option to the `workflow:dump` command to include places,
transitions and workflow's metadata into dumped graph
* Add support for storing marking in a property
* Add a profiler

6.2
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Workflow\DataCollector;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
use Symfony\Component\Workflow\Dumper\MermaidDumper;
use Symfony\Component\Workflow\StateMachine;

/**
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
final class WorkflowDataCollector extends DataCollector implements LateDataCollectorInterface
{
public function __construct(
private readonly iterable $workflows,
) {
}

public function collect(Request $request, Response $response, \Throwable $exception = null): void
{
}

public function lateCollect(): void
{
foreach ($this->workflows as $workflow) {
$type = $workflow instanceof StateMachine ? MermaidDumper::TRANSITION_TYPE_STATEMACHINE : MermaidDumper::TRANSITION_TYPE_WORKFLOW;
$dumper = new MermaidDumper($type);
$this->data['workflows'][$workflow->getName()] = [
'dump' => $dumper->dump($workflow->getDefinition()),
];
}
}

public function getName(): string
{
return 'workflow';
}

public function reset(): void
{
$this->data = [];
}

public function getWorkflows(): array
{
return $this->data['workflows'] ?? [];
}
}