From 1d5a1de6d40c9c7360c93fc7458589961dc5ebf9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr>
Date: Wed, 9 Mar 2016 17:59:06 +0100
Subject: [PATCH] Ajout de nouveaux filtres.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Et expérimentations.
---
 Command/EntitySchemaCommand.php  | 28 ++++++++++++++++------
 Model/Filter/DirectoryFilter.php | 40 ++++++++++++++++++++++++++++++++
 Model/Filter/NamespaceFilter.php | 40 ++++++++++++++++++++++++++++++++
 3 files changed, 101 insertions(+), 7 deletions(-)
 create mode 100644 Model/Filter/DirectoryFilter.php
 create mode 100644 Model/Filter/NamespaceFilter.php

diff --git a/Command/EntitySchemaCommand.php b/Command/EntitySchemaCommand.php
index 6edbc7c..0a2a737 100644
--- a/Command/EntitySchemaCommand.php
+++ b/Command/EntitySchemaCommand.php
@@ -15,9 +15,12 @@ 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\DirectoryFilter;
+use Irstea\PlantUmlBundle\Model\Filter\NamespaceFilter;
 use Irstea\PlantUmlBundle\Model\Filter\Whitelist;
 use Irstea\PlantUmlBundle\Model\Namespace_\BundleNamespace;
 use Irstea\PlantUmlBundle\Model\Namespace_\FlatNamespace;
+use Irstea\PlantUmlBundle\Model\Namespace_\MappedNamespace;
 use Irstea\PlantUmlBundle\Writer\OutputWriter;
 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
 use Symfony\Component\Console\Input\InputInterface;
@@ -49,16 +52,22 @@ class EntitySchemaCommand extends ContainerAwareCommand
     {
         /* @var $manager EntityManagerInterface */
         $manager = $this->getContainer()->get('doctrine.orm.entity_manager');
-        $factory = $manager->getMetadataFactory();
 
+        $factory = $manager->getMetadataFactory();
         $allMetadata = $factory->getAllMetadata();
 
-        $classes = array_map(
-            function(ClassMetadata $metadata) { return $metadata->getName(); },
-            $allMetadata
+        $decorationFilter = new DirectoryFilter(
+            [
+                realpath($this->getContainer()->getParameter('kernel.root_dir').'/../src')
+            ]
         );
 
-        $namespace = new BundleNamespace($this->getContainer()->getParameter('kernel.bundles'));
+        $bundleNamespace = 'Irstea\\SygadeBundle\\Entity\\';
+
+        $entityFilter = new NamespaceFilter([$bundleNamespace]);
+
+        //$namespace = new BundleNamespace($this->getContainer()->getParameter('kernel.bundles'));
+        $namespace = new MappedNamespace([$bundleNamespace => '']);
 
         $decorator = new FilteringDecorator(
             new CompositeDecorator([
@@ -66,12 +75,17 @@ class EntitySchemaCommand extends ContainerAwareCommand
                 new EntityDecorator($factory),
                 new AssociationDecorator($factory),
             ]),
-            new Whitelist($classes)
+            $decorationFilter
         );
 
         $visitor = new ClassVisitor($decorator, null, $namespace);
 
-        array_walk($classes, [$visitor, 'visitClass']);
+        foreach($allMetadata as $metadata) {
+            /* @var $metadata ClassMetadata */
+            if ($entityFilter->accept($metadata->getReflectionClass())) {
+                $visitor->visitClass($metadata->getName());
+            }
+        }
 
         $writer = new OutputWriter($output);
         $writer->write("@startuml\n");
diff --git a/Model/Filter/DirectoryFilter.php b/Model/Filter/DirectoryFilter.php
new file mode 100644
index 0000000..02a8f81
--- /dev/null
+++ b/Model/Filter/DirectoryFilter.php
@@ -0,0 +1,40 @@
+<?php
+
+/*
+ * © 2016 IRSTEA
+ * Guillaume Perréal <guillaume.perreal@irstea.fr>
+ * Tous droits réservés.
+ */
+
+namespace Irstea\PlantUmlBundle\Model\Filter;
+
+use Irstea\PlantUmlBundle\Model\ClassFilterInterface;
+use ReflectionClass;
+
+/**
+ * Description of DirectoryFilter
+ *
+ * @author Guillaume Perréal <guillaume.perreal@irstea.fr>
+ */
+class DirectoryFilter implements ClassFilterInterface
+{
+    /**
+     * @var string[]
+     */
+    private $accepted = [];
+
+    public function __construct(array $accepted)
+    {
+        $this->accepted = $accepted;
+    }
+
+    public function accept(ReflectionClass $class)
+    {
+        foreach($this->accepted as $path) {
+            if (strpos($class->getFileName(), $path) === 0) {
+                return true;
+            }
+        }
+        return false;
+    }
+}
diff --git a/Model/Filter/NamespaceFilter.php b/Model/Filter/NamespaceFilter.php
new file mode 100644
index 0000000..dcafefc
--- /dev/null
+++ b/Model/Filter/NamespaceFilter.php
@@ -0,0 +1,40 @@
+<?php
+
+/*
+ * © 2016 IRSTEA
+ * Guillaume Perréal <guillaume.perreal@irstea.fr>
+ * Tous droits réservés.
+ */
+
+namespace Irstea\PlantUmlBundle\Model\Filter;
+
+use Irstea\PlantUmlBundle\Model\ClassFilterInterface;
+use ReflectionClass;
+
+/**
+ * Description of DirectoryFilter
+ *
+ * @author Guillaume Perréal <guillaume.perreal@irstea.fr>
+ */
+class NamespaceFilter implements ClassFilterInterface
+{
+    /**
+     * @var string[]
+     */
+    private $accepted = [];
+
+    public function __construct(array $accepted)
+    {
+        $this->accepted = $accepted;
+    }
+
+    public function accept(ReflectionClass $class)
+    {
+        foreach($this->accepted as $namespace) {
+            if (strpos($class->getName(), $namespace) === 0) {
+                return true;
+            }
+        }
+        return false;
+    }
+}
-- 
GitLab