RenderCommand.php 3.93 KiB
<?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;
use Symfony\Component\Console\Style\SymfonyStyle;
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');
        $io = new SymfonyStyle($input, $output);
        foreach($graphs as $name) {
            $target = $outputDir . DIRECTORY_SEPARATOR . $name . "." . $format;
            $io->section("Graphe: $name");
            $graph = $this->getContainer()->get("irstea_plant_uml.graph.$name");
            $this->renderGraph($graph, $target, $format, $io);
    protected function renderGraph(Graph $graph, $target, $format, SymfonyStyle $io)
        $io->writeln("Fichier de sortie: <comment>$target</comment>");
        $io->write("Exploration des classes: ");
        $graph->visitAll();
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
$io->writeln("<info>Ok</info>."); $io->write("Démarrage de PlantUML: "); list($proc, $pipes) = $this->startProcess($target, $format); $io->writeln("<info>Ok</info>."); $io->write("Génération du graphe: "); $writer = new StreamWriter($pipes[0]); $graph->writeTo($writer); fclose($pipes[0]); $io->writeln("<info>Ok</info>."); $io->write("Attente de PlantUML: "); $res = proc_close($proc); if ($res === 0) { $io->writeln("<info>Ok</info>."); } else { $io->writeln("<error>Nok</error>."); } } 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]; } }