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 route, template and export #218

Merged
merged 1 commit into from
Sep 20, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Components/StateMachines/Mermaid.php
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 %}