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

Ajout d'un "FlatNamespace".

Permet de tout représenter sans afficher les boxes des namespaces.
No related merge requests found
Showing with 209 additions and 67 deletions
+209 -67
...@@ -15,6 +15,7 @@ use Irstea\PlantUmlBundle\Model\Decorator\CompositeDecorator; ...@@ -15,6 +15,7 @@ use Irstea\PlantUmlBundle\Model\Decorator\CompositeDecorator;
use Irstea\PlantUmlBundle\Model\Decorator\FilteringDecorator; use Irstea\PlantUmlBundle\Model\Decorator\FilteringDecorator;
use Irstea\PlantUmlBundle\Model\Decorator\InheritanceDecorator; use Irstea\PlantUmlBundle\Model\Decorator\InheritanceDecorator;
use Irstea\PlantUmlBundle\Model\Filter\Whitelist; use Irstea\PlantUmlBundle\Model\Filter\Whitelist;
use Irstea\PlantUmlBundle\Model\Namespace_\FlatNamespace;
use Irstea\PlantUmlBundle\Writer\OutputWriter; use Irstea\PlantUmlBundle\Writer\OutputWriter;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
...@@ -64,7 +65,7 @@ class EntitySchemaCommand extends ContainerAwareCommand ...@@ -64,7 +65,7 @@ class EntitySchemaCommand extends ContainerAwareCommand
new Whitelist($classes) new Whitelist($classes)
); );
$visitor = new ClassVisitor($decorator); $visitor = new ClassVisitor($decorator, null, new FlatNamespace());
array_walk($classes, [$visitor, 'visitClass']); array_walk($classes, [$visitor, 'visitClass']);
......
...@@ -27,7 +27,7 @@ use ReflectionClass; ...@@ -27,7 +27,7 @@ use ReflectionClass;
class ClassVisitor implements ClassVisitorInterface, WritableInterface class ClassVisitor implements ClassVisitorInterface, WritableInterface
{ {
/** /**
* @var RootNamespace * @var NamespaceInterface
*/ */
protected $rootNamespace; protected $rootNamespace;
...@@ -41,11 +41,14 @@ class ClassVisitor implements ClassVisitorInterface, WritableInterface ...@@ -41,11 +41,14 @@ class ClassVisitor implements ClassVisitorInterface, WritableInterface
*/ */
protected $decorator; protected $decorator;
public function __construct(DecoratorInterface $decorator = null, ClassFilterInterface $filter = null) public function __construct(
{ DecoratorInterface $decorator = null,
$this->rootNamespace = new RootNamespace(); ClassFilterInterface $filter = null,
NamespaceInterface $namespace = null
) {
$this->filter = $filter ?: AcceptAllFilter::instance(); $this->filter = $filter ?: AcceptAllFilter::instance();
$this->decorator = $decorator ?: NullDecorator::instance(); $this->decorator = $decorator ?: NullDecorator::instance();
$this->rootNamespace = $namespace ?: new RootNamespace();
} }
/** /**
...@@ -98,15 +101,20 @@ class ClassVisitor implements ClassVisitorInterface, WritableInterface ...@@ -98,15 +101,20 @@ class ClassVisitor implements ClassVisitorInterface, WritableInterface
*/ */
protected function createNode(NamespaceInterface $namespace, ReflectionClass $class) protected function createNode(NamespaceInterface $namespace, ReflectionClass $class)
{ {
$className = $class->getName();
$id = str_replace('\\', '.', $className).'_node';
$label = $namespace->getShortName($className);
if ($class->isTrait()) { if ($class->isTrait()) {
return new Trait_($namespace, $class->getName()); return new Trait_($namespace, $id, $label);
} }
if ($class->isInterface()) { 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) public function outputTo(WriterInterface $writer)
......
...@@ -22,6 +22,12 @@ interface NamespaceInterface extends WritableInterface ...@@ -22,6 +22,12 @@ interface NamespaceInterface extends WritableInterface
*/ */
public function getNamespace($namespaceName); public function getNamespace($namespaceName);
/**
* @param string $name
* @return string
*/
public function getShortName($name);
/** /**
* @param NodeInterface $node * @param NodeInterface $node
* @return self * @return self
......
<?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);
}
}
...@@ -25,11 +25,6 @@ abstract class AbstractNamespace implements WritableInterface, NamespaceInterfac ...@@ -25,11 +25,6 @@ abstract class AbstractNamespace implements WritableInterface, NamespaceInterfac
*/ */
private $nodes = []; private $nodes = [];
/**
* @var NamespaceInterface
*/
private $children = [];
/** /**
* *
* @param NodeInterface $node * @param NodeInterface $node
...@@ -40,26 +35,6 @@ abstract class AbstractNamespace implements WritableInterface, NamespaceInterfac ...@@ -40,26 +35,6 @@ abstract class AbstractNamespace implements WritableInterface, NamespaceInterfac
return $this; 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 * @param WriterInterface $writer
* @return self * @return self
...@@ -72,23 +47,11 @@ abstract class AbstractNamespace implements WritableInterface, NamespaceInterfac ...@@ -72,23 +47,11 @@ abstract class AbstractNamespace implements WritableInterface, NamespaceInterfac
return $this; return $this;
} }
/**
* @param WriterInterface $writer
* @return self
*/
protected function outputChildrenTo(WriterInterface $writer)
{
foreach ($this->children as $child) {
$child->outputTo($writer);
}
return $this;
}
/** /**
* @return bool * @return bool
*/ */
protected function isEmpty() protected function isEmpty()
{ {
return empty($this->children) && empty($this->nodes); return empty($this->nodes);
} }
} }
<?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;
}
}
...@@ -17,10 +17,10 @@ use Irstea\PlantUmlBundle\Writer\WriterInterface; ...@@ -17,10 +17,10 @@ use Irstea\PlantUmlBundle\Writer\WriterInterface;
* *
* @author Guillaume Perréal <guillaume.perreal@irstea.fr> * @author Guillaume Perréal <guillaume.perreal@irstea.fr>
*/ */
class Namespace_ extends AbstractNamespace class Namespace_ extends AbstractHierachicalNamespace
{ {
/** /**
* @var NamespaceInterface * @var AbstractNamespace
*/ */
private $parent; private $parent;
...@@ -29,13 +29,13 @@ class Namespace_ extends AbstractNamespace ...@@ -29,13 +29,13 @@ class Namespace_ extends AbstractNamespace
*/ */
private $name; private $name;
public function __construct(NamespaceInterface $parent, $name) public function __construct(AbstractHierachicalNamespace $parent, $name)
{ {
$this->parent = $parent; $this->parent = $parent;
$this->name = $name; $this->name = $name;
} }
/** /**
* @param ArrowInterface $arrow * @param ArrowInterface $arrow
* @return self * @return self
*/ */
...@@ -46,6 +46,14 @@ class Namespace_ extends AbstractNamespace ...@@ -46,6 +46,14 @@ class Namespace_ extends AbstractNamespace
} }
/** /**
* @return string
*/
protected function getNamespacePrefix()
{
return $this->parent->getNamespacePrefix() . $this->name . '\\';
}
/**
* @param resource WriterInterface $writer * @param resource WriterInterface $writer
* @return self * @return self
*/ */
......
...@@ -16,7 +16,7 @@ use Irstea\PlantUmlBundle\Writer\WriterInterface; ...@@ -16,7 +16,7 @@ use Irstea\PlantUmlBundle\Writer\WriterInterface;
* *
* @author Guillaume Perréal <guillaume.perreal@irstea.fr> * @author Guillaume Perréal <guillaume.perreal@irstea.fr>
*/ */
class RootNamespace extends AbstractNamespace class RootNamespace extends AbstractHierachicalNamespace
{ {
/** /**
* @var ArrowInterface[] * @var ArrowInterface[]
...@@ -31,6 +31,7 @@ class RootNamespace extends AbstractNamespace ...@@ -31,6 +31,7 @@ class RootNamespace extends AbstractNamespace
public function outputTo(WriterInterface $writer) public function outputTo(WriterInterface $writer)
{ {
$writer->write("set namespaceSeparator .\n");
$this $this
->outputNodesTo($writer) ->outputNodesTo($writer)
->outputChildrenTo($writer) ->outputChildrenTo($writer)
...@@ -45,4 +46,9 @@ class RootNamespace extends AbstractNamespace ...@@ -45,4 +46,9 @@ class RootNamespace extends AbstractNamespace
} }
return $this; return $this;
} }
protected function getNamespacePrefix()
{
return "";
}
} }
...@@ -23,12 +23,12 @@ class BaseNode implements NodeInterface ...@@ -23,12 +23,12 @@ class BaseNode implements NodeInterface
/** /**
* @var string * @var string
*/ */
private $name; private $label;
/** /**
* @var string * @var string
*/ */
private $alias; private $id;
/** /**
* @var string * @var string
...@@ -52,18 +52,17 @@ class BaseNode implements NodeInterface ...@@ -52,18 +52,17 @@ class BaseNode implements NodeInterface
/** /**
* @param NamespaceInterface $namespace * @param NamespaceInterface $namespace
* @param string $name * @param string $id
* @param string $label
* @param string $nodeType * @param string $nodeType
* @param array $classifiers * @param array $classifiers
* @param array $stereotypes * @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; $this->namespace = $namespace;
$this->id = $id;
$pos = strrpos($name, '\\'); $this->label = $label;
$this->name = substr($name, $pos + 1);
$this->alias = str_replace('\\', '.', $name)."_node";
$this->nodeType = $nodeType; $this->nodeType = $nodeType;
$this->classifiers = $classifiers; $this->classifiers = $classifiers;
...@@ -82,12 +81,18 @@ class BaseNode implements NodeInterface ...@@ -82,12 +81,18 @@ class BaseNode implements NodeInterface
return $this; return $this;
} }
public function setLabel($label)
{
$this->label = $label;
return $this;
}
public function outputTo(WriterInterface $writer) public function outputTo(WriterInterface $writer)
{ {
$this $this
->outputClassifiersTo($writer) ->outputClassifiersTo($writer)
->outputNodeTypeTo($writer); ->outputNodeTypeTo($writer);
$writer->printf('"%s" as %s', $this->name, $this->alias); $writer->printf('"%s" as %s', $this->label, $this->id);
$this $this
->outputStereotypesTo($writer); ->outputStereotypesTo($writer);
$writer $writer
...@@ -104,7 +109,7 @@ class BaseNode implements NodeInterface ...@@ -104,7 +109,7 @@ class BaseNode implements NodeInterface
public function outputAliasTo(WriterInterface $writer) public function outputAliasTo(WriterInterface $writer)
{ {
$writer->write($this->alias); $writer->write($this->id);
return $this; return $this;
} }
......
...@@ -17,7 +17,7 @@ use Irstea\PlantUmlBundle\Model\NamespaceInterface; ...@@ -17,7 +17,7 @@ use Irstea\PlantUmlBundle\Model\NamespaceInterface;
*/ */
class Class_ extends BaseNode class Class_ extends BaseNode
{ {
public function __construct(NamespaceInterface $namespace, $name, $isAbstract, $isFinal) public function __construct(NamespaceInterface $namespace, $id, $label, $isAbstract, $isFinal)
{ {
$classifiers = []; $classifiers = [];
if ($isAbstract) { if ($isAbstract) {
...@@ -25,6 +25,6 @@ class Class_ extends BaseNode ...@@ -25,6 +25,6 @@ class Class_ extends BaseNode
} elseif ($isFinal) { } elseif ($isFinal) {
$classifiers[] = 'final'; $classifiers[] = 'final';
} }
parent::__construct($namespace, $name, "class", $classifiers, []); parent::__construct($namespace, $id, $label, "class", $classifiers, []);
} }
} }
...@@ -17,8 +17,8 @@ use Irstea\PlantUmlBundle\Model\NamespaceInterface; ...@@ -17,8 +17,8 @@ use Irstea\PlantUmlBundle\Model\NamespaceInterface;
*/ */
class Interface_ extends BaseNode 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', [], []);
} }
} }
...@@ -17,8 +17,8 @@ use Irstea\PlantUmlBundle\Model\NamespaceInterface; ...@@ -17,8 +17,8 @@ use Irstea\PlantUmlBundle\Model\NamespaceInterface;
*/ */
class Trait_ extends BaseNode 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']);
} }
} }
...@@ -16,6 +16,12 @@ use Irstea\PlantUmlBundle\Writer\WritableNodeInterface; ...@@ -16,6 +16,12 @@ use Irstea\PlantUmlBundle\Writer\WritableNodeInterface;
*/ */
interface NodeInterface extends WritableNodeInterface interface NodeInterface extends WritableNodeInterface
{ {
/**
* @param string $label
* @return self
*/
public function setLabel($label);
/** /**
* @param ArrowInterface $arrow * @param ArrowInterface $arrow
* @return self * @return self
......
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