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.
parent e721fb6a
......@@ -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']);
......
......@@ -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)
......
......@@ -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
......
<?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
*/
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);
}
}
<?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;
*
* @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
*/
......
......@@ -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 "";
}
}
......@@ -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;
}
......
......@@ -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, []);
}
}
......@@ -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', [], []);
}
}
......@@ -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']);
}
}
......@@ -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
......
Markdown is supported
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