EntitySchemaCommand.php 2.4 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
use Irstea\PlantUmlBundle\Doctrine\AssociationDecorator;
use Irstea\PlantUmlBundle\Doctrine\EntityDecorator;
13
14
15
16
use Irstea\PlantUmlBundle\Model\ClassVisitor;
use Irstea\PlantUmlBundle\Model\Decorator\CompositeDecorator;
use Irstea\PlantUmlBundle\Model\Decorator\FilteringDecorator;
use Irstea\PlantUmlBundle\Model\Decorator\InheritanceDecorator;
17
use Irstea\PlantUmlBundle\Model\Filter\Whitelist;
18
use Irstea\PlantUmlBundle\Model\Namespace_\FlatNamespace;
19
use Irstea\PlantUmlBundle\Writer\OutputWriter;
20
21
22
23
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

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

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

59
        $decorator = new FilteringDecorator(
60
61
62
63
64
            new CompositeDecorator([
                new InheritanceDecorator(),
                new EntityDecorator($factory),
                new AssociationDecorator($factory),
            ]),
65
66
67
            new Whitelist($classes)
        );

68
        $visitor = new ClassVisitor($decorator, null, new FlatNamespace());
69

70
        array_walk($classes, [$visitor, 'visitClass']);
71

72
73
74
75
        $writer = new OutputWriter($output);
        $writer->write("@startuml\n");
        $visitor->outputTo($writer);
        $writer->write("@enduml\n");
76
77
    }
}