ClassVisitor.php 4.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php

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

namespace Irstea\PlantUmlBundle\Model;

use Irstea\PlantUmlBundle\Model\Node\Class_;
use Irstea\PlantUmlBundle\Model\Node\Interface_;
use Irstea\PlantUmlBundle\Model\Node\Trait_;
14
15
use Irstea\PlantUmlBundle\Writer\WritableInterface;
use Irstea\PlantUmlBundle\Writer\WritableNodeInterface;
16
use Irstea\PlantUmlBundle\Writer\WriterInterface;
17
18
19
20
21
22
23
use ReflectionClass;

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

31
32
33
34
35
36
    /**
     * @var ClassFilterInterface
     */
    protected $filter;

    public function __construct(ClassFilterInterface $filter = null)
37
38
    {
        $this->rootNamespace = new RootNamespace();
39
40
41
42
43
44
45
46
47
48
49
        $this->filter = $filter ?: Filter\AcceptAllFilter::instance();
    }

    /**
     * @param \Irstea\PlantUmlBundle\Model\ClassFilterInterface $filter
     * @return self
     */
    public function setClassFilter(ClassFilterInterface $filter)
    {
        $this->filter = $filter;
        return $this;
50
    }
Guillaume Perréal's avatar
Guillaume Perréal committed
51

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

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

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

    /**
     * @param ReflectionClass $class
78
     * @return NodeInterface
79
80
81
82
83
84
85
86
87
88
     */
    protected function visitClassReflection(ReflectionClass $class)
    {
        if ($class->isTrait()) {
            $node  = new Trait_($class->getName());
        } elseif ($class->isInterface()) {
            $node  = new Interface_($class->getName());
        } else {
            $node = new Class_($class->getName(), $class->isAbstract(), $class->isFinal());
        }
89

90
        $this->nodes[$class->getName()] = $node;
91
92
93

        $namespace = $this->rootNamespace->getNamespace($class->getNamespaceName());
        $namespace->addNode($node);
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

        $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());
113
            $this->visitRelations($node, $namespace, [$parentClass->getName()], 'Irstea\PlantUmlBundle\Model\Arrow\ExtendsClass');
114
115
        }

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

        return $node;
    }

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

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