diff --git a/Command/EntitySchemaCommand.php b/Command/EntitySchemaCommand.php index 46782c7834f7f78a11873c60465b119032eca582..6edbc7cd9d24e7447aa57b2167339e9b54b5586f 100644 --- a/Command/EntitySchemaCommand.php +++ b/Command/EntitySchemaCommand.php @@ -9,12 +9,14 @@ namespace Irstea\PlantUmlBundle\Command; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\ClassMetadata; use Irstea\PlantUmlBundle\Doctrine\AssociationDecorator; +use Irstea\PlantUmlBundle\Doctrine\DoctrineNamespace; use Irstea\PlantUmlBundle\Doctrine\EntityDecorator; use Irstea\PlantUmlBundle\Model\ClassVisitor; use Irstea\PlantUmlBundle\Model\Decorator\CompositeDecorator; use Irstea\PlantUmlBundle\Model\Decorator\FilteringDecorator; use Irstea\PlantUmlBundle\Model\Decorator\InheritanceDecorator; use Irstea\PlantUmlBundle\Model\Filter\Whitelist; +use Irstea\PlantUmlBundle\Model\Namespace_\BundleNamespace; use Irstea\PlantUmlBundle\Model\Namespace_\FlatNamespace; use Irstea\PlantUmlBundle\Writer\OutputWriter; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; @@ -56,6 +58,8 @@ class EntitySchemaCommand extends ContainerAwareCommand $allMetadata ); + $namespace = new BundleNamespace($this->getContainer()->getParameter('kernel.bundles')); + $decorator = new FilteringDecorator( new CompositeDecorator([ new InheritanceDecorator(), @@ -65,7 +69,7 @@ class EntitySchemaCommand extends ContainerAwareCommand new Whitelist($classes) ); - $visitor = new ClassVisitor($decorator, null, new FlatNamespace()); + $visitor = new ClassVisitor($decorator, null, $namespace); array_walk($classes, [$visitor, 'visitClass']); diff --git a/Doctrine/AssociationDecorator.php b/Doctrine/AssociationDecorator.php index a5027fd207c167f7e283c9ad4c31578bae854ebe..9d15b709da408bcc2f8ee58811a6fa4648ce38ad 100644 --- a/Doctrine/AssociationDecorator.php +++ b/Doctrine/AssociationDecorator.php @@ -44,7 +44,7 @@ class AssociationDecorator extends AbstractDoctrineDecorator return; } - $link = "-->"; + $link = "->"; if ($association["isCascadeRemove"]) { $link = "o".$link; } diff --git a/Doctrine/DoctrineNamespace.php b/Doctrine/DoctrineNamespace.php new file mode 100644 index 0000000000000000000000000000000000000000..6281536fcb08247deca991b9abf4ce363afca02c --- /dev/null +++ b/Doctrine/DoctrineNamespace.php @@ -0,0 +1,28 @@ +<?php + +/* + * © 2016 IRSTEA + * Guillaume Perréal <guillaume.perreal@irstea.fr> + * Tous droits réservés. + */ + +namespace Irstea\PlantUmlBundle\Doctrine; + +/** + * Description of DoctrineNamespace + * + * @author Guillaume Perréal <guillaume.perreal@irstea.fr> + */ +class DoctrineNamespace extends \Irstea\PlantUmlBundle\Model\Namespace_\MappedNamespace +{ + const SEPARATOR = '::'; + + public function __construct(array $entityNamespaces) + { + $mapping = []; + foreach($entityNamespaces as $alias => $namespace) { + $mapping[$namespace.'\\'] = $alias.'::'; + } + parent::__construct($mapping); + } +} diff --git a/Model/ClassVisitor.php b/Model/ClassVisitor.php index 5821f91ce594ba72039bfa3eb0387caebbb58d4f..75730ae8dcb501e520e6601466af9dd00541e8e9 100644 --- a/Model/ClassVisitor.php +++ b/Model/ClassVisitor.php @@ -10,12 +10,11 @@ namespace Irstea\PlantUmlBundle\Model; use Irstea\PlantUmlBundle\Model\Decorator\NullDecorator; use Irstea\PlantUmlBundle\Model\Filter\AcceptAllFilter; -use Irstea\PlantUmlBundle\Model\Namespace_\RootNamespace; +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\WritableInterface; -use Irstea\PlantUmlBundle\Writer\WritableNodeInterface; use Irstea\PlantUmlBundle\Writer\WriterInterface; use ReflectionClass; @@ -103,18 +102,15 @@ class ClassVisitor implements ClassVisitorInterface, WritableInterface { $className = $class->getName(); - $id = str_replace('\\', '.', $className).'_node'; - $label = $namespace->getShortName($className); - if ($class->isTrait()) { - return new Trait_($namespace, $id, $label); + return new Trait_($namespace, $className); } if ($class->isInterface()) { - return new Interface_($namespace, $id, $label, $class->getName()); + return new Interface_($namespace, $className); } - return new Class_($namespace, $id, $label, $class->isAbstract(), $class->isFinal()); + return new Class_($namespace, $className, $class->isAbstract(), $class->isFinal()); } public function outputTo(WriterInterface $writer) diff --git a/Model/NamespaceInterface.php b/Model/NamespaceInterface.php index ed69141aeb55574cd6fee13cae95817a0dfe45d6..5fbabf0ae45b79483678ce77cd490d5ca5c79a3d 100644 --- a/Model/NamespaceInterface.php +++ b/Model/NamespaceInterface.php @@ -23,10 +23,16 @@ interface NamespaceInterface extends WritableInterface public function getNamespace($namespaceName); /** - * @param string $name + * @param string $className * @return string */ - public function getShortName($name); + public function getNodeId($className); + + /** + * @param string $className + * @return string + */ + public function getNodeLabel($className); /** * @param NodeInterface $node diff --git a/Model/Namespace_/BundleNamespace.php b/Model/Namespace_/BundleNamespace.php new file mode 100644 index 0000000000000000000000000000000000000000..63aa185aac8a250c602105940522769511b16b98 --- /dev/null +++ b/Model/Namespace_/BundleNamespace.php @@ -0,0 +1,29 @@ +<?php + +/* + * © 2016 IRSTEA + * Guillaume Perréal <guillaume.perreal@irstea.fr> + * Tous droits réservés. + */ + +namespace Irstea\PlantUmlBundle\Model\Namespace_; + +/** + * Description of BundleNamespace + * + * @author Guillaume Perréal <guillaume.perreal@irstea.fr> + */ +class BundleNamespace extends MappedNamespace +{ + const SEPARATOR = '::'; + + public function __construct(array $bundles) + { + $mapping = []; + foreach($bundles as $bundle => $php) { + $mapping[substr($php, 0, 1+strrpos($php, '\\'))] = $bundle.'::'; + } + + parent::__construct($mapping); + } +} diff --git a/Model/Namespace_/FlatNamespace.php b/Model/Namespace_/FlatNamespace.php index 0eab38e81db774ea855e2fd48f5a5048875eaf38..587fb8d6288f6e976c7ca524cc912b7e16d4fd13 100644 --- a/Model/Namespace_/FlatNamespace.php +++ b/Model/Namespace_/FlatNamespace.php @@ -18,6 +18,8 @@ use Irstea\PlantUmlBundle\Writer\WriterInterface; */ class FlatNamespace extends AbstractNamespace { + const SEPARATOR = 'none'; + /** * @var ArrowInterface[] */ @@ -31,7 +33,7 @@ class FlatNamespace extends AbstractNamespace public function outputTo(WriterInterface $writer) { - $writer->write("set namespaceSeparator none\n"); + $writer->printf("set namespaceSeparator %s\n", static::SEPARATOR); $this ->outputNodesTo($writer) ->outputArrowsTo($writer); @@ -51,7 +53,12 @@ class FlatNamespace extends AbstractNamespace return $this; } - public function getShortName($name) + public function getNodeId($name) + { + return str_replace('\\', '_', $name); + } + + public function getNodeLabel($name) { return $name; } diff --git a/Model/Namespace_/MappedNamespace.php b/Model/Namespace_/MappedNamespace.php new file mode 100644 index 0000000000000000000000000000000000000000..c395f08261c4eeb305bd7fde75f005af8d09db35 --- /dev/null +++ b/Model/Namespace_/MappedNamespace.php @@ -0,0 +1,46 @@ +<?php + +/* + * © 2016 IRSTEA + * Guillaume Perréal <guillaume.perreal@irstea.fr> + * Tous droits réservés. + */ + +namespace Irstea\PlantUmlBundle\Model\Namespace_; + +/** + * Description of BundleNamespace + * + * @author Guillaume Perréal <guillaume.perreal@irstea.fr> + */ +class MappedNamespace extends FlatNamespace +{ + const SEPARATOR = '::'; + + /** + * @var string[] + */ + private $mappedNamespaces = []; + + /** + * @var string[] + */ + private $phpNamespaces = []; + + public function __construct(array $mapping) + { + $this->phpNamespaces = array_keys($mapping); + $this->mappedNamespaces = array_values($mapping); + } + + public function getNodeId($name) + { + return str_replace('\\', '__', str_replace($this->phpNamespaces, $this->mappedNamespaces, $name)); + } + + public function getNodeLabel($name) + { + return str_replace($this->phpNamespaces, '', $name); + } + +} diff --git a/Model/Namespace_/AbstractHierachicalNamespace.php b/Model/Namespace_/Php/AbstractNamespace.php similarity index 87% rename from Model/Namespace_/AbstractHierachicalNamespace.php rename to Model/Namespace_/Php/AbstractNamespace.php index 9aac536efb6c8ec3d1b6b1351f67ec83297a2155..2d591ee16b8106ca6be534093332589820c1f821 100644 --- a/Model/Namespace_/AbstractHierachicalNamespace.php +++ b/Model/Namespace_/Php/AbstractNamespace.php @@ -6,7 +6,7 @@ * Tous droits réservés. */ -namespace Irstea\PlantUmlBundle\Model\Namespace_; +namespace Irstea\PlantUmlBundle\Model\Namespace_\Php; use Irstea\PlantUmlBundle\Model\NamespaceInterface; use Irstea\PlantUmlBundle\Model\NodeInterface; @@ -18,7 +18,7 @@ use Irstea\PlantUmlBundle\Writer\WriterInterface; * * @author Guillaume Perréal <guillaume.perreal@irstea.fr> */ -abstract class AbstractHierachicalNamespace extends AbstractNamespace +abstract class AbstractNamespace extends AbstractNamespace { /** * @var NamespaceInterface @@ -50,7 +50,7 @@ abstract class AbstractHierachicalNamespace extends AbstractNamespace */ abstract protected function getNamespacePrefix(); - public function getShortName($name) + public function getNodeLabel($name) { $prefix = $this->getNamespacePrefix(); if (0 === strpos($name, $prefix)) { @@ -59,6 +59,11 @@ abstract class AbstractHierachicalNamespace extends AbstractNamespace return $name; } + public function getNodeId($name) + { + return str_replace('\\', '.', $name).'_node'; + } + /** * @param WriterInterface $writer * @return self diff --git a/Model/Namespace_/Namespace_.php b/Model/Namespace_/Php/Namespace_.php similarity index 96% rename from Model/Namespace_/Namespace_.php rename to Model/Namespace_/Php/Namespace_.php index 89fceef9ef4f947c7eabcc22037308f1fc8a33fe..8c6da0bb3a05bb01468f99467be482657b5e6c98 100644 --- a/Model/Namespace_/Namespace_.php +++ b/Model/Namespace_/Php/Namespace_.php @@ -6,7 +6,7 @@ * Tous droits réservés. */ -namespace Irstea\PlantUmlBundle\Model\Namespace_; +namespace Irstea\PlantUmlBundle\Model\Namespace_\Php; use Irstea\PlantUmlBundle\Model\ArrowInterface; use Irstea\PlantUmlBundle\Model\NamespaceInterface; diff --git a/Model/Namespace_/RootNamespace.php b/Model/Namespace_/Php/RootNamespace.php similarity index 90% rename from Model/Namespace_/RootNamespace.php rename to Model/Namespace_/Php/RootNamespace.php index 0b87754a8d1b551fd36ea5b548790d1da6024833..90c5f1a391075ef9a16020a3bef9bc7c78a67032 100644 --- a/Model/Namespace_/RootNamespace.php +++ b/Model/Namespace_/Php/RootNamespace.php @@ -6,7 +6,7 @@ * Tous droits réservés. */ -namespace Irstea\PlantUmlBundle\Model\Namespace_; +namespace Irstea\PlantUmlBundle\Model\Namespace_\Php; use Irstea\PlantUmlBundle\Model\ArrowInterface; use Irstea\PlantUmlBundle\Writer\WriterInterface; @@ -16,7 +16,7 @@ use Irstea\PlantUmlBundle\Writer\WriterInterface; * * @author Guillaume Perréal <guillaume.perreal@irstea.fr> */ -class RootNamespace extends AbstractHierachicalNamespace +class RootNamespace extends AbstractNamespace { /** * @var ArrowInterface[] diff --git a/Model/Node/BaseNode.php b/Model/Node/BaseNode.php index dcc73a7030133074358f30ee6e261c168eb217a7..1be3396715f59312754bc495811714d5460f06a1 100644 --- a/Model/Node/BaseNode.php +++ b/Model/Node/BaseNode.php @@ -52,17 +52,16 @@ class BaseNode implements NodeInterface /** * @param NamespaceInterface $namespace - * @param string $id - * @param string $label + * @param string $name * @param string $nodeType * @param array $classifiers * @param array $stereotypes */ - function __construct(NamespaceInterface $namespace, $id, $label, $nodeType, array $classifiers = [], array $stereotypes = []) + function __construct(NamespaceInterface $namespace, $name, $nodeType, array $classifiers = [], array $stereotypes = []) { $this->namespace = $namespace; - $this->id = $id; - $this->label = $label; + $this->id = $namespace->getNodeId($name); + $this->label = $namespace->getNodeLabel($name); $this->nodeType = $nodeType; $this->classifiers = $classifiers; diff --git a/Model/Node/Class_.php b/Model/Node/Class_.php index c4b0a52d5bc793e37b9f32a59ecc76f196fbfd40..d9e5477c26d952f1d398076bf9a95ee165aac4ab 100644 --- a/Model/Node/Class_.php +++ b/Model/Node/Class_.php @@ -17,7 +17,7 @@ use Irstea\PlantUmlBundle\Model\NamespaceInterface; */ class Class_ extends BaseNode { - public function __construct(NamespaceInterface $namespace, $id, $label, $isAbstract, $isFinal) + public function __construct(NamespaceInterface $namespace, $name, $isAbstract, $isFinal) { $classifiers = []; if ($isAbstract) { @@ -25,6 +25,6 @@ class Class_ extends BaseNode } elseif ($isFinal) { $classifiers[] = 'final'; } - parent::__construct($namespace, $id, $label, "class", $classifiers, []); + parent::__construct($namespace, $name, "class", $classifiers, []); } } diff --git a/Model/Node/Interface_.php b/Model/Node/Interface_.php index f1926a69ceb062f4710f8dff27bf673960cee6da..6d36ccf14dff3e397b6430c925696b04b08d36ef 100644 --- a/Model/Node/Interface_.php +++ b/Model/Node/Interface_.php @@ -17,8 +17,8 @@ use Irstea\PlantUmlBundle\Model\NamespaceInterface; */ class Interface_ extends BaseNode { - public function __construct(NamespaceInterface $namespace, $id, $label) + public function __construct(NamespaceInterface $namespace, $name) { - parent::__construct($namespace, $id, $label, 'interface', [], []); + parent::__construct($namespace, $name, 'interface', [], []); } } diff --git a/Model/Node/Trait_.php b/Model/Node/Trait_.php index 2f682a3cc0f6d9672102d208f65fb2cd0c93e8d2..62a50dcb3dd0ac8fde328e75d9481ce9eb8636e8 100644 --- a/Model/Node/Trait_.php +++ b/Model/Node/Trait_.php @@ -17,8 +17,8 @@ use Irstea\PlantUmlBundle\Model\NamespaceInterface; */ class Trait_ extends BaseNode { - public function __construct(NamespaceInterface $namespace, $id, $label) + public function __construct(NamespaceInterface $namespace, $name) { - parent::__construct($namespace, $id, $label, 'class', [], ['trait']); + parent::__construct($namespace, $name, 'class', [], ['trait']); } }