Commit 23467d72 authored by Guillaume Perréal's avatar Guillaume Perréal

N'affiche plus les asoociations et champs hérités de Doctrine.

parent 136fe0ff
......@@ -33,14 +33,22 @@ abstract class AbstractDoctrineDecorator implements DecoratorInterface
$this->metadataFactory = $manager->getMetadataFactory();
}
public function decorate(ReflectionClass $class, NodeInterface $node, ClassVisitorInterface $visitor)
/**
* @param callable $callback
* @param ReflectionClass $class
* @return mixed
*/
protected function withMetadata($callback, ReflectionClass $class = null)
{
if (!$class) {
return null;
}
$className = $class->getName();
if ($this->metadataFactory->hasMetadataFor($className)) {
$this->decorateEntity($this->metadataFactory->getMetadataFor($className), $node, $visitor);
if (!$this->metadataFactory->hasMetadataFor($className)) {
return null;
}
return $this;
return $callback($this->metadataFactory->getMetadataFor($className));
}
abstract protected function decorateEntity(ClassMetadata $metadata, NodeInterface $node, ClassVisitorInterface $visitor);
}
\ No newline at end of file
}
......@@ -22,19 +22,19 @@ use ReflectionClass;
*/
class AssociationDecorator extends AbstractDoctrineDecorator
{
protected function decorateEntity(ClassMetadata $metadata, NodeInterface $node, ClassVisitorInterface $visitor)
use \Irstea\PlantUmlBundle\Model\Decorator\InheritableItemDecoratorTrait;
protected function extractItems(ReflectionClass $class)
{
$associations = $metadata->getAssociationMappings();
if(empty($associations)) {
return;
}
foreach($associations as $association) {
$this->visitAssociation($node, $visitor, $association);
}
return $this->withMetadata(
function ($metadata) {
return $metadata->getAssociationMappings();
},
$class
);
}
protected function visitAssociation(NodeInterface $node, ClassVisitorInterface $visitor, array $association)
protected function decorateItem(ReflectionClass $class, NodeInterface $node, ClassVisitorInterface $visitor, $association)
{
if (!$association['isOwningSide']) {
return;
......
......@@ -19,14 +19,19 @@ use Irstea\PlantUmlBundle\Model\NodeInterface;
*/
class EntityDecorator extends AbstractDoctrineDecorator
{
protected function decorateEntity(ClassMetadata $metadata, NodeInterface $node, ClassVisitorInterface $visitor)
public function decorate(\ReflectionClass $class, NodeInterface $node, ClassVisitorInterface $visitor)
{
if ($metadata->isMappedSuperclass) {
$node->addStereotype('mappedSuperClass');
} elseif($metadata->isEmbeddedClass) {
$node->addStereotype('embedded');
} else {
$node->addStereotype('entity');
}
$this->withMetadata(
function ($metadata) use($node) {
if ($metadata->isMappedSuperclass) {
$node->addStereotype('mappedSuperClass');
} elseif($metadata->isEmbeddedClass) {
$node->addStereotype('embedded');
} else {
$node->addStereotype('entity');
}
},
$class
);
}
}
......@@ -11,6 +11,7 @@ namespace Irstea\PlantUmlBundle\Doctrine;
use Doctrine\ORM\Mapping\ClassMetadata;
use Irstea\PlantUmlBundle\Model\ClassVisitorInterface;
use Irstea\PlantUmlBundle\Model\NodeInterface;
use ReflectionClass;
/**
* Description of RelationDecorator
......@@ -19,28 +20,32 @@ use Irstea\PlantUmlBundle\Model\NodeInterface;
*/
class FieldDecorator extends AbstractDoctrineDecorator
{
protected function decorateEntity(ClassMetadata $metadata, NodeInterface $node, ClassVisitorInterface $visitor)
{
if(empty($metadata->fieldMappings)) {
return;
}
use \Irstea\PlantUmlBundle\Model\Decorator\InheritableItemDecoratorTrait;
foreach($metadata->fieldMappings as $field) {
$this->visitField($node, $visitor, $field, $metadata->isIdentifier($field['fieldName']));
}
protected function extractItems(ReflectionClass $class)
{
return $this->withMetadata(
function ($metadata) {
return $metadata->fieldMappings;
},
$class
);
}
protected function visitField(NodeInterface $node, ClassVisitorInterface $visitor, array $field, $isIdentifier)
protected function decorateItem(ReflectionClass $class, NodeInterface $node, ClassVisitorInterface $visitor, $field)
{
if (isset($field['inherited']) || isset($field['declared'])) {
return;
}
$isIdentifier = $this->withMetadata(
function ($metadata) use ($field) {
return $metadata->isIdentifier($field['fieldName']);
},
$class
);
$node->addAttribute(new Field(
$field['fieldName'],
$field['type'],
$field['unique'],
$field['nullable'],
$isIdentifier
$field['nullable']
));
}
}
<?php
/*
* © 2016 IRSTEA
* Guillaume Perréal <guillaume.perreal@irstea.fr>
* Tous droits réservés.
*/
namespace Irstea\PlantUmlBundle\Model\Decorator;
use Irstea\PlantUmlBundle\Model\ClassVisitorInterface;
use Irstea\PlantUmlBundle\Model\NodeInterface;
use ReflectionClass;
/**
* Décorateur abstrait pour les éléments d'une classe qui peuvent être hérités d'une classe base.
*
* Ce décorateur s'assure de n'afficher que les éléments propres à la classe.
*
* @author Guillaume Perréal <guillaume.perreal@irstea.fr>
*/
trait InheritableItemDecoratorTrait
{
/**
*
* @param \ReflectionClass $class
* @param NodeInterface $node
* @param ClassVisitorInterface $visitor
* @return self
*/
public function decorate(ReflectionClass $class, NodeInterface $node, ClassVisitorInterface $visitor)
{
$items = $this->extractItems($class);
if (empty($items)) {
return;
}
$parent = $class->getParentClass();
if ($parent) {
$parentItems = $this->extractItems($parent);
if (!empty($parentItems)) {
$items = array_diff_key($items, $parentItems);
}
}
foreach($items as $item) {
$this->decorateItem($class, $node, $visitor, $item);
}
}
abstract protected function extractItems(ReflectionClass $class);
abstract protected function decorateItem(ReflectionClass $class, NodeInterface $node, ClassVisitorInterface $visitor, $item);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment