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

Refactoring des namespaces.

No related merge requests found
Showing with 151 additions and 31 deletions
+151 -31
......@@ -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']);
......
......@@ -44,7 +44,7 @@ class AssociationDecorator extends AbstractDoctrineDecorator
return;
}
$link = "-->";
$link = "->";
if ($association["isCascadeRemove"]) {
$link = "o".$link;
}
......
<?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);
}
}
......@@ -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)
......
......@@ -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
......
<?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);
}
}
......@@ -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;
}
......
<?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);
}
}
......@@ -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
......
......@@ -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;
......
......@@ -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[]
......
......@@ -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;
......
......@@ -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, []);
}
}
......@@ -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', [], []);
}
}
......@@ -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']);
}
}
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