Skip to content

Commit

Permalink
feat: Add command options and config to exclude tables and views
Browse files Browse the repository at this point in the history
  • Loading branch information
jawira committed Feb 5, 2024
1 parent eb976e4 commit 63c75c6
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"php": "^8.0",
"doctrine/doctrine-bundle": "^2.5",
"doctrine/orm": "^2.9 || ^3.0",
"jawira/db-draw": "^1.2",
"jawira/db-draw": "^1.6",
"jawira/plantuml-client": "^1.0",
"symfony/console": "^6.0 || ^7.0",
"symfony/filesystem": "^6.0 || ^7.0",
Expand Down
8 changes: 6 additions & 2 deletions src/Command/DoctrineDiagramCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ protected function configure(): void
->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::CONNECTION, null, InputOption::VALUE_REQUIRED, 'Doctrine connection to use.')
->addOption(Config::THEME, null, InputOption::VALUE_REQUIRED, 'Change diagram colors and style.');
->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.');
}

protected function execute(InputInterface $input, OutputInterface $output): int
Expand All @@ -61,8 +62,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$server = $this->stringOrNullOption($input, Config::SERVER);
$filename = $this->stringOrNullOption($input, Config::FILENAME);
$theme = $this->stringOrNullOption($input, Config::THEME);
$exclude = $this->stringOrNullOption($input, Config::EXCLUDE);

$puml = $this->doctrineDiagram->generatePuml($connectionName, $size, $theme);
$excludeArray = is_string($exclude) ? explode(',', $exclude) : null;

$puml = $this->doctrineDiagram->generatePuml($connectionName, $size, $theme, $excludeArray);
$content = $this->doctrineDiagram->convertWithServer($puml, $format, $server);
$fullName = $this->doctrineDiagram->dumpDiagram($content, $filename, $format);

Expand Down
1 change: 1 addition & 0 deletions src/Constants/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ class Config
public const SERVER = 'server';
public const CONNECTION = 'connection';
public const THEME = 'theme';
public const EXCLUDE = 'exclude';
}
7 changes: 5 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class Configuration implements ConfigurationInterface
public function getConfigTreeBuilder(): TreeBuilder
{
$buildTree = (new TreeBuilder('doctrine_diagram'));
/** @var ParentNodeDefinitionInterface $rootNode */
$rootNode = $buildTree->getRootNode();
$rootNode = $buildTree->getRootNode();
assert($rootNode instanceof ParentNodeDefinitionInterface);
$rootNode->children()
->enumNode(Config::SIZE)
->values([Size::MINI, Size::MIDI, Size::MAXI])
Expand All @@ -41,6 +41,9 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarNode(Config::CONNECTION)
->defaultValue(null)
->end()
->arrayNode(Config::EXCLUDE)
->scalarPrototype()->end()
->end()
->end();

return $buildTree;
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/DoctrineDiagramExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function load(array $configs, ContainerBuilder $container): void
$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]);
$container->setParameter('doctrine_diagram.' . Config::EXCLUDE, $config[Config::EXCLUDE]);

$loader = new PhpFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.php');
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
->arg('$format', param('doctrine_diagram.' . Config::FORMAT))
->arg('$server', param('doctrine_diagram.' . Config::SERVER))
->arg('$theme', param('doctrine_diagram.' . Config::THEME))
->arg('$connection', param('doctrine_diagram.' . Config::CONNECTION));
->arg('$connection', param('doctrine_diagram.' . Config::CONNECTION))
->arg('$exclude', param('doctrine_diagram.' . Config::EXCLUDE));

$services->set('jawira.doctrine_diagram.command', DoctrineDiagramCommand::class)
->arg('$doctrineDiagram', service('jawira.doctrine_diagram.service'))
Expand Down
13 changes: 9 additions & 4 deletions src/Service/DoctrineDiagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jawira\DoctrineDiagramBundle\Service;

use Doctrine\DBAL\Connection;
use Doctrine\Persistence\ConnectionRegistry;
use Jawira\DbDraw\DbDraw;
use Jawira\DoctrineDiagramBundle\Constants\Format;
Expand All @@ -26,7 +27,10 @@ public function __construct(
private string $theme,
/** This value comes from doctrine_diagram.yaml */
private ?string $connection,
) {
/** This value comes from doctrine_diagram.yaml */
private array $exclude,
)
{
}

/**
Expand All @@ -37,19 +41,20 @@ public function __construct(
*
* @param ?string $connectionName Doctrine connection name,this value comes from console.
*/
public function generatePuml(?string $connectionName = null, ?string $size = null, ?string $theme = null): string
public function generatePuml(?string $connectionName = null, ?string $size = null, ?string $theme = null, ?array $exclude = null): string
{
// Fallback values from doctrine_diagram.yaml
$connectionName ??= $this->connection;
$size ??= $this->size;
$theme ??= $this->theme;
$exclude ??= $this->exclude;

// Generate puml diagram
$connection = $this->doctrine->getConnection($connectionName);
/** @var \Doctrine\DBAL\Connection $connection */
assert($connection instanceof Connection);
$dbDraw = new DbDraw($connection);

return $dbDraw->generatePuml($size, $theme);
return $dbDraw->generatePuml($size, $theme, $exclude);
}

/**
Expand Down

0 comments on commit 63c75c6

Please sign in to comment.