diff --git a/Command/EntitySchemaCommand.php b/Command/EntitySchemaCommand.php index a47536c8067b9d4ef32ede948d63628529321af8..46782c7834f7f78a11873c60465b119032eca582 100644 --- a/Command/EntitySchemaCommand.php +++ b/Command/EntitySchemaCommand.php @@ -15,6 +15,7 @@ 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_\FlatNamespace; use Irstea\PlantUmlBundle\Writer\OutputWriter; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputInterface; @@ -64,7 +65,7 @@ class EntitySchemaCommand extends ContainerAwareCommand new Whitelist($classes) ); - $visitor = new ClassVisitor($decorator); + $visitor = new ClassVisitor($decorator, null, new FlatNamespace()); array_walk($classes, [$visitor, 'visitClass']); diff --git a/Model/ClassVisitor.php b/Model/ClassVisitor.php index 9262634dcabf037438e8f4333f02011ee1858b8c..5821f91ce594ba72039bfa3eb0387caebbb58d4f 100644 --- a/Model/ClassVisitor.php +++ b/Model/ClassVisitor.php @@ -27,7 +27,7 @@ use ReflectionClass; class ClassVisitor implements ClassVisitorInterface, WritableInterface { /** - * @var RootNamespace + * @var NamespaceInterface */ protected $rootNamespace; @@ -41,11 +41,14 @@ class ClassVisitor implements ClassVisitorInterface, WritableInterface */ protected $decorator; - public function __construct(DecoratorInterface $decorator = null, ClassFilterInterface $filter = null) - { - $this->rootNamespace = new RootNamespace(); + public function __construct( + DecoratorInterface $decorator = null, + ClassFilterInterface $filter = null, + NamespaceInterface $namespace = null + ) { $this->filter = $filter ?: AcceptAllFilter::instance(); $this->decorator = $decorator ?: NullDecorator::instance(); + $this->rootNamespace = $namespace ?: new RootNamespace(); } /** @@ -98,15 +101,20 @@ class ClassVisitor implements ClassVisitorInterface, WritableInterface */ protected function createNode(NamespaceInterface $namespace, ReflectionClass $class) { + $className = $class->getName(); + + $id = str_replace('\\', '.', $className).'_node'; + $label = $namespace->getShortName($className); + if ($class->isTrait()) { - return new Trait_($namespace, $class->getName()); + return new Trait_($namespace, $id, $label); } if ($class->isInterface()) { - return new Interface_($namespace, $class->getName()); + return new Interface_($namespace, $id, $label, $class->getName()); } - return new Class_($namespace, $class->getName(), $class->isAbstract(), $class->isFinal()); + return new Class_($namespace, $id, $label, $class->isAbstract(), $class->isFinal()); } public function outputTo(WriterInterface $writer) diff --git a/Model/NamespaceInterface.php b/Model/NamespaceInterface.php index b2639d1e0c3e0eb93ef7a4d4ac4839a693d7aa7b..ed69141aeb55574cd6fee13cae95817a0dfe45d6 100644 --- a/Model/NamespaceInterface.php +++ b/Model/NamespaceInterface.php @@ -22,6 +22,12 @@ interface NamespaceInterface extends WritableInterface */ public function getNamespace($namespaceName); + /** + * @param string $name + * @return string + */ + public function getShortName($name); + /** * @param NodeInterface $node * @return self diff --git a/Model/Namespace_/AbstractHierachicalNamespace.php b/Model/Namespace_/AbstractHierachicalNamespace.php new file mode 100644 index 0000000000000000000000000000000000000000..9aac536efb6c8ec3d1b6b1351f67ec83297a2155 --- /dev/null +++ b/Model/Namespace_/AbstractHierachicalNamespace.php @@ -0,0 +1,81 @@ +<?php + +/* + * © 2016 IRSTEA + * Guillaume Perréal <guillaume.perreal@irstea.fr> + * Tous droits réservés. + */ + +namespace Irstea\PlantUmlBundle\Model\Namespace_; + +use Irstea\PlantUmlBundle\Model\NamespaceInterface; +use Irstea\PlantUmlBundle\Model\NodeInterface; +use Irstea\PlantUmlBundle\Writer\WritableInterface; +use Irstea\PlantUmlBundle\Writer\WriterInterface; + +/** + * Description of Namespace + * + * @author Guillaume Perréal <guillaume.perreal@irstea.fr> + */ +abstract class AbstractHierachicalNamespace extends AbstractNamespace +{ + /** + * @var NamespaceInterface + */ + private $children = []; + + /** + * @param type $namespaceName + * @return NamespaceInterface + */ + public function getNamespace($namespaceName) + { + $namespaceName = trim($namespaceName, '\\'); + if (!$namespaceName) { + return $this; + } + @list($head, $tail) = explode('\\', $namespaceName, 2); + if (!isset($this->children[$head])) { + $this->children[$head] = new Namespace_($this, $head); + } + if (empty($tail)) { + return $this->children[$head]; + } + return $this->children[$head]->getNamespace($tail); + } + + /** + * @return $string + */ + abstract protected function getNamespacePrefix(); + + public function getShortName($name) + { + $prefix = $this->getNamespacePrefix(); + if (0 === strpos($name, $prefix)) { + return substr($name, strlen($prefix)); + } + return $name; + } + + /** + * @param WriterInterface $writer + * @return self + */ + protected function outputChildrenTo(WriterInterface $writer) + { + foreach ($this->children as $child) { + $child->outputTo($writer); + } + return $this; + } + + /** + * @return bool + */ + protected function isEmpty() + { + return parent::isEmpty() && empty($this->children); + } +} diff --git a/Model/Namespace_/AbstractNamespace.php b/Model/Namespace_/AbstractNamespace.php index f2826c043f58447cb6794c05880fd089218a3b0a..db728eaadb1be96b0d49b2f890780f97a308f14b 100644 --- a/Model/Namespace_/AbstractNamespace.php +++ b/Model/Namespace_/AbstractNamespace.php @@ -25,11 +25,6 @@ abstract class AbstractNamespace implements WritableInterface, NamespaceInterfac */ private $nodes = []; - /** - * @var NamespaceInterface - */ - private $children = []; - /** * * @param NodeInterface $node @@ -40,26 +35,6 @@ abstract class AbstractNamespace implements WritableInterface, NamespaceInterfac return $this; } - /** - * @param type $namespaceName - * @return NamespaceInterface - */ - public function getNamespace($namespaceName) - { - $namespaceName = trim($namespaceName, '\\'); - if (!$namespaceName) { - return $this; - } - @list($head, $tail) = explode('\\', $namespaceName, 2); - if (!isset($this->children[$head])) { - $this->children[$head] = new Namespace_($this, $head); - } - if (empty($tail)) { - return $this->children[$head]; - } - return $this->children[$head]->getNamespace($tail); - } - /** * @param WriterInterface $writer * @return self @@ -72,23 +47,11 @@ abstract class AbstractNamespace implements WritableInterface, NamespaceInterfac return $this; } - /** - * @param WriterInterface $writer - * @return self - */ - protected function outputChildrenTo(WriterInterface $writer) - { - foreach ($this->children as $child) { - $child->outputTo($writer); - } - return $this; - } - /** * @return bool */ protected function isEmpty() { - return empty($this->children) && empty($this->nodes); + return empty($this->nodes); } } diff --git a/Model/Namespace_/FlatNamespace.php b/Model/Namespace_/FlatNamespace.php new file mode 100644 index 0000000000000000000000000000000000000000..0eab38e81db774ea855e2fd48f5a5048875eaf38 --- /dev/null +++ b/Model/Namespace_/FlatNamespace.php @@ -0,0 +1,58 @@ +<?php + +/* + * © 2016 IRSTEA + * Guillaume Perréal <guillaume.perreal@irstea.fr> + * Tous droits réservés. + */ + +namespace Irstea\PlantUmlBundle\Model\Namespace_; + +use Irstea\PlantUmlBundle\Model\ArrowInterface; +use Irstea\PlantUmlBundle\Writer\WriterInterface; + +/** + * Description of RootNamespace + * + * @author Guillaume Perréal <guillaume.perreal@irstea.fr> + */ +class FlatNamespace extends AbstractNamespace +{ + /** + * @var ArrowInterface[] + */ + private $arrows = []; + + public function addArrow(ArrowInterface $arrow) + { + $this->arrows[] = $arrow; + return $this; + } + + public function outputTo(WriterInterface $writer) + { + $writer->write("set namespaceSeparator none\n"); + $this + ->outputNodesTo($writer) + ->outputArrowsTo($writer); + return $this; + } + + protected function outputArrowsTo(WriterInterface $writer) + { + foreach ($this->arrows as $arrow) { + $arrow->outputTo($writer); + } + return $this; + } + + public function getNamespace($namespaceName) + { + return $this; + } + + public function getShortName($name) + { + return $name; + } +} diff --git a/Model/Namespace_/Namespace_.php b/Model/Namespace_/Namespace_.php index 2f4977acfee00472aefd0811956bf54e99697173..89fceef9ef4f947c7eabcc22037308f1fc8a33fe 100644 --- a/Model/Namespace_/Namespace_.php +++ b/Model/Namespace_/Namespace_.php @@ -17,10 +17,10 @@ use Irstea\PlantUmlBundle\Writer\WriterInterface; * * @author Guillaume Perréal <guillaume.perreal@irstea.fr> */ -class Namespace_ extends AbstractNamespace +class Namespace_ extends AbstractHierachicalNamespace { /** - * @var NamespaceInterface + * @var AbstractNamespace */ private $parent; @@ -29,13 +29,13 @@ class Namespace_ extends AbstractNamespace */ private $name; - public function __construct(NamespaceInterface $parent, $name) + public function __construct(AbstractHierachicalNamespace $parent, $name) { $this->parent = $parent; $this->name = $name; } - /** + /** * @param ArrowInterface $arrow * @return self */ @@ -46,6 +46,14 @@ class Namespace_ extends AbstractNamespace } /** + * @return string + */ + protected function getNamespacePrefix() + { + return $this->parent->getNamespacePrefix() . $this->name . '\\'; + } + + /** * @param resource WriterInterface $writer * @return self */ diff --git a/Model/Namespace_/RootNamespace.php b/Model/Namespace_/RootNamespace.php index 52719daabac863ae1e2c2e83653cbcd0d73b44eb..0b87754a8d1b551fd36ea5b548790d1da6024833 100644 --- a/Model/Namespace_/RootNamespace.php +++ b/Model/Namespace_/RootNamespace.php @@ -16,7 +16,7 @@ use Irstea\PlantUmlBundle\Writer\WriterInterface; * * @author Guillaume Perréal <guillaume.perreal@irstea.fr> */ -class RootNamespace extends AbstractNamespace +class RootNamespace extends AbstractHierachicalNamespace { /** * @var ArrowInterface[] @@ -31,6 +31,7 @@ class RootNamespace extends AbstractNamespace public function outputTo(WriterInterface $writer) { + $writer->write("set namespaceSeparator .\n"); $this ->outputNodesTo($writer) ->outputChildrenTo($writer) @@ -45,4 +46,9 @@ class RootNamespace extends AbstractNamespace } return $this; } + + protected function getNamespacePrefix() + { + return ""; + } } diff --git a/Model/Node/BaseNode.php b/Model/Node/BaseNode.php index 1b19d52af3b597eb6c471069f61d83a32cf20131..dcc73a7030133074358f30ee6e261c168eb217a7 100644 --- a/Model/Node/BaseNode.php +++ b/Model/Node/BaseNode.php @@ -23,12 +23,12 @@ class BaseNode implements NodeInterface /** * @var string */ - private $name; + private $label; /** * @var string */ - private $alias; + private $id; /** * @var string @@ -52,18 +52,17 @@ class BaseNode implements NodeInterface /** * @param NamespaceInterface $namespace - * @param string $name + * @param string $id + * @param string $label * @param string $nodeType * @param array $classifiers * @param array $stereotypes */ - function __construct(NamespaceInterface $namespace, $name, $nodeType, array $classifiers = [], array $stereotypes = []) + function __construct(NamespaceInterface $namespace, $id, $label, $nodeType, array $classifiers = [], array $stereotypes = []) { $this->namespace = $namespace; - - $pos = strrpos($name, '\\'); - $this->name = substr($name, $pos + 1); - $this->alias = str_replace('\\', '.', $name)."_node"; + $this->id = $id; + $this->label = $label; $this->nodeType = $nodeType; $this->classifiers = $classifiers; @@ -82,12 +81,18 @@ class BaseNode implements NodeInterface return $this; } + public function setLabel($label) + { + $this->label = $label; + return $this; + } + public function outputTo(WriterInterface $writer) { $this ->outputClassifiersTo($writer) ->outputNodeTypeTo($writer); - $writer->printf('"%s" as %s', $this->name, $this->alias); + $writer->printf('"%s" as %s', $this->label, $this->id); $this ->outputStereotypesTo($writer); $writer @@ -104,7 +109,7 @@ class BaseNode implements NodeInterface public function outputAliasTo(WriterInterface $writer) { - $writer->write($this->alias); + $writer->write($this->id); return $this; } diff --git a/Model/Node/Class_.php b/Model/Node/Class_.php index d9e5477c26d952f1d398076bf9a95ee165aac4ab..c4b0a52d5bc793e37b9f32a59ecc76f196fbfd40 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, $name, $isAbstract, $isFinal) + public function __construct(NamespaceInterface $namespace, $id, $label, $isAbstract, $isFinal) { $classifiers = []; if ($isAbstract) { @@ -25,6 +25,6 @@ class Class_ extends BaseNode } elseif ($isFinal) { $classifiers[] = 'final'; } - parent::__construct($namespace, $name, "class", $classifiers, []); + parent::__construct($namespace, $id, $label, "class", $classifiers, []); } } diff --git a/Model/Node/Interface_.php b/Model/Node/Interface_.php index 6d36ccf14dff3e397b6430c925696b04b08d36ef..f1926a69ceb062f4710f8dff27bf673960cee6da 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, $name) + public function __construct(NamespaceInterface $namespace, $id, $label) { - parent::__construct($namespace, $name, 'interface', [], []); + parent::__construct($namespace, $id, $label, 'interface', [], []); } } diff --git a/Model/Node/Trait_.php b/Model/Node/Trait_.php index 62a50dcb3dd0ac8fde328e75d9481ce9eb8636e8..2f682a3cc0f6d9672102d208f65fb2cd0c93e8d2 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, $name) + public function __construct(NamespaceInterface $namespace, $id, $label) { - parent::__construct($namespace, $name, 'class', [], ['trait']); + parent::__construct($namespace, $id, $label, 'class', [], ['trait']); } } diff --git a/Model/NodeInterface.php b/Model/NodeInterface.php index 7a27509249fc34d7d94b327a2c8937101d960739..d74a3441b1a6b1fa261ad0a402ef8d0c1da35553 100644 --- a/Model/NodeInterface.php +++ b/Model/NodeInterface.php @@ -16,6 +16,12 @@ use Irstea\PlantUmlBundle\Writer\WritableNodeInterface; */ interface NodeInterface extends WritableNodeInterface { + /** + * @param string $label + * @return self + */ + public function setLabel($label); + /** * @param ArrowInterface $arrow * @return self