Skip to content

Commit

Permalink
Add route, template and export (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
Isengo1989 committed Sep 20, 2023
1 parent 6d3fc82 commit 2618975
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/Components/StateMachines/Mermaid.php
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);

namespace Frosh\Tools\Components\StateMachines;

use Shopware\Core\System\StateMachine\StateMachineEntity;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

final class Mermaid implements ExportInterface
{
private const DEFAULT_PATH = __DIR__ . '/../../Resources/views/administration/mermaid';

private readonly Environment $twig;

public function __construct()
{
$path = realpath(self::DEFAULT_PATH);
if (!\is_string($path)) {
throw new \RuntimeException('Could not find default path for PlantUML templates');
}

$this->twig = new Environment(new FilesystemLoader($path));
}

public function export(StateMachineEntity $stateMachine, string $title = ''): string
{
return $this->twig->render('state-machine.html.twig', [
'title' => $title,
'initialState' => $stateMachine->getInitialState(),
'states' => $stateMachine->getStates()?->getElements(),
'transitions' => $stateMachine->getTransitions()?->getElements(),
]);
}
}
31 changes: 31 additions & 0 deletions src/Controller/StateMachineController.php
Expand Up @@ -4,6 +4,7 @@
namespace Frosh\Tools\Controller;

use Frosh\Tools\Components\Planuml;
use Frosh\Tools\Components\StateMachines\Mermaid;
use Frosh\Tools\Components\StateMachines\Plantuml;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
Expand Down Expand Up @@ -51,4 +52,34 @@ public function load(Request $request): JsonResponse

return $response;
}

#[Route(path: '/state-machines/load-mermaid', name: 'api.frosh.tools.state-machines.load-mermaid', methods: ['GET'])]
public function loadMermaid(Request $request): JsonResponse
{
$stateMachineType = $request->query->get('stateMachine');

if (empty($stateMachineType)) {
return new JsonResponse();
}

$tmp = explode('.', $stateMachineType);
$title = ucwords(str_replace('_', ' ', $tmp[0]));

$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('technicalName', $stateMachineType));
$criteria->addAssociations([
'states',
'transitions',
]);

$stateMachine = $this->stateMachineRepository->search($criteria, Context::createDefaultContext())->first();

$exporter = new Mermaid();
$generatedMermaid = $exporter->export($stateMachine, $title);

$response = new JsonResponse($generatedMermaid);
$response->setData(['data' => $generatedMermaid]);

return $response;
}
}
18 changes: 18 additions & 0 deletions src/Resources/views/administration/mermaid/state-machine.html.twig
@@ -0,0 +1,18 @@
flowchart TD
{{ 'START_STATE[Start state] -->' }} {{ initialState.getId() }}

{% for state in states %}
{{ state.getId() }}({{ state.getName()|replace({'(': "", ')': ""}) }})
{% set temp %}{% apply spaceless %}
{% for transition in transitions %}
{% if transition.getFromStateId() == state.getId() and transition.getActionName() != 'reopen' %}yes{% else %}{% endif %}
{% endfor %}{% endapply %}
{% endset %}
{% if temp|trim is empty %}
{{ state.getId() }} {{ '--> FINAL_STATE[Final state]' }}
{% endif %}
{% endfor %}

{% for transition in transitions %}
{{ transition.getFromStateId() }} {{ '--' }} {{ transition.getActionName() }} {{ '-->' }} {{ transition.getToStateId() }}
{% endfor %}

0 comments on commit 2618975

Please sign in to comment.