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

Ajout d'un décorateur pour les champs Doctrine.

No related merge requests found
Showing with 336 additions and 4 deletions
+336 -4
......@@ -11,6 +11,7 @@ use Irstea\PlantUmlBundle\Doctrine\AssociationDecorator;
use Irstea\PlantUmlBundle\Doctrine\DoctrineNamespace;
use Irstea\PlantUmlBundle\Doctrine\EntityDecorator;
use Irstea\PlantUmlBundle\Doctrine\EntityFinder;
use Irstea\PlantUmlBundle\Doctrine\FieldDecorator;
use Irstea\PlantUmlBundle\Finder\ClassFinder;
use Irstea\PlantUmlBundle\Finder\FilteringFinder;
use Irstea\PlantUmlBundle\Finder\FinderInterface;
......@@ -208,6 +209,8 @@ class GenerateCommand extends ContainerAwareCommand
return new EntityDecorator($this->entityManager->getMetadataFactory());
case 'associations':
return new AssociationDecorator($this->entityManager->getMetadataFactory());
case 'fields':
return new FieldDecorator($this->entityManager->getMetadataFactory());
}
}
......
......@@ -85,9 +85,9 @@ class Configuration implements ConfigurationInterface
->addDefaultsIfNotSet()
->children()
->arrayNode('decorators')
->defaultValue(['inheritance', 'traits', 'interfaces', 'entity', 'associations', 'properties', 'methods'])
->defaultValue(['inheritance', 'traits', 'interfaces', 'entity', 'associations', 'properties', 'methods', 'fields'])
->prototype('enum')
->values(['inheritance', 'traits', 'interfaces', 'entity', 'associations', 'properties', 'methods'])
->values(['inheritance', 'traits', 'interfaces', 'entity', 'associations', 'properties', 'methods', 'fields'])
->end()
->end()
->append($this->buildFilterNode('include'))
......
......@@ -11,6 +11,7 @@ namespace Irstea\PlantUmlBundle\Doctrine;
use Doctrine\ORM\Mapping\ClassMetadata;
use Irstea\PlantUmlBundle\Model\Arrow\BaseArrow;
use Irstea\PlantUmlBundle\Model\ClassVisitorInterface;
use Irstea\PlantUmlBundle\Model\Node\Member\Member;
use Irstea\PlantUmlBundle\Model\NodeInterface;
use ReflectionClass;
......@@ -41,6 +42,11 @@ class AssociationDecorator extends AbstractDoctrineDecorator
$target = $visitor->visitClass($association['targetEntity']);
if ($target === false) {
$type = $association['targetEntity'];
if ($association["type"] & ClassMetadata::TO_MANY != 0) {
$type .= '[]';
}
$node->addAttribute(new Member($association['fieldName'], $type));
return;
}
......
<?php
/*
* © 2016 IRSTEA
* Guillaume Perréal <guillaume.perreal@irstea.fr>
* Tous droits réservés.
*/
namespace Irstea\PlantUmlBundle\Doctrine;
use Irstea\PlantUmlBundle\Model\Node\Member\Member;
use Irstea\PlantUmlBundle\Writer\WriterInterface;
/**
* Description of Field
*
* @author Guillaume Perréal <guillaume.perreal@irstea.fr>
*/
class Field extends Member
{
/**
* @var boolean
*/
private $isUnique;
/**
* @var boolean
*/
private $isNullable;
/**
* @var boolean
*/
private $isIdentifier;
public function __construct($symbol, $type, $isUnique = false, $isNullable = false, $isIdentifier = false)
{
parent::__construct($symbol, $type);
$this->isUnique = $isUnique;
$this->isNullable = $isNullable;
$this->isIdentifier = $isIdentifier;
}
protected function writeSymbolTo(WriterInterface $writer)
{
if ($this->isIdentifier) {
$writer->write('<b>');
}
if (!$this->isNullable) {
$writer->write('<i>');
}
if ($this->isUnique) {
$writer->write('<u>');
}
parent::writeSymbolTo($writer);
if ($this->isUnique) {
$writer->write('</u>');
}
if (!$this->isNullable) {
$writer->write('</i>');
}
if ($this->isIdentifier) {
$writer->write('</b>');
}
return $this;
}
}
<?php
/*
* © 2016 IRSTEA
* Guillaume Perréal <guillaume.perreal@irstea.fr>
* Tous droits réservés.
*/
namespace Irstea\PlantUmlBundle\Doctrine;
use Doctrine\ORM\Mapping\ClassMetadata;
use Irstea\PlantUmlBundle\Model\ClassVisitorInterface;
use Irstea\PlantUmlBundle\Model\NodeInterface;
/**
* Description of RelationDecorator
*
* @author Guillaume Perréal <guillaume.perreal@irstea.fr>
*/
class FieldDecorator extends AbstractDoctrineDecorator
{
protected function decorateEntity(ClassMetadata $metadata, NodeInterface $node, ClassVisitorInterface $visitor)
{
if(empty($metadata->fieldMappings)) {
return;
}
foreach($metadata->fieldMappings as $field) {
$this->visitField($node, $visitor, $field, $metadata->isIdentifier($field['fieldName']));
}
}
protected function visitField(NodeInterface $node, ClassVisitorInterface $visitor, array $field, $isIdentifier)
{
if (isset($field['inherited']) || isset($field['declared'])) {
return;
}
$node->addAttribute(new Field(
$field['fieldName'],
$field['type'],
$field['unique'],
$field['nullable'],
$isIdentifier
));
}
}
......@@ -10,6 +10,8 @@ namespace Irstea\PlantUmlBundle\Model\Node;
use Irstea\PlantUmlBundle\Model\ArrowInterface;
use Irstea\PlantUmlBundle\Model\NamespaceInterface;
use Irstea\PlantUmlBundle\Model\Node\Member\AttributeInterface;
use Irstea\PlantUmlBundle\Model\Node\Member\MethodInterface;
use Irstea\PlantUmlBundle\Model\NodeInterface;
use Irstea\PlantUmlBundle\Writer\WriterInterface;
......@@ -50,6 +52,17 @@ class BaseNode implements NodeInterface
*/
private $namespace;
/**
* @var AttributeInterface[]
*/
private $attributes = [];
/**
*
* @var MethodInterface[]
*/
private $methods = [];
/**
* @param NamespaceInterface $namespace
* @param string $name
......@@ -80,6 +93,18 @@ class BaseNode implements NodeInterface
return $this;
}
public function addAttribute(AttributeInterface $attribute)
{
$this->attributes[] = $attribute;
return $this;
}
public function addMethod(MethodInterface $method)
{
$this->methods[] = $method;
return $this;
}
public function setLabel($label)
{
$this->label = $label;
......@@ -136,11 +161,17 @@ class BaseNode implements NodeInterface
protected function writeAttributesTo(WriterInterface $writer)
{
foreach($this->attributes as $attribute) {
$attribute->writeTo($writer);
}
return $this;
}
protected function writeMethodsTo(WriterInterface $writer)
{
foreach($this->methods as $method) {
$method->writeTo($writer);
}
return $this;
}
......
<?php
/*
* © 2016 IRSTEA
* Guillaume Perréal <guillaume.perreal@irstea.fr>
* Tous droits réservés.
*/
namespace Irstea\PlantUmlBundle\Model\Node\Member;
/**
*
* @author Guillaume Perréal <guillaume.perreal@irstea.fr>
*/
interface AttributeInterface extends MemberInterface
{
}
<?php
/*
* © 2016 IRSTEA
* Guillaume Perréal <guillaume.perreal@irstea.fr>
* Tous droits réservés.
*/
namespace Irstea\PlantUmlBundle\Model\Node\Member;
use Irstea\PlantUmlBundle\Model\TypedSymbol;
use Irstea\PlantUmlBundle\Writer\WriterInterface;
/**
* Description of AbstractMember
*
* @author Guillaume Perréal <guillaume.perreal@irstea.fr>
*/
class Member extends TypedSymbol implements MethodInterface, AttributeInterface
{
/**
* @var string
*/
private $visibility;
public function __construct($symbol, $type = false, $visibility = self::UNKNOWN)
{
parent::__construct($symbol, $type);
$this->visibility = $visibility;
}
public function writeTo(WriterInterface $writer)
{
$writer->write($this->visibility);
$this->writeMemberTo($writer);
$writer->write("\n");
return $this;
}
protected function writeMemberTo(WriterInterface $writer)
{
return parent::writeTo($writer);
}
}
<?php
/*
* © 2016 IRSTEA
* Guillaume Perréal <guillaume.perreal@irstea.fr>
* Tous droits réservés.
*/
namespace Irstea\PlantUmlBundle\Model\Node\Member;
use Irstea\PlantUmlBundle\Writer\WritableInterface;
/**
*
* @author Guillaume Perréal <guillaume.perreal@irstea.fr>
*/
interface MemberInterface extends WritableInterface
{
const PRIVATE_ = '-';
const PROTECTED_ = '#';
const PUBLIC_ = '+';
const UNKNOWN = '';
}
<?php
/*
* © 2016 IRSTEA
* Guillaume Perréal <guillaume.perreal@irstea.fr>
* Tous droits réservés.
*/
namespace Irstea\PlantUmlBundle\Model\Node\Member;
/**
*
* @author Guillaume Perréal <guillaume.perreal@irstea.fr>
*/
interface MethodInterface extends MemberInterface
{
}
......@@ -8,6 +8,8 @@
namespace Irstea\PlantUmlBundle\Model;
use Irstea\PlantUmlBundle\Model\Node\Member\AttributeInterface;
use Irstea\PlantUmlBundle\Model\Node\Member\MethodInterface;
use Irstea\PlantUmlBundle\Writer\WritableNodeInterface;
/**
......@@ -30,13 +32,26 @@ interface NodeInterface extends WritableNodeInterface
/**
* @param string $classifier
* @return string
* @return self
*/
public function addClassifier($classifier);
/**
* @param string $stereotype
* @return string
* @return self
*/
public function addStereotype($stereotype);
/**
* @param AttributeInterface $attribute
* @return self
*/
public function addAttribute(AttributeInterface $attribute);
/**
* @param MethodInterface $method
* @return self
*/
public function addMethod(MethodInterface $method);
}
<?php
/*
* © 2016 IRSTEA
* Guillaume Perréal <guillaume.perreal@irstea.fr>
* Tous droits réservés.
*/
namespace Irstea\PlantUmlBundle\Model;
use Irstea\PlantUmlBundle\Writer\WritableInterface;
use Irstea\PlantUmlBundle\Writer\WriterInterface;
/**
* Description of TypedSymbol
*
* @author Guillaume Perréal <guillaume.perreal@irstea.fr>
*/
class TypedSymbol implements WritableInterface
{
/**
* @var string
*/
private $type;
/**
* @var string
*/
private $symbol;
/**
* @param type $symbol
* @param type $type
*/
public function __construct($symbol, $type = false)
{
$this->type = $type;
$this->symbol = $symbol;
}
/**
* @param WriterInterface $writer
* @return TypedSymbol
*/
public function writeTo(WriterInterface $writer)
{
return $this->writeTypeTo($writer)->writeSymbolTo($writer);
}
protected function writeTypeTo(WriterInterface $writer)
{
if ($this->type) {
$writer->writeFormatted("%s ", $this->type);
}
return $this;
}
protected function writeSymbolTo(WriterInterface $writer)
{
$writer->write($this->symbol);
return $this;
}
}
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