RenderCommand.php 4.04 KB
Newer Older
1
<?php declare(strict_types=1);
2
/*
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
3
 * Copyright (C) 2016-2017 IRSTEA
4
5
 * All rights reserved.
 */
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
6

7
8
9
10
11
12
13
14
15
namespace Irstea\PlantUmlBundle\Command;

use Irstea\PlantUmlBundle\Model\Graph;
use Irstea\PlantUmlBundle\Writer\StreamWriter;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
16
use Symfony\Component\Console\Style\SymfonyStyle;
17
18
19
use Symfony\Component\Filesystem\Filesystem;

/**
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
20
 * Description of ImportAffiliationCommand.
21
22
23
24
25
26
27
28
 */
class RenderCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this
            ->setName('irstea:plantuml:render')
            ->setDescription("Créer la page d'un graphe ou plusieurs graphes.")
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
29
30
            ->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Répertoire de destination.')
            ->addOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format du fichier à générer')
31
32
33
34
            ->addArgument('graph', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Graphe à générer');
    }

    /**
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
35
     * @param InputInterface  $input
36
37
38
39
40
41
42
43
44
     * @param OutputInterface $output
     * @SuppressWarnings(UnusedFormalParameter)
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $graphs = $input->getArgument('graph') ?: $this->getContainer()->getParameter('irstea_plant_uml.graph_keys');
        $format = $input->getOption('format') ?: $this->getContainer()->getParameter('irstea_plant_uml.output.format');
        $outputDir = $input->getOption('output') ?: $this->getContainer()->getParameter('irstea_plant_uml.output.directory');

45
46
        $io = new SymfonyStyle($input, $output);

Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
47
48
        foreach ($graphs as $name) {
            $target = $outputDir . DIRECTORY_SEPARATOR . $name . '.' . $format;
49
50
51

            $io->section("Graphe: $name");

52
            $graph = $this->getContainer()->get("irstea_plant_uml.graph.$name");
53
54

            $this->renderGraph($graph, $target, $format, $io);
55
56
57
        }
    }

58
    protected function renderGraph(Graph $graph, $target, $format, SymfonyStyle $io)
59
    {
60
61
        $io->writeln("Fichier de sortie: <comment>$target</comment>");

62
63
64
65
66
67
        if (OutputInterface::VERBOSITY_VERY_VERBOSE <= $io->getVerbosity()) {
            $desc = [];
            $graph->toConfig($desc);
            $io->writeln(json_encode($desc, JSON_PRETTY_PRINT));
        }

Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
68
        $io->write('Exploration des classes: ');
69
        $graph->visitAll();
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
70
        $io->writeln('<info>Ok</info>.');
71

Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
72
        $io->write('Démarrage de PlantUML: ');
73
        list($proc, $pipes) = $this->startProcess($target, $format);
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
74
        $io->writeln('<info>Ok</info>.');
75

Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
76
        $io->write('Génération du graphe: ');
77
78
79
        $writer = new StreamWriter($pipes[0]);
        $graph->writeTo($writer);
        fclose($pipes[0]);
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
80
        $io->writeln('<info>Ok</info>.');
81

Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
82
        $io->write('Rendu graphique par PlantUML: ');
83
84
        $res = proc_close($proc);

85
        if ($res === 0) {
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
86
            $io->writeln('<info>Ok</info>.');
87
        } else {
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
88
            $io->writeln('<error>Nok</error>.');
89
        }
90
91
92
93
94
95
96
97
    }

    protected function startProcess($target, $format)
    {
        $fs = new Filesystem();
        $fs->mkdir(dirname($target));

        $ctn = $this->getContainer();
Raidelet Nicolas's avatar
Raidelet Nicolas committed
98
99
100
101
102
103
104
105
106
107
108
109
        $cmd = implode(
            ' ',
            [
                $ctn->getParameter('irstea_plant_uml.binaries.java'),
                '-jar',
                $ctn->getParameter('irstea_plant_uml.binaries.plamtuml_jar'),
                '-graphvizdot',
                $ctn->getParameter('irstea_plant_uml.binaries.dot'),
                '-pipe',
                '-t' . $format,
            ]
        );
110
111
112
113
114
115
116

        $desc = [
            // stdin
            ['pipe', 'r'],
            // stdout
            ['file', $target, 'wt'],
            // stderr
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
117
            STDERR,
118
119
120
121
122
123
124
125
        ];
        $pipes = [];

        $proc = proc_open($cmd, $desc, $pipes);

        return [$proc, $pipes];
    }
}