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

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

No related merge requests found
Showing with 111 additions and 39 deletions
+111 -39
...@@ -33,14 +33,22 @@ abstract class AbstractDoctrineDecorator implements DecoratorInterface ...@@ -33,14 +33,22 @@ abstract class AbstractDoctrineDecorator implements DecoratorInterface
$this->metadataFactory = $manager->getMetadataFactory(); $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(); $className = $class->getName();
if ($this->metadataFactory->hasMetadataFor($className)) { if (!$this->metadataFactory->hasMetadataFor($className)) {
$this->decorateEntity($this->metadataFactory->getMetadataFor($className), $node, $visitor); 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; ...@@ -22,19 +22,19 @@ use ReflectionClass;
*/ */
class AssociationDecorator extends AbstractDoctrineDecorator 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(); return $this->withMetadata(
if(empty($associations)) { function ($metadata) {
return; return $metadata->getAssociationMappings();
} },
$class
foreach($associations as $association) { );
$this->visitAssociation($node, $visitor, $association);
}
} }
protected function visitAssociation(NodeInterface $node, ClassVisitorInterface $visitor, array $association) protected function decorateItem(ReflectionClass $class, NodeInterface $node, ClassVisitorInterface $visitor, $association)
{ {
if (!$association['isOwningSide']) { if (!$association['isOwningSide']) {
return; return;
......
...@@ -19,14 +19,19 @@ use Irstea\PlantUmlBundle\Model\NodeInterface; ...@@ -19,14 +19,19 @@ use Irstea\PlantUmlBundle\Model\NodeInterface;
*/ */
class EntityDecorator extends AbstractDoctrineDecorator 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) { $this->withMetadata(
$node->addStereotype('mappedSuperClass'); function ($metadata) use($node) {
} elseif($metadata->isEmbeddedClass) { if ($metadata->isMappedSuperclass) {
$node->addStereotype('embedded'); $node->addStereotype('mappedSuperClass');
} else { } elseif($metadata->isEmbeddedClass) {
$node->addStereotype('entity'); $node->addStereotype('embedded');
} } else {
$node->addStereotype('entity');
}
},
$class
);
} }
} }
...@@ -11,6 +11,7 @@ namespace Irstea\PlantUmlBundle\Doctrine; ...@@ -11,6 +11,7 @@ namespace Irstea\PlantUmlBundle\Doctrine;
use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Mapping\ClassMetadata;
use Irstea\PlantUmlBundle\Model\ClassVisitorInterface; use Irstea\PlantUmlBundle\Model\ClassVisitorInterface;
use Irstea\PlantUmlBundle\Model\NodeInterface; use Irstea\PlantUmlBundle\Model\NodeInterface;
use ReflectionClass;
/** /**
* Description of RelationDecorator * Description of RelationDecorator
...@@ -19,28 +20,32 @@ use Irstea\PlantUmlBundle\Model\NodeInterface; ...@@ -19,28 +20,32 @@ use Irstea\PlantUmlBundle\Model\NodeInterface;
*/ */
class FieldDecorator extends AbstractDoctrineDecorator class FieldDecorator extends AbstractDoctrineDecorator
{ {
protected function decorateEntity(ClassMetadata $metadata, NodeInterface $node, ClassVisitorInterface $visitor) use \Irstea\PlantUmlBundle\Model\Decorator\InheritableItemDecoratorTrait;
{
if(empty($metadata->fieldMappings)) {
return;
}
foreach($metadata->fieldMappings as $field) { protected function extractItems(ReflectionClass $class)
$this->visitField($node, $visitor, $field, $metadata->isIdentifier($field['fieldName'])); {
} 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'])) { $isIdentifier = $this->withMetadata(
return; function ($metadata) use ($field) {
} return $metadata->isIdentifier($field['fieldName']);
},
$class
);
$node->addAttribute(new Field( $node->addAttribute(new Field(
$field['fieldName'], $field['fieldName'],
$field['type'], $field['type'],
$field['unique'], $field['unique'],
$field['nullable'], $field['nullable']
$isIdentifier
)); ));
} }
} }
<?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);
}
Supports Markdown
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