From 6129567166f87c374ce3f95061874634b6d8c4f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr> Date: Wed, 9 Mar 2016 16:44:39 +0100 Subject: [PATCH] Ajout d'un "FlatNamespace". MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Permet de tout représenter sans afficher les boxes des namespaces. --- Command/EntitySchemaCommand.php | 3 +- Model/ClassVisitor.php | 22 +++-- Model/NamespaceInterface.php | 6 ++ .../AbstractHierachicalNamespace.php | 81 +++++++++++++++++++ Model/Namespace_/AbstractNamespace.php | 39 +-------- Model/Namespace_/FlatNamespace.php | 58 +++++++++++++ Model/Namespace_/Namespace_.php | 16 +++- Model/Namespace_/RootNamespace.php | 8 +- Model/Node/BaseNode.php | 25 +++--- Model/Node/Class_.php | 4 +- Model/Node/Interface_.php | 4 +- Model/Node/Trait_.php | 4 +- Model/NodeInterface.php | 6 ++ 13 files changed, 209 insertions(+), 67 deletions(-) create mode 100644 Model/Namespace_/AbstractHierachicalNamespace.php create mode 100644 Model/Namespace_/FlatNamespace.php diff --git a/Command/EntitySchemaCommand.php b/Command/EntitySchemaCommand.php index a47536c..46782c7 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 9262634..5821f91 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 b2639d1..ed69141 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 0000000..9aac536 --- /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 f2826c0..db728ea 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 0000000..0eab38e --- /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 2f4977a..89fceef 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 52719da..0b87754 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 1b19d52..dcc73a7 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 d9e5477..c4b0a52 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 6d36ccf..f1926a6 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 62a50dc..2f682a3 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 7a27509..d74a344 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 -- GitLab