EntitySchemaCommand.php 1.79 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
52
53
54
<?php

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

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Irstea\PlantUmlBundle\Model\Orm\EntityVisitor;
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;

/**
 * Description of ImportAffiliationCommand
 *
 * @author Guillaume Perréal <guillaume.perreal@irstea.fr>
 */
class EntitySchemaCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this
            ->setName('irstea:plantuml:entities')
            ->setDescription("Génère un schéma PlantUML à partir des métadonnées de Doctrine.");
    }

    /**
     *
     * @param InputInterface $input
     * @param OutputInterface $output
     *
     * @SuppressWarnings(UnusedFormalParameter)
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        /* @var $manager EntityManagerInterface */
        $manager = $this->getContainer()->get('doctrine.orm.entity_manager');
        $factory = $manager->getMetadataFactory();

        $allMetadata = $factory->getAllMetadata();

        $visitor = new EntityVisitor($allMetadata);
        foreach($allMetadata as $metadata) {
            /* @var $metadata ClassMetadata */
            $visitor->visitClass($metadata->getName());
        }

        $stream = fopen("php://output", "wt");
        fputs($stream, "@startuml\n");
55
        fputs($stream, "set namespaceSeparator .\n");
56
57
58
59
60
        $visitor->outputTo($stream);
        fputs($stream, "@enduml\n");
        fclose($stream);
    }
}