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

refactor: Rename parameter/config from "extension" to "format" #22

Merged
merged 1 commit into from
Jan 11, 2024
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
18 changes: 11 additions & 7 deletions src/Command/DoctrineDiagramCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,40 @@
use Doctrine\Persistence\ConnectionRegistry;
use Jawira\DbDraw\DbDraw;
use Jawira\DoctrineDiagramBundle\Constants\Config;
use Jawira\DoctrineDiagramBundle\Constants\Size;
use Jawira\DoctrineDiagramBundle\Service\DoctrineDiagram;
use Jawira\PlantUmlClient\Client;
use Jawira\PlantUmlClient\Format;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;


/**
* @author Jawira Portugal
*/
#[AsCommand('doctrine:diagram')]
class DoctrineDiagramCommand extends Command
{

public function __construct(private DoctrineDiagram $doctrineDiagram, string $name)
public function __construct(private DoctrineDiagram $doctrineDiagram)
{
parent::__construct($name);
parent::__construct();
}

protected function configure(): void
{
$this->setDescription('Create database diagram.')
->addUsage('--connection=default')
->addUsage('--size=mini')
->addUsage('--filename=my-diagram --extension=png')
->addUsage(sprintf('--%s=%s', Config::SIZE, Size::MINI))
->addUsage(sprintf('--%s=my-diagram --%s=png', Config::FILENAME, Config::FORMAT))
->addUsage(sprintf('--%s=default', Config::CONNECTION))
->addOption(Config::SIZE, null, InputOption::VALUE_REQUIRED, 'Diagram size (mini, midi or maxi)')
->addOption(Config::FILENAME, null, InputOption::VALUE_REQUIRED, 'Destination file name.')
->addOption(Config::EXTENSION, null, InputOption::VALUE_REQUIRED, 'Diagram format (svg, png or puml).')
->addOption(Config::FORMAT, null, InputOption::VALUE_REQUIRED, 'Diagram format (svg, png or puml).')
->addOption(Config::CONNECTION, null, InputOption::VALUE_REQUIRED, 'Doctrine connection to use.')
->addOption(Config::SERVER, null, InputOption::VALUE_REQUIRED, 'PlantUML server URL, used to convert puml diagrams to svg and png.');
}
Expand All @@ -46,7 +50,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$connectionName = $input->getOption(Config::CONNECTION);
$size = $input->getOption(Config::SIZE);
$format = $input->getOption(Config::EXTENSION);
$format = $input->getOption(Config::FORMAT);
$server = $input->getOption(Config::SERVER);
$filename = $input->getOption(Config::FILENAME);

Expand Down
2 changes: 1 addition & 1 deletion src/Constants/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Config
{
public const SIZE = 'size';
public const FILENAME = 'filename';
public const EXTENSION = 'extension';
public const FORMAT = 'format';
public const SERVER = 'server';
public const CONNECTION = 'connection';
}
2 changes: 1 addition & 1 deletion src/Constants/Fallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Fallback
{
public const SIZE = Size::MIDI;
public const FILENAME = 'database';
public const EXTENSION = Format::SVG;
public const FORMAT = Format::SVG;
public const SERVER = 'http//www.plantuml.com/plantuml';
public const CONNECTION = 'default';
}
4 changes: 2 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarNode(Config::FILENAME)
->defaultValue(Fallback::FILENAME)
->end()
->enumNode(Config::EXTENSION)
->enumNode(Config::FORMAT)
->values([Format::PUML, Format::PNG, Format::SVG])
->defaultValue(Fallback::EXTENSION)
->defaultValue(Fallback::FORMAT)
->end()
->scalarNode(Config::SERVER)
->defaultValue(Fallback::SERVER)
Expand Down
10 changes: 4 additions & 6 deletions src/DependencyInjection/DoctrineDiagramExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace Jawira\DoctrineDiagramBundle\DependencyInjection;

use Jawira\DoctrineDiagramBundle\Constants\Config;
use Jawira\DoctrineDiagramBundle\Constants\Fallback;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;

class DoctrineDiagramExtension extends Extension
{
Expand All @@ -17,14 +16,13 @@ public function load(array $configs, ContainerBuilder $container): void
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);


$container->setParameter('doctrine_diagram.' . Config::SIZE, $config[Config::SIZE]);
$container->setParameter('doctrine_diagram.' . Config::FILENAME, $config[Config::FILENAME]);
$container->setParameter('doctrine_diagram.' . Config::EXTENSION, $config[Config::EXTENSION]);
$container->setParameter('doctrine_diagram.' . Config::FORMAT, $config[Config::FORMAT]);
$container->setParameter('doctrine_diagram.' . Config::SERVER, $config[Config::SERVER]);
$container->setParameter('doctrine_diagram.' . Config::CONNECTION, $config[Config::CONNECTION]);

$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
$loader = new PhpFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.php');
}
}
24 changes: 24 additions & 0 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Jawira\DoctrineDiagramBundle\Command\DoctrineDiagramCommand;
use Jawira\DoctrineDiagramBundle\Constants\Config;
use Jawira\DoctrineDiagramBundle\Service\DoctrineDiagram;

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

$services->set('jawira.doctrine_diagram.doctrine_diagram', DoctrineDiagram::class)
->arg('$doctrine', service('doctrine'))
->arg('$size', param('doctrine_diagram.' . Config::SIZE))
->arg('$filename', param('doctrine_diagram.' . Config::FILENAME))
->arg('$format', param('doctrine_diagram.' . Config::FORMAT))
->arg('$server', param('doctrine_diagram.' . Config::SERVER))
->arg('$connection', param('doctrine_diagram.' . Config::CONNECTION));

$services->set('jawira.doctrine_diagram.doctrine_diagram_command', DoctrineDiagramCommand::class)
->arg('$doctrineDiagram', service('jawira.doctrine_diagram.doctrine_diagram'))
->tag('console.command')
->private();
};
25 changes: 0 additions & 25 deletions src/Resources/config/services.xml

This file was deleted.

14 changes: 7 additions & 7 deletions src/Service/DoctrineDiagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(
/** This value comes from doctrine_diagram.yaml */
private string $filename,
/** This value comes from doctrine_diagram.yaml */
private string $extension,
private string $format,
/** This value comes from doctrine_diagram.yaml */
private string $server,
/** This value comes from doctrine_diagram.yaml */
Expand Down Expand Up @@ -59,7 +59,7 @@ public function generatePuml(?string $connectionName = null, ?string $size = nul
public function convertWithServer(string $puml, ?string $format = null, ?string $server = null): string
{
// Fallback values from doctrine_diagram.yaml
$format ??= $this->extension;
$format ??= $this->format;
$server ??= $this->server;


Expand All @@ -80,16 +80,16 @@ public function convertWithServer(string $puml, ?string $format = null, ?string
*
* @param string $content The diagram content to be dumped.
* @param string|null $filename Target file name.
* @param string|null $extension Target file extension.
* @param string|null $format Target file extension.
* @return string
*/
public function dumpDiagram(string $content, ?string $filename = null, ?string $extension = null): string
public function dumpDiagram(string $content, ?string $filename = null, ?string $format = null): string
{
// Fallback values from doctrine_diagram.yaml
$filename ??= $this->filename;
$extension ??= $this->extension;
$filename ??= $this->filename;
$format ??= $this->format;

$fullName = str_ends_with($filename, ".$extension") ? $filename : "$filename.$extension";
$fullName = str_ends_with($filename, ".$format") ? $filename : "$filename.$format";
(new Filesystem)->dumpFile($fullName, $content);

return $fullName;
Expand Down