* Tous droits réservés. */ namespace Irstea\PlantUmlBundle\Model; use Irstea\PlantUmlBundle\Model\Filter\AcceptAllFilter; use Irstea\PlantUmlBundle\Model\Namespace_\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\WritableNodeInterface; use Irstea\PlantUmlBundle\Writer\WriterInterface; use ReflectionClass; /** * Description of Visitor * * @author Guillaume Perréal */ class ClassVisitor implements ClassVisitorInterface, WritableInterface { /** * @var RootNamespace */ protected $rootNamespace; /** * @var ClassFilterInterface */ protected $filter; /** * @var DecoratorInterface */ protected $decorator; public function __construct(DecoratorInterface $decorator = null, ClassFilterInterface $filter = null) { $this->rootNamespace = new RootNamespace(); $this->filter = $filter ?: AcceptAllFilter::instance(); $this->decorator = $decorator ?: NullDecorator::instance(); } /** * @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; } public function visitClass($className) { assert('is_string($className)', $className); if (isset($this->nodes[$className])) { return $this->nodes[$className]; } if (!$this->filter->accept($className)) { return $this->nodes[$className] = false; } return $this->createNode($className); } /** * * @param string $className * @return NodeInterface */ protected function createNode($className) { $class = new ReflectionClass($className); $namespace = $this->rootNamespace->getNamespace($class->getNamespaceName()); if ($class->isTrait()) { $node = new Trait_($namespace, $className); } elseif ($class->isInterface()) { $node = new Interface_($namespace, $className); } else { $node = new Class_($namespace, $className, $class->isAbstract(), $class->isFinal()); } $this->nodes[$class->getName()] = $node; $namespace->addNode($node); $this->decorator->decorate($className, $node, $this); return $node; } public function outputTo(WriterInterface $writer) { return $this->rootNamespace->outputTo($writer); } }