Commit 33404913 authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Tentative de refactoring.

parent 27b8bc8b
......@@ -29,7 +29,7 @@
},
"minimum-stability": "stable",
"require": {
"php": "^7.1",
"php": ">= 7.1",
"ext-json": "*",
"doctrine/doctrine-bundle": "^1.6",
"doctrine/orm": "^2.5",
......@@ -75,6 +75,7 @@
"test:phpcpd": "@php vendor/bin/phpcpd --fuzzy src",
"test:phpmd": "@php vendor/bin/phpmd src text phpmd-ruleset.xml",
"test:phpstan": "@php vendor/bin/phpstan analyse",
"test:composer-require-checker": "@php vendor/bin/composer-require-checker"
"test:composer-require-checker": "@php vendor/bin/composer-require-checker",
"test:phpunit": "echo Pas de tests PHPUNIT"
}
}
......@@ -23,6 +23,7 @@ namespace Irstea\PlantUmlBundle\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* Class ListCommand.
......@@ -34,20 +35,18 @@ class ListCommand extends Command
*/
private const CMD_NAME = 'irstea:plantuml:list';
/**
* @var string[]
*/
private $graphNames;
/** @var ServiceLocator */
private $graphLocator;
/**
* ListCommand constructor.
*
* @param string[] $graphNames
*/
public function __construct(array $graphNames)
public function __construct(ServiceLocator $graphLocator)
{
parent::__construct(self::CMD_NAME);
$this->graphNames = $graphNames;
$this->graphLocator = $graphLocator;
}
/**
......@@ -65,7 +64,8 @@ class ListCommand extends Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
foreach ($this->graphNames as $name) {
$names = array_keys($this->graphLocator->getProvidedServices());
foreach ($names as $name) {
$output->writeln($name);
}
......
......@@ -21,17 +21,18 @@
namespace Irstea\PlantUmlBundle\Command;
use Irstea\PlantUmlBundle\Model\Graph;
use Irstea\PlantUmlBundle\Renderer\RendererInterface;
use Irstea\PlantUmlBundle\Renderer\RendererRegistryInterface;
use Irstea\PlantUmlBundle\Writer\StreamWriter;
use PHPStan\Symfony\Service;
use Psr\Container\ContainerInterface;
use Renderer\RendererInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
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;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* Description of ImportAffiliationCommand.
......@@ -42,45 +43,35 @@ class RenderCommand extends Command
* @var string
*/
private const CMD_NAME = 'irstea:plantuml:render';
/**
* @var ContainerInterface
*/
private $container;
/**
* @var string[]
*/
private $graphNames;
/**
* @var string
*/
/** @var string */
private $defaultFormat;
/**
* @var string
*/
/** @var string */
private $defaultOutput;
/** @var RendererInterface */
private $renderer;
/** @var ServiceLocator */
private $graphLocator;
/** @var ServiceLocator */
private $rendererLocator;
/**
* RenderCommand constructor.
*
* @param ContainerInterface $container
* @param array $graphNames
* @param string $defaultFormat
* @param string $defaultOutput
*/
public function __construct(ContainerInterface $container, array $graphNames, string $defaultFormat, string $defaultOutput, RendererInterface $renderer)
public function __construct(ServiceLocator $graphLocator, ServiceLocator $rendererLocator, string $defaultFormat = "svg", string $defaultOutput = ".")
{
parent::__construct(self::CMD_NAME);
$this->container = $container;
$this->graphNames = $graphNames;
$this->defaultFormat = $defaultFormat;
$this->defaultOutput = $defaultOutput;
$this->renderer = $renderer;
$this->graphLocator = $graphLocator;
$this->rendererLocator = $rendererLocator;
}
protected function getDefaultRenderer(): string
{
$aliases = array_keys($this->rendererLocator->getProvidedServices());
return array_shift($aliases);
}
/**
......@@ -88,12 +79,14 @@ class RenderCommand extends Command
*/
protected function configure(): void
{
$this
->setName(self::CMD_NAME)
->setDescription("Créer la page d'un graphe ou plusieurs graphes.")
->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Répertoire de destination.')
->addOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format du fichier à générer')
->addArgument('graph', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Graphe à générer');
$defaultRenderer =
$this
->setName(self::CMD_NAME)
->setDescription("Créer la page d'un graphe ou plusieurs graphes.")
->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Répertoire de destination.')
->addOption('renderer', 'r', InputOption::VALUE_REQUIRED, 'Moteur de rendu')
->addOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format du fichier à générer')
->addArgument('graph', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Graphe à générer');
}
/**
......@@ -101,10 +94,10 @@ class RenderCommand extends Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$graphs = $input->getArgument('graph') ?: $this->graphNames;
$graphs = $input->getArgument('graph') ?: array_keys($this->graphLocator->getProvidedServices());
$format = $input->getOption('format') ?: $this->defaultFormat;
$outputDir = $input->getOption('output') ?: $this->defaultOutput;
$rendererAlias = $input->getOption('renderer') ?: $this->getDefaultRenderer();
$io = new SymfonyStyle($input, $output);
foreach ($graphs as $name) {
......@@ -112,19 +105,14 @@ class RenderCommand extends Command
$io->section("Graphe: $name");
$graph = $this->container->get("irstea_plant_uml.graph.$name");
$graph = $this->graphLocator->get($name);
$renderer = $this->rendererLocator->get($rendererAlias);
$this->renderGraph($graph, $target, $format, $io);
$this->renderGraph($graph, $target, $format, $io, $renderer);
}
}
/**
* @param Graph $graph
* @param $target
* @param $format
* @param SymfonyStyle $io
*/
private function renderGraph(Graph $graph, $target, $format, SymfonyStyle $io): void
private function renderGraph(Graph $graph, string $target, string $format, SymfonyStyle $io, RendererInterface $renderer): void
{
if (OutputInterface::VERBOSITY_VERY_VERBOSE <= $io->getVerbosity()) {
$desc = [];
......@@ -140,7 +128,7 @@ class RenderCommand extends Command
$output = StreamWriter::open($target, 'wt');
$io->write('Démarrage du renderer: ');
$pipe = $this->renderer->render($format, $output);
$pipe = $renderer->render($format, $output);
$io->writeln('<info>Ok</info>.');
$io->write('Génération du graphe: ');
......@@ -149,57 +137,4 @@ class RenderCommand extends Command
$pipe->close();
}
/**
* @param $target
* @param $format
*
* @return array
*/
private function startProcess($target, $format): array
{
$cmd = sprintf(
'%s -jar %s -graphvizdot %s -pipe -t%s',
$this->findExecutable($this->javaBinary),
$this->plantUmlJar,
$this->findExecutable($this->dotBinary),
$format
);
$fs = new Filesystem();
$fs->mkdir(dirname($target));
$desc = [
// stdin
['pipe', 'r'],
// stdout
['file', $target, 'wt'],
// stderr
STDERR,
];
$pipes = [];
$proc = proc_open($cmd, $desc, $pipes);
return [$proc, $pipes];
}
/**
* @param string $nameOrPath
*
* @return string
*/
private function findExecutable(string $nameOrPath): string
{
if (\file_exists($nameOrPath) && \is_executable($nameOrPath)) {
return $nameOrPath;
}
$exec = new ExecutableFinder();
$path = $exec->find($nameOrPath);
if ($path === null) {
throw new \RuntimeException("cannot find executable: $nameOrPath");
}
return $path;
}
}
......@@ -169,7 +169,7 @@ class GraphDefinitionBuilder
/**
* @return Reference
*/
protected function buildDecorator(): Reference
protected function buildDecorator(): ?Reference
{
$config = $this->config['decoration']['decorators'];
......
......@@ -34,8 +34,8 @@ class IrsteaPlantUmlExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
......
......@@ -50,4 +50,9 @@ class EntityFinder implements FinderInterface
{
$conf['type'] = 'entities';
}
public static function getAlias(): string
{
return 'entites';
}
}
......@@ -100,4 +100,11 @@ class ClassFinder implements FinderInterface
$conf['type'] = 'classes';
$conf['directories'] = $this->directories;
}
public static function getAlias(): string
{
return 'classes';
}
}
......@@ -54,4 +54,9 @@ class FilteringFinder implements FinderInterface
$this->inner->toConfig($conf);
$this->filter->toConfig($conf);
}
public static function getAlias(): string
{
return 'filter';
}
}
......@@ -26,4 +26,5 @@ use Traversable;
interface FinderInterface extends Traversable, IteratorAggregate, ToConfigInterface
{
public static function getAlias(): string;
}
......@@ -19,7 +19,7 @@
*/
namespace Irstea\PlantUmlBundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class IrsteaPlantUmlBundle extends Bundle
......
......@@ -22,10 +22,18 @@ namespace Irstea\PlantUmlBundle\Renderer;
use Irstea\PlantUmlBundle\Writer\ClosableWriterInterface;
final class NullRenderer implements RendererInterface
/**
* @final
*/
class NullRenderer implements RendererInterface
{
public function render(string $format, ClosableWriterInterface $output): ClosableWriterInterface
{
return $output;
}
public static function getAlias(): string
{
return 'null';
}
}
......@@ -25,7 +25,10 @@ use Irstea\PlantUmlBundle\Writer\WriterStreamWrapper;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
final class ProcessRenderer implements RendererInterface
/**
* @final
*/
class ProcessRenderer implements RendererInterface
{
/** @var string */
private $javaBinary;
......@@ -74,4 +77,9 @@ final class ProcessRenderer implements RendererInterface
return new ProcessInputWriter($process, $pipes[0], $this->logger);
}
public static function getAlias(): string
{
return 'local';
}
}
......@@ -25,4 +25,6 @@ use Irstea\PlantUmlBundle\Writer\ClosableWriterInterface;
interface RendererInterface
{
public function render(string $format, ClosableWriterInterface $output): ClosableWriterInterface;
public static function getAlias(): string;
}
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<defaults
public="false"
autoconfigure="true"
autowire="true"
/>
<instanceof id="Irstea\PlantUmlBundle\Finder\FinderInterface" >
<tag name="irstea_plantuml.finder"/>
</instanceof>
<instanceof id="Irstea\PlantUmlBundle\Model\DecoratorInterface" >
<tag name="irstea_plantuml.decorator"/>
</instanceof>
<instanceof id="Irstea\PlantUmlBundle\Model\NamespaceInterface" >
<tag name="irstea_plantuml.namespace"/>
</instanceof>
<instanceof id="Irstea\PlantUmlBundle\Renderer\RendererInterface" lazy="true">
<tag name="irstea_plantuml.renderer"/>
</instanceof>
<prototype
namespace="Irstea\PlantUmlBundle\"
resource="../../src/*"
exclude="../../src/Resources/*"
/>
<service class="Irstea\PlantUmlBundle\Command\ListCommand">
<argument type="tagged_locator" tag="irstea_plantuml.graph" default-index-method="getName"/>
</service>
<service class="Irstea\PlantUmlBundle\Command\RenderCommand">
<argument type="tagged_locator" tag="irstea_plantuml.graph" default-index-method="getName"/>
<argument type="tagged_locator" tag="irstea_plantuml.renderer" default-index-method="getAlias"/>
<argument type="string">%irstea_plantuml.output.format%</argument>
<argument type="string">%irstea_plantuml.output.directory%</argument>
</service>
</services>
</container>
parameters: ~
services:
irstea.plant_uml.finder.classes.template:
class: Irstea\PlantUmlBundle\Finder\ClassFinder
abstract: true
public: false
irstea.plant_uml.finder.entities.template:
class: Irstea\PlantUmlBundle\Doctrine\EntityFinder
abstract: true
public: false
irstea.plant_uml.decorator.inheritance:
class: Irstea\PlantUmlBundle\Model\Decorator\InheritanceDecorator
irstea.plant_uml.decorator.interfaces:
class: Irstea\PlantUmlBundle\Model\Decorator\InterfaceDecorator
irstea.plant_uml.decorator.traits:
class: Irstea\PlantUmlBundle\Model\Decorator\TraitDecorator
irstea.plant_uml.decorator.attributes:
class: Irstea\PlantUmlBundle\Model\Decorator\AttributeDecorator
irstea.plant_uml.decorator.methods:
class: Irstea\PlantUmlBundle\Model\Decorator\MethodDecorator
irstea.plant_uml.decorator.null:
class: Irstea\PlantUmlBundle\Model\Decorator\NullDecorator
irstea.plant_uml.decorator.entity.template:
class: Irstea\PlantUmlBundle\Doctrine\EntityDecorator
abstract: true
public: false
irstea.plant_uml.decorator.associations.template:
class: Irstea\PlantUmlBundle\Doctrine\AssociationDecorator
abstract: true
public: false
irstea.plant_uml.decorator.fields.template:
class: Irstea\PlantUmlBundle\Doctrine\FieldDecorator
abstract: true
public: false
irstea.plant_uml.decorator.filtered.template:
class: Irstea\PlantUmlBundle\Model\Decorator\FilteringDecorator
abstract: true
public: false
irstea.plant_uml.decorator.composite.template:
class: Irstea\PlantUmlBundle\Model\Decorator\CompositeDecorator
abstract: true
public: false
irstea.plant_uml.namespaces.php.template:
class: Irstea\PlantUmlBundle\Model\Namespace_\Php\RootNamespace
abstract: true
public: false
irstea.plant_uml.namespaces.flat.template:
class: Irstea\PlantUmlBundle\Model\Namespace_\FlatNamespace
abstract: true
public: false
irstea.plant_uml.namespaces.bundles.template:
class: Irstea\PlantUmlBundle\Model\Namespace_\BundleNamespace
arguments: ["%kernel.bundles%"]
abstract: true
public: false
irstea.plant_uml.namespaces.entities.template:
class: Irstea\PlantUmlBundle\Doctrine\DoctrineNamespace
abstract: true
public: false
irstea.plant_uml.command.generate:
class: Irstea\PlantUmlBundle\Command\GenerateCommand
lazy: true
public: false
calls:
- [ setContainer, ["@service_container"] ]
tags:
- { name: console.command }
irstea.plant_uml.command.render:
class: Irstea\PlantUmlBundle\Command\RenderCommand
lazy: true
public: false
arguments:
- '@service_container'
- '%irstea_plant_uml.graph_keys%'
- '%irstea_plant_uml.output.format%'
- '%irstea_plant_uml.output.directory%'
- '%irstea_plant_uml.binaries.java%'
- '%irstea_plant_uml.binaries.plamtuml_jar%'
- '%irstea_plant_uml.binaries.dot%'
tags:
- { name: console.command }
irstea.plant_uml.command.list:
class: Irstea\PlantUmlBundle\Command\ListCommand
lazy: true
public: false
tags:
- { name: console.command }
arguments:
- '%irstea_plant_uml.graph_keys%'
......@@ -21,7 +21,6 @@
namespace Irstea\PlantUmlBundle\Writer;
use Symfony\Component\Filesystem\Filesystem;
use Writer\ClosableWriterInterface;
/**
* Description of OutputWriter.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment