ClassVisitor.php 2.9 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
    /**
     * @var ClassFilterInterface
     */
    protected $filter;

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

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

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

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

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

80
        return $this->createNode($className);
81
82
83
84
85
    }

    /**
     *
     * @param string $className
86
     * @return NodeInterface
87
     */
88
    protected function createNode($className)
89
    {
90
        $class = new ReflectionClass($className);
91
92
        $namespace = $this->rootNamespace->getNamespace($class->getNamespaceName());

93
        if ($class->isTrait()) {
94
            $node  = new Trait_($namespace, $className);
95
        } elseif ($class->isInterface()) {
96
            $node  = new Interface_($namespace, $className);
97
        } else {
98
            $node = new Class_($namespace, $className, $class->isAbstract(), $class->isFinal());
99
        }
100

101
        $this->nodes[$class->getName()] = $node;
102
        $namespace->addNode($node);
103

104
        $this->decorator->decorate($className, $node, $this);
105
106
107
108

        return $node;
    }

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