EntitySchemaCommand.php 2.11 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
<?php

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

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
11
12
13
14
15
use Irstea\PlantUmlBundle\Model\ClassVisitor;
use Irstea\PlantUmlBundle\Model\Decorator\CompositeDecorator;
use Irstea\PlantUmlBundle\Model\Decorator\FilteringDecorator;
use Irstea\PlantUmlBundle\Model\Decorator\InheritanceDecorator;
use Irstea\PlantUmlBundle\Model\Decorator\NullDecorator;
16
use Irstea\PlantUmlBundle\Model\Filter\Whitelist;
17
use Irstea\PlantUmlBundle\Writer\OutputWriter;
18
19
20
21
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

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
/**
 * 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();

52
53
54
55
56
        $classes = array_map(
            function(ClassMetadata $metadata) { return $metadata->getName(); },
            $allMetadata
        );

57
58
59
60
61
62
63
        $decorator = new FilteringDecorator(
            new InheritanceDecorator(),
            new Whitelist($classes)
        );

        $visitor = new ClassVisitor($decorator);

64
        array_walk($classes, [$visitor, 'visitClass']);
65

66
67
68
69
        $writer = new OutputWriter($output);
        $writer->write("@startuml\n");
        $visitor->outputTo($writer);
        $writer->write("@enduml\n");
70
71
    }
}