RenderCommand.php 3.94 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php

/*
 * Copyright (C) 2015 IRSTEA
 * All rights reserved.
 */
namespace Irstea\PlantUmlBundle\Command;

use Irstea\PlantUmlBundle\Model\Graph;
use Irstea\PlantUmlBundle\Writer\OutputWriter;
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;
17
use Symfony\Component\Console\Style\SymfonyStyle;
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\ProcessBuilder;
use Symfony\Component\Security\Core\Exception\InvalidArgumentException;

/**
 * Description of ImportAffiliationCommand
 *
 * @author Guillaume Perréal <guillaume.perreal@irstea.fr>
 */
class RenderCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this
            ->setName('irstea:plantuml:render')
            ->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');
    }

    /**
     *
     * @param InputInterface $input
     * @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');

52
53
        $io = new SymfonyStyle($input, $output);

54
55
        foreach($graphs as $name) {
            $target = $outputDir . DIRECTORY_SEPARATOR . $name . "." . $format;
56
57
58

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

59
            $graph = $this->getContainer()->get("irstea_plant_uml.graph.$name");
60
61

            $this->renderGraph($graph, $target, $format, $io);
62
63
64
        }
    }

65
    protected function renderGraph(Graph $graph, $target, $format, SymfonyStyle $io)
66
    {
67
68
69
        $io->writeln("Fichier de sortie: <comment>$target</comment>");

        $io->write("Exploration des classes: ");
70
        $graph->visitAll();
71
        $io->writeln("<info>Ok</info>.");
72

73
        $io->write("Démarrage de PlantUML: ");
74
        list($proc, $pipes) = $this->startProcess($target, $format);
75
        $io->writeln("<info>Ok</info>.");
76

77
        $io->write("Génération du graphe: ");
78
79
80
        $writer = new StreamWriter($pipes[0]);
        $graph->writeTo($writer);
        fclose($pipes[0]);
81
        $io->writeln("<info>Ok</info>.");
82

83
        $io->write("Rendu graphique par PlantUML: ");
84
85
        $res = proc_close($proc);

86
87
88
89
90
        if ($res === 0) {
            $io->writeln("<info>Ok</info>.");
        } else {
            $io->writeln("<error>Nok</error>.");
        }
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
    }

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

        $ctn = $this->getContainer();
        $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
        ]);

        $desc = [
            // stdin
            ['pipe', 'r'],
            // stdout
            ['file', $target, 'wt'],
            // stderr
            STDERR
        ];
        $pipes = [];

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

        return [$proc, $pipes];
    }

}