Skip to content

Commit

Permalink
feat: Add jar converter
Browse files Browse the repository at this point in the history
  • Loading branch information
jawira committed Mar 13, 2024
1 parent 0828af5 commit f5dc178
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
</target>

<target name="visualizer" unless="uptodate.visualizer" description="Create buildfile map">
<visualizer format="svg"/>
<visualizer format="svg" footer="Phing buildfile"/>
</target>

<target name="mkdocs:serve" description="Run local server">
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"doctrine/orm": "^2.9 || ^3.0",
"jawira/db-draw": "^1.6",
"jawira/plantuml-client": "^1.0",
"jawira/plantuml-to-image": "^1.1",
"symfony/console": "^6.0 || ^7.0",
"symfony/filesystem": "^6.0 || ^7.0",
"symfony/framework-bundle": "^6.0 || ^7.0"
Expand All @@ -39,7 +40,7 @@
"phpstan/phpstan-symfony": "^1.3.7"
},
"suggest": {
"jawira/plantuml": "Install PlantUML executable with Composer."
"jawira/plantuml": "Install PlantUML executable/jar with Composer."
},
"autoload": {
"psr-4": {
Expand Down
13 changes: 11 additions & 2 deletions src/Command/DoctrineDiagramCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Jawira\DoctrineDiagramBundle\Command;

use Jawira\DoctrineDiagramBundle\Constants\Config;
use Jawira\DoctrineDiagramBundle\Constants\Converter;
use Jawira\DoctrineDiagramBundle\Constants\Format;
use Jawira\DoctrineDiagramBundle\Constants\Size;
use Jawira\DoctrineDiagramBundle\Service\DoctrineDiagram;
Expand Down Expand Up @@ -45,7 +46,9 @@ protected function configure(): void
->addOption(Config::FILENAME, null, InputOption::VALUE_REQUIRED, 'Destination file name.')
->addOption(Config::FORMAT, null, InputOption::VALUE_REQUIRED, sprintf('Diagram format (<info>%s</info>, <info>%s</info> or <info>%s</info>).', Format::SVG, Format::PNG, Format::PUML))
->addOption(Config::SIZE, null, InputOption::VALUE_REQUIRED, sprintf('Diagram size (<info>%s</info>, <info>%s</info> or <info>%s</info>).', Size::MINI, Size::MIDI, Size::MAXI))
->addOption(Config::SERVER, null, InputOption::VALUE_REQUIRED, 'PlantUML server URL, only used to convert puml diagrams to svg and png.')
->addOption(Config::CONVERTER, null, InputOption::VALUE_REQUIRED, 'Which strategy will be used to convert puml to another format.', Converter::AUTO, Converter::JAR, Converter::SERVER)
->addOption(Config::SERVER, null, InputOption::VALUE_REQUIRED, 'PlantUML server URL, used to convert puml diagrams to svg or png.')
->addOption(Config::JAR, null, InputOption::VALUE_REQUIRED, 'Path to plantuml.jar, used to convert puml diagrams to svg or png.')
->addOption(Config::CONNECTION, null, InputOption::VALUE_REQUIRED, 'Doctrine connection to use.')
->addOption(Config::THEME, null, InputOption::VALUE_REQUIRED, 'Change diagram colors and style.')
->addOption(Config::EXCLUDE, null, InputOption::VALUE_REQUIRED, 'Comma separated list of tables to exclude from diagram.');
Expand All @@ -60,14 +63,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$size = $this->stringOrNullOption($input, Config::SIZE);
$format = $this->stringOrNullOption($input, Config::FORMAT);
$server = $this->stringOrNullOption($input, Config::SERVER);
$converter = $this->stringOrNullOption($input, Config::CONVERTER);
$jar = $this->stringOrNullOption($input, Config::JAR);
$filename = $this->stringOrNullOption($input, Config::FILENAME);
$theme = $this->stringOrNullOption($input, Config::THEME);
$exclude = $this->stringOrNullOption($input, Config::EXCLUDE);

$excludeArray = is_string($exclude) ? explode(',', $exclude) : null;

$puml = $this->doctrineDiagram->generatePuml($connectionName, $size, $theme, $excludeArray);
$content = $this->doctrineDiagram->convertWithServer($puml, $format, $server);
$content = match ($converter) {
Converter::AUTO => $this->doctrineDiagram->convertAuto($puml, $format, $server, $jar),
Converter::JAR => $this->doctrineDiagram->convertWithJar($puml, $format, $jar),
Converter::SERVER => $this->doctrineDiagram->convertWithServer($puml, $format, $server),
};
$fullName = $this->doctrineDiagram->dumpDiagram($content, $filename, $format);

$errorStyle->success("Diagram: $fullName");
Expand Down
2 changes: 2 additions & 0 deletions src/Constants/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Config
public const FILENAME = 'filename';
public const FORMAT = 'format';
public const SERVER = 'server';
public const JAR = 'jar';
public const CONVERTER = 'converter';
public const CONNECTION = 'connection';
public const THEME = 'theme';
public const EXCLUDE = 'exclude';
Expand Down
13 changes: 13 additions & 0 deletions src/Constants/Converter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace Jawira\DoctrineDiagramBundle\Constants;

/**
* @internal
*/
class Converter
{
public const JAR = 'jar';
public const SERVER = 'server';
public const AUTO = 'auto';
}
1 change: 1 addition & 0 deletions src/Constants/Fallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ class Fallback
public const FORMAT = Format::SVG;
public const SERVER = 'http://www.plantuml.com/plantuml';
public const THEME = '_none_';
public const CONVERTER = Converter::AUTO;
}
7 changes: 7 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Jawira\DoctrineDiagramBundle\DependencyInjection;

use Jawira\DoctrineDiagramBundle\Constants\Config;
use Jawira\DoctrineDiagramBundle\Constants\Converter;
use Jawira\DoctrineDiagramBundle\Constants\Fallback;
use Jawira\DoctrineDiagramBundle\Constants\Size;
use Jawira\DoctrineDiagramBundle\Constants\Format;
Expand Down Expand Up @@ -33,6 +34,12 @@ public function getConfigTreeBuilder(): TreeBuilder
->values([Format::PUML, Format::PNG, Format::SVG])
->defaultValue(Fallback::FORMAT)
->end()
->scalarNode(Config::CONVERTER)
->defaultValue(Fallback::CONVERTER)
->end()
->scalarNode(Config::JAR)
->defaultValue(null)
->end()
->scalarNode(Config::SERVER)
->defaultValue(Fallback::SERVER)
->end()
Expand Down
2 changes: 2 additions & 0 deletions src/DependencyInjection/DoctrineDiagramExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function load(array $configs, ContainerBuilder $container): void
$container->setParameter('doctrine_diagram.' . Config::SIZE, $config[Config::SIZE]);
$container->setParameter('doctrine_diagram.' . Config::FILENAME, $config[Config::FILENAME]);
$container->setParameter('doctrine_diagram.' . Config::FORMAT, $config[Config::FORMAT]);
$container->setParameter('doctrine_diagram.' . Config::CONVERTER, $config[Config::CONVERTER]);
$container->setParameter('doctrine_diagram.' . Config::JAR, $config[Config::JAR]);
$container->setParameter('doctrine_diagram.' . Config::SERVER, $config[Config::SERVER]);
$container->setParameter('doctrine_diagram.' . Config::THEME, $config[Config::THEME]);
$container->setParameter('doctrine_diagram.' . Config::CONNECTION, $config[Config::CONNECTION]);
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@
use Jawira\DoctrineDiagramBundle\Constants\Config;
use Jawira\DoctrineDiagramBundle\Service\DoctrineDiagram;
use Jawira\DoctrineDiagramBundle\Service\Toolbox;
use Jawira\PlantUmlToImage\PlantUml;

return function (ContainerConfigurator $container): void {
$services = $container->services();

$services->set('.jawira.doctrine_diagram.plantuml_to_image', PlantUml::class);
$services->set('.jawira.doctrine_diagram.toolbox', Toolbox::class);

$services->set('jawira.doctrine_diagram.service', DoctrineDiagram::class)
->arg('$doctrine', service('doctrine'))
->arg('$pumlToImage', service('.jawira.doctrine_diagram.plantuml_to_image'))
->arg('$toolbox', service('.jawira.doctrine_diagram.toolbox'))
->arg('$size', param('doctrine_diagram.' . Config::SIZE))
->arg('$filename', param('doctrine_diagram.' . Config::FILENAME))
->arg('$format', param('doctrine_diagram.' . Config::FORMAT))
->arg('$jar', param('doctrine_diagram.' . Config::JAR))
->arg('$server', param('doctrine_diagram.' . Config::SERVER))
->arg('$theme', param('doctrine_diagram.' . Config::THEME))
->arg('$connection', param('doctrine_diagram.' . Config::CONNECTION))
Expand Down
39 changes: 38 additions & 1 deletion src/Service/DoctrineDiagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Jawira\DbDraw\DbDraw;
use Jawira\DoctrineDiagramBundle\Constants\Format;
use Jawira\PlantUmlClient\Client;
use Jawira\PlantUmlToImage\PlantUml;
use RuntimeException;
use Symfony\Component\Filesystem\Filesystem;

Expand All @@ -17,16 +18,19 @@ class DoctrineDiagram
{
public function __construct(
private ConnectionRegistry $doctrine,
private PlantUml $pumlToImage,
private Toolbox $toolbox,
private string $size,
private string $filename,
private string $format,
private ?string $jar,
private string $server,
private string $theme,
private ?string $connection,
/** @var string[] */
private array $exclude,
) {
)
{
}

/**
Expand All @@ -53,6 +57,39 @@ public function generatePuml(?string $connectionName = null, ?string $size = nul
return $dbDraw->generatePuml($size, $theme, $exclude);
}

/**
* Convert diagram with jar file if it's available, or use server otherwise.
*/
public function convertAuto(string $puml, ?string $format = null, ?string $server = null, ?string $jar = null): string
{
// Fallback values from doctrine_diagram.yaml
$jar ??= $this->jar;

if (is_string($jar)) {
$this->pumlToImage->setJar($jar);
}
if ($this->pumlToImage->isPlantUmlAvailable()) {
return $this->convertWithJar($puml, $format, $jar);
}

return $this->convertWithServer($puml, $format, $server);
}

/**
* Converts PlantUml diagram using jar file or executable as fallback.
*/
public function convertWithJar(string $puml, ?string $format = null, ?string $jar = null): string
{
// Fallback values from doctrine_diagram.yaml
$format ??= $this->format;
$jar ??= $this->jar;

if (is_string($jar)) {
$this->pumlToImage->setJar($jar);
}
return $this->pumlToImage->convertTo($puml, $format);
}

/**
* Convert diagram from Puml to another format using remote PlantUML server.
*
Expand Down

0 comments on commit f5dc178

Please sign in to comment.