ClassVisitor.php 3.03 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 RootNamespace
31
     */
32
    protected $rootNamespace;
33

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

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

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

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

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

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

        $class = new ReflectionClass($className);

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

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

    /**
     *
95
     * @param NamespaceInterface $namespace
96
     * @param ReflectionClass $class
97
     * @return NodeInterface
98
     */
99
    protected function createNode(NamespaceInterface $namespace, ReflectionClass $class)
100
101
    {
        if ($class->isTrait()) {
102
            return new Trait_($namespace, $class->getName());
103
        }
104

105
106
107
        if ($class->isInterface()) {
            return new Interface_($namespace, $class->getName());
        }
108

109
        return new Class_($namespace, $class->getName(), $class->isAbstract(), $class->isFinal());
110
111
    }

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