ClassVisitor.php 3.08 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
use Irstea\PlantUmlBundle\Model\Filter\AcceptAllFilter;
13
use Irstea\PlantUmlBundle\Model\Namespace_\Php\RootNamespace;
14
15
16
use Irstea\PlantUmlBundle\Model\Node\Class_;
use Irstea\PlantUmlBundle\Model\Node\Interface_;
use Irstea\PlantUmlBundle\Model\Node\Trait_;
17
use Irstea\PlantUmlBundle\Writer\WritableInterface;
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 NamespaceInterface
30
     */
31
    protected $rootNamespace;
32

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

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

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

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

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

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

        $class = new ReflectionClass($className);

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

86
87
88
89
90
91
92
        $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;
93
94
95
96
    }

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

105
        if ($class->isTrait()) {
106
            return new Trait_($namespace, $className);
107
        }
108

109
        if ($class->isInterface()) {
110
            return new Interface_($namespace, $className);
111
        }
112

113
        return new Class_($namespace, $className, $class->isAbstract(), $class->isFinal());
114
115
    }

116
    public function outputTo(WriterInterface $writer)
Guillaume Perréal's avatar
Guillaume Perréal committed
117
    {
118
        return $this->rootNamespace->outputTo($writer);
Guillaume Perréal's avatar
Guillaume Perréal committed
119
    }
120
}