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

Ajout d'un filtre sur les noms de classe.

parent c80e08d3
...@@ -144,6 +144,10 @@ class Configuration implements ConfigurationInterface ...@@ -144,6 +144,10 @@ class Configuration implements ConfigurationInterface
->info("Namespaces $description") ->info("Namespaces $description")
->prototype('scalar')->end() ->prototype('scalar')->end()
->end() ->end()
->arrayNode('classes')
->info("Classes $description")
->prototype('scalar')->end()
->end()
->end(); ->end();
return $node; return $node;
......
...@@ -10,6 +10,7 @@ namespace Irstea\PlantUmlBundle\Factory; ...@@ -10,6 +10,7 @@ namespace Irstea\PlantUmlBundle\Factory;
use Irstea\PlantUmlBundle\Model\ClassFilterInterface; use Irstea\PlantUmlBundle\Model\ClassFilterInterface;
use Irstea\PlantUmlBundle\Model\Filter\AcceptAllFilter; use Irstea\PlantUmlBundle\Model\Filter\AcceptAllFilter;
use Irstea\PlantUmlBundle\Model\Filter\ClassFilter;
use Irstea\PlantUmlBundle\Model\Filter\Composite\AllFilter; use Irstea\PlantUmlBundle\Model\Filter\Composite\AllFilter;
use Irstea\PlantUmlBundle\Model\Filter\DirectoryFilter; use Irstea\PlantUmlBundle\Model\Filter\DirectoryFilter;
use Irstea\PlantUmlBundle\Model\Filter\NamespaceFilter; use Irstea\PlantUmlBundle\Model\Filter\NamespaceFilter;
...@@ -42,45 +43,50 @@ class FilterFactory ...@@ -42,45 +43,50 @@ class FilterFactory
$filters = []; $filters = [];
if (!empty($include)) { if (!empty($include)) {
$filters[] = $this->createSubFilters($include, false); $this->appendFilters($filters, $include, false);
} }
if (!empty($exclude)) { if (!empty($exclude)) {
$filters[] = $this->createSubFilters($exclude, true); $this->appendFilters($filters, $exclude, true);
} }
$filters = array_merge($filters);
switch(count($filters)) { switch(count($filters)) {
case 0: case 0:
return AcceptAllFilter::instance(); $filter = AcceptAllFilter::instance();
break;
case 1: case 1:
return $filters[0]; $filter = $filters[0];
break;
default: default:
return new AllFilter($filters); $filter = new AllFilter($filters);
} }
dump(['include' => $include, 'exclude' => $exclude, 'filter' => $filter->describe()]);
return $filter;
} }
protected function createSubFilters(array $config, $notFound) protected function appendFilters(array &$filters, array $config, $notFound)
{ {
$filters = [];
if (!empty($config['directories'])) { if (!empty($config['directories'])) {
$paths = $this->parseDirectories($config['directories']); $paths = $this->expandBundlePaths($config['directories']);
$filters[] = new DirectoryFilter($paths, $notFound); $filters[] = new DirectoryFilter($paths, $notFound);
} }
if (!empty($config['namespaces'])) { if (!empty($config['namespaces'])) {
$namespaces = $this->parseNamespaces($config['namespaces']); $namespaces = $this->expandBundleNamespaces($config['namespaces']);
$filters[] = new NamespaceFilter($namespaces, $notFound); $filters[] = new NamespaceFilter($namespaces, $notFound);
} }
return $filters; if (!empty($config['classes'])) {
$classes = $this->expandBundleNamespaces($config['classes']);
$filters[] = new ClassFilter($classes, $notFound);
}
} }
/** /**
* @param array $paths * @param array $paths
* @return array * @return array
*/ */
protected function parseDirectories(array $paths) protected function expandBundlePaths(array $paths)
{ {
$actualPaths = []; $actualPaths = [];
foreach($paths as $path) { foreach($paths as $path) {
...@@ -97,16 +103,16 @@ class FilterFactory ...@@ -97,16 +103,16 @@ class FilterFactory
* @param array $paths * @param array $paths
* @return array * @return array
*/ */
protected function parseNamespaces(array $namespaces) protected function expandBundleNamespaces(array $names)
{ {
$actualNamespaces = []; $actualNames = [];
foreach($namespaces as $namespace) { foreach($names as $name) {
if (preg_match('/^@(\w+)(.*)$/', $namespace, $groups)) { if (preg_match('/^@(\w+)(.*)$/', $name, $groups)) {
$bundle = $this->kernel->getBundle($groups[1]); $bundle = $this->kernel->getBundle($groups[1]);
$namespace = $bundle->getNamespace() . $groups[2]; $name = $bundle->getNamespace() . $groups[2];
} }
$actualNamespaces[] = $namespace; $actualNames[] = $name;
} }
return $actualNamespaces; return $actualNames;
} }
} }
...@@ -12,24 +12,48 @@ use Irstea\PlantUmlBundle\Model\ClassFilterInterface; ...@@ -12,24 +12,48 @@ use Irstea\PlantUmlBundle\Model\ClassFilterInterface;
use ReflectionClass; use ReflectionClass;
/** /**
* Description of Whiltelist * Description of DirectoryFilter
* *
* @author Guillaume Perréal <guillaume.perreal@irstea.fr> * @author Guillaume Perréal <guillaume.perreal@irstea.fr>
*/ */
class Whitelist implements ClassFilterInterface class ClassFilter implements ClassFilterInterface
{ {
/** /**
* @var string[] * @var string[]
*/ */
private $accepted; private $classes = [];
public function __construct(array $classNames) /**
* @var boolena
*/
private $notFound;
public function __construct(array $classes, $notFound = false)
{ {
$this->accepted = array_fill_keys($classNames, true); $this->classes = array_map(
function ($class) {
return trim($class, '\\');
},
$classes
);
$this->notFound = $notFound;
} }
public function accept(ReflectionClass $class) public function accept(ReflectionClass $class)
{ {
return isset($this->accepted[$class->getName()]); $className = $class->getName();
if (in_array($className, $this->classes)) {
return !$this->notFound;
}
return $this->notFound;
}
public function describe()
{
return sprintf(
"Class is%s [%s]",
$this->notFound ? '' : ' not',
implode(', ', $this->classes)
);
} }
} }
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