-
Guillaume Perréal authoredd56eec09
<?php declare(strict_types=1);
/*
* Copyright (C) 2016-2018 IRSTEA
* All rights reserved.
*/
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\WriterInterface;
use ReflectionClass;
/**
* Description of Visitor.
*/
class ClassVisitor implements ClassVisitorInterface
{
/**
* @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;
}
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
public function visitClass($classOrName)
{
if ($classOrName instanceof \ReflectionClass) {
$class = $classOrName;
} elseif (is_string($classOrName)) {
$class = new \ReflectionClass($classOrName);
} else {
throw new \InvalidArgumentException('Invalid argument, expected ReflectionClass or string');
}
$className = $class->getName();
if (isset($this->nodes[$className])) {
return $this->nodes[$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 writeTo(WriterInterface $writer)
{
return $this->rootNamespace->writeTo($writer);
}
/**
* @param array $conf
*/
public function toConfig(array &$conf)
{
$conf['layout'] = [];
$conf['decoration'] = [];
$this->filter->toConfig($conf['layout']);
$this->rootNamespace->toConfig($conf['layout']);
$this->decorator->toConfig($conf['decoration']);
}
}