ClassVisitor.php 4.13 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
12
use Irstea\PlantUmlBundle\Model\Filter\AcceptAllFilter;
use Irstea\PlantUmlBundle\Model\Namespace_\RootNamespace;
13
14
15
use Irstea\PlantUmlBundle\Model\Node\Class_;
use Irstea\PlantUmlBundle\Model\Node\Interface_;
use Irstea\PlantUmlBundle\Model\Node\Trait_;
16
17
use Irstea\PlantUmlBundle\Writer\WritableInterface;
use Irstea\PlantUmlBundle\Writer\WritableNodeInterface;
18
use Irstea\PlantUmlBundle\Writer\WriterInterface;
19
20
21
22
23
24
25
use ReflectionClass;

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

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

    public function __construct(ClassFilterInterface $filter = null)
39
40
    {
        $this->rootNamespace = new RootNamespace();
41
        $this->filter = $filter ?: AcceptAllFilter::instance();
42
43
44
    }

    /**
45
     * @param ClassFilterInterface $filter
46
47
48
49
50
51
     * @return self
     */
    public function setClassFilter(ClassFilterInterface $filter)
    {
        $this->filter = $filter;
        return $this;
52
    }
Guillaume Perréal's avatar
Guillaume Perréal committed
53

54
55
56
57
58
59
    public function visitClass($className)
    {
        assert('is_string($className)', $className);
        if (isset($this->nodes[$className])) {
            return $this->nodes[$className];
        }
60
61
62
        if (!$this->filter->accept($className)) {
            return $this->nodes[$className] = false;
        }
63
64
65
66
67
68
69

        return $this->doVisitClass($className);
    }

    /**
     *
     * @param string $className
70
     * @return NodeInterface
71
72
73
74
75
76
77
78
79
     */
    protected function doVisitClass($className)
    {
        $reflection = new ReflectionClass($className);
        return $this->visitClassReflection($reflection);
    }

    /**
     * @param ReflectionClass $class
80
     * @return NodeInterface
81
82
83
     */
    protected function visitClassReflection(ReflectionClass $class)
    {
84
85
        $namespace = $this->rootNamespace->getNamespace($class->getNamespaceName());

86
        if ($class->isTrait()) {
87
            $node  = new Trait_($namespace, $class->getName());
88
        } elseif ($class->isInterface()) {
89
            $node  = new Interface_($namespace, $class->getName());
90
        } else {
91
            $node = new Class_($namespace, $class->getName(), $class->isAbstract(), $class->isFinal());
92
        }
93

94
        $this->nodes[$class->getName()] = $node;
95
        $namespace->addNode($node);
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

        $parentClass = $class->getParentClass();
        $traitNames = $class->getTraitNames();

        $indirectInterfaces = array_filter(
                array_map(
                    function ($i) { return $i->getInterfaceNames(); },
                    $class->getInterfaces()
                )
        );
        $interfaceNames = $class->getInterfaceNames();
        if (!empty($indirectInterfaces)) {
            $indirectInterfaces = call_user_func_array('array_merge', $indirectInterfaces);
            $interfaceNames = array_diff($interfaceNames, $indirectInterfaces);
        }

        if ($parentClass) {
            $traitNames = array_diff($traitNames, $parentClass->getTraitNames());
            $interfaceNames = array_diff($interfaceNames, $parentClass->getInterfaceNames());
115
            $this->visitRelations($node, [$parentClass->getName()], 'Irstea\PlantUmlBundle\Model\Arrow\ExtendsClass');
116
117
        }

118
119
        $this->visitRelations($node, $interfaceNames, 'Irstea\PlantUmlBundle\Model\Arrow\ImplementsInterface');
        $this->visitRelations($node, $traitNames, 'Irstea\PlantUmlBundle\Model\Arrow\UsesTrait');
120
121
122
123

        return $node;
    }

124
    protected function visitRelations(NodeInterface $source, array $classNames, $relationClass)
125
126
127
    {
        foreach ($classNames as $className) {
            $target = $this->visitClass($className);
128
            if ($target) {
129
                $source->addArrow(new $relationClass($source, $target));
130
            }
131
132
133
        }
    }

134
    public function outputTo(WriterInterface $writer)
Guillaume Perréal's avatar
Guillaume Perréal committed
135
    {
136
        return $this->rootNamespace->outputTo($writer);
Guillaume Perréal's avatar
Guillaume Perréal committed
137
    }
138
}