ClassVisitor.php 3.08 KiB
<?php
/*
 * © 2016 IRSTEA
 * Guillaume Perréal <guillaume.perreal@irstea.fr>
 * Tous droits réservés.
 */
namespace Irstea\PlantUmlBundle\Model;
use Irstea\PlantUmlBundle\Model\Decorator\NullDecorator;
use Irstea\PlantUmlBundle\Model\Filter\AcceptAllFilter;
use Irstea\PlantUmlBundle\Model\Namespace_\Php\RootNamespace;
use Irstea\PlantUmlBundle\Model\Node\Class_;
use Irstea\PlantUmlBundle\Model\Node\Interface_;
use Irstea\PlantUmlBundle\Model\Node\Trait_;
use Irstea\PlantUmlBundle\Writer\WritableInterface;
use Irstea\PlantUmlBundle\Writer\WriterInterface;
use ReflectionClass;
/**
 * Description of Visitor
 * @author Guillaume Perréal <guillaume.perreal@irstea.fr>
class ClassVisitor implements ClassVisitorInterface, WritableInterface
    /**
     * @var NamespaceInterface
    protected $rootNamespace;
    /**
     * @var ClassFilterInterface
    protected $filter;
    /**
     * @var DecoratorInterface
    protected $decorator;
    public function __construct(
        DecoratorInterface $decorator = null,
        ClassFilterInterface $filter = null,
        NamespaceInterface $namespace = null
    ) {
        $this->filter = $filter ?: AcceptAllFilter::instance();
        $this->decorator = $decorator ?: NullDecorator::instance();
        $this->rootNamespace = $namespace ?: new RootNamespace();
    /**
     * @param ClassFilterInterface $filter
     * @return self
    public function setClassFilter(ClassFilterInterface $filter)
        $this->filter = $filter;
        return $this;
    /**
     * @param DecoratorInterface $decorator
     * @return self
    public function setDecorator(DecoratorInterface $decorator)
        $this->decorator = $decorator;
        return $this;
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
} public function visitClass($className) { assert('is_string($className)', $className); if (isset($this->nodes[$className])) { return $this->nodes[$className]; } $class = new ReflectionClass($className); if (!$this->filter->accept($class)) { return $this->nodes[$className] = false; } $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; } /** * * @param NamespaceInterface $namespace * @param ReflectionClass $class * @return NodeInterface */ protected function createNode(NamespaceInterface $namespace, ReflectionClass $class) { $className = $class->getName(); if ($class->isTrait()) { return new Trait_($namespace, $className); } if ($class->isInterface()) { return new Interface_($namespace, $className); } return new Class_($namespace, $className, $class->isAbstract(), $class->isFinal()); } public function outputTo(WriterInterface $writer) { return $this->rootNamespace->outputTo($writer); } }