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

/*
 * © 2016 IRSTEA
 * Guillaume Perréal <guillaume.perreal@irstea.fr>
 * Tous droits réservés.
 */

namespace Irstea\PlantUmlBundle\Model;

11
use Irstea\PlantUmlBundle\Model\Decorator\NullDecorator;
12
13
use Irstea\PlantUmlBundle\Model\Filter\AcceptAllFilter;
use Irstea\PlantUmlBundle\Model\Namespace_\RootNamespace;
14
15
16
use Irstea\PlantUmlBundle\Model\Node\Class_;
use Irstea\PlantUmlBundle\Model\Node\Interface_;
use Irstea\PlantUmlBundle\Model\Node\Trait_;
17
18
use Irstea\PlantUmlBundle\Writer\WritableInterface;
use Irstea\PlantUmlBundle\Writer\WritableNodeInterface;
19
use Irstea\PlantUmlBundle\Writer\WriterInterface;
20
21
22
23
24
25
26
use ReflectionClass;

/**
 * Description of Visitor
 *
 * @author Guillaume Perréal <guillaume.perreal@irstea.fr>
 */
27
class ClassVisitor implements ClassVisitorInterface, WritableInterface
28
29
{
    /**
30
     * @var NamespaceInterface
31
     */
32
    protected $rootNamespace;
33

34
35
36
37
38
    /**
     * @var ClassFilterInterface
     */
    protected $filter;

39
40
41
42
43
    /**
     * @var DecoratorInterface
     */
    protected $decorator;

44
45
46
47
48
    public function __construct(
        DecoratorInterface $decorator = null,
        ClassFilterInterface $filter = null,
        NamespaceInterface $namespace = null
    ) {
49
        $this->filter = $filter ?: AcceptAllFilter::instance();
50
        $this->decorator = $decorator ?: NullDecorator::instance();
51
        $this->rootNamespace = $namespace ?: new RootNamespace();
52
53
54
    }

    /**
55
     * @param ClassFilterInterface $filter
56
57
58
59
60
61
     * @return self
     */
    public function setClassFilter(ClassFilterInterface $filter)
    {
        $this->filter = $filter;
        return $this;
62
    }
Guillaume Perréal's avatar
Guillaume Perréal committed
63

64
65
66
67
68
69
70
71
72
73
    /**
     * @param DecoratorInterface $decorator
     * @return self
     */
    public function setDecorator(DecoratorInterface $decorator)
    {
        $this->decorator = $decorator;
        return $this;
    }

74
75
76
77
78
79
    public function visitClass($className)
    {
        assert('is_string($className)', $className);
        if (isset($this->nodes[$className])) {
            return $this->nodes[$className];
        }
80
81
82
83

        $class = new ReflectionClass($className);

        if (!$this->filter->accept($class)) {
84
85
            return $this->nodes[$className] = false;
        }
86

87
88
89
90
91
92
93
        $namespace = $this->rootNamespace->getNamespace($class->getNamespaceName());
        $node = $this->nodes[$className] = $this->createNode($namespace, $class);
        $namespace->addNode($node);

        $this->decorator->decorate($class, $node, $this);

        return $node;
94
95
96
97
    }

    /**
     *
98
     * @param NamespaceInterface $namespace
99
     * @param ReflectionClass $class
100
     * @return NodeInterface
101
     */
102
    protected function createNode(NamespaceInterface $namespace, ReflectionClass $class)
103
    {
104
105
106
107
108
        $className = $class->getName();

        $id = str_replace('\\', '.', $className).'_node';
        $label = $namespace->getShortName($className);

109
        if ($class->isTrait()) {
110
            return new Trait_($namespace, $id, $label);
111
        }
112

113
        if ($class->isInterface()) {
114
            return new Interface_($namespace, $id, $label, $class->getName());
115
        }
116

117
        return new Class_($namespace, $id, $label, $class->isAbstract(), $class->isFinal());
118
119
    }

120
    public function outputTo(WriterInterface $writer)
Guillaume Perréal's avatar
Guillaume Perréal committed
121
    {
122
        return $this->rootNamespace->outputTo($writer);
Guillaume Perréal's avatar
Guillaume Perréal committed
123
    }
124
}