Skip to content

Commit

Permalink
[Workflow] Add a profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx committed Aug 1, 2023
1 parent 5fcaa74 commit e90147a
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 0 deletions.
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,61 @@
{% extends '@WebProfiler/Profiler/layout.html.twig' %}

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

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

{% if collector.dumps|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';
// 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) {
console.log('A');
return;
}
mermaid.run({
flowchart: { useMaxWidth: false },
securityLevel: 'loose',
nodes: [mEl],
});
});
observer.observe(el, { attributeFilter: ['class'] });
});
});
</script>

<h2>Definitions</h2>
<div class="sf-tabs js-tabs">
{% for name, dump in collector.dumps %}
<div class="tab">
<h3 class="tab-title">{{ name }}</h3>
<div class="tab-content">
<pre class="sf-mermaid">
{{ dump|raw }}
</pre>
</div>
</div>
{% endfor %}
</div>
{% endif %}
{% endblock %}
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,58 @@
<?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)
{
}

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

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

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

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

0 comments on commit e90147a

Please sign in to comment.