ClassVisitor.php 3.53 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
    public function __construct()
    {
        $this->rootNamespace = new RootNamespace();
    }
Guillaume Perréal's avatar
Guillaume Perréal committed
35

36
37
38
39
40
41
42
43
44
45
46
47
48
    public function visitClass($className)
    {
        assert('is_string($className)', $className);
        if (isset($this->nodes[$className])) {
            return $this->nodes[$className];
        }

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

    /**
     *
     * @param string $className
49
     * @return NodeInterface
50
51
52
53
54
55
56
57
58
     */
    protected function doVisitClass($className)
    {
        $reflection = new ReflectionClass($className);
        return $this->visitClassReflection($reflection);
    }

    /**
     * @param ReflectionClass $class
59
     * @return NodeInterface
60
61
62
63
64
65
66
67
68
69
     */
    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());
        }
70

71
        $this->nodes[$class->getName()] = $node;
72
73
74

        $namespace = $this->rootNamespace->getNamespace($class->getNamespaceName());
        $namespace->addNode($node);
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

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

97
98
        $this->visitRelations($node, $namespace, $interfaceNames, 'Irstea\PlantUmlBundle\Model\Arrow\ImplementsInterface');
        $this->visitRelations($node, $namespace, $traitNames, 'Irstea\PlantUmlBundle\Model\Arrow\UsesTrait');
99
100
101
102

        return $node;
    }

103
    protected function visitRelations(WritableNodeInterface $source, NamespaceInterface $namespace, array $classNames, $relationClass)
104
105
106
    {
        foreach ($classNames as $className) {
            $target = $this->visitClass($className);
107
            $namespace->addArrow(new $relationClass($source, $target));
108
109
110
        }
    }

111
    public function outputTo(WriterInterface $writer)
Guillaume Perréal's avatar
Guillaume Perréal committed
112
    {
113
        return $this->rootNamespace->outputTo($writer);
Guillaume Perréal's avatar
Guillaume Perréal committed
114
    }
115
}