From 8f94a20a1c24bcb0db5b38138e1e6011f1ca362f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr> Date: Thu, 12 May 2016 13:45:50 +0200 Subject: [PATCH] Ajout d'un filtre sur les noms de classe. --- DependencyInjection/Configuration.php | 4 ++ Factory/FilterFactory.php | 46 ++++++++++++--------- Model/Filter/ClassFilter.php | 59 +++++++++++++++++++++++++++ Model/Filter/Whitelist.php | 35 ---------------- 4 files changed, 89 insertions(+), 55 deletions(-) create mode 100644 Model/Filter/ClassFilter.php delete mode 100644 Model/Filter/Whitelist.php diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 0d722c4..b3a0f9a 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -144,6 +144,10 @@ class Configuration implements ConfigurationInterface ->info("Namespaces $description") ->prototype('scalar')->end() ->end() + ->arrayNode('classes') + ->info("Classes $description") + ->prototype('scalar')->end() + ->end() ->end(); return $node; diff --git a/Factory/FilterFactory.php b/Factory/FilterFactory.php index 931dc83..b405f93 100644 --- a/Factory/FilterFactory.php +++ b/Factory/FilterFactory.php @@ -10,6 +10,7 @@ namespace Irstea\PlantUmlBundle\Factory; use Irstea\PlantUmlBundle\Model\ClassFilterInterface; use Irstea\PlantUmlBundle\Model\Filter\AcceptAllFilter; +use Irstea\PlantUmlBundle\Model\Filter\ClassFilter; use Irstea\PlantUmlBundle\Model\Filter\Composite\AllFilter; use Irstea\PlantUmlBundle\Model\Filter\DirectoryFilter; use Irstea\PlantUmlBundle\Model\Filter\NamespaceFilter; @@ -42,45 +43,50 @@ class FilterFactory $filters = []; if (!empty($include)) { - $filters[] = $this->createSubFilters($include, false); + $this->appendFilters($filters, $include, false); } if (!empty($exclude)) { - $filters[] = $this->createSubFilters($exclude, true); + $this->appendFilters($filters, $exclude, true); } - $filters = array_merge($filters); switch(count($filters)) { case 0: - return AcceptAllFilter::instance(); + $filter = AcceptAllFilter::instance(); + break; case 1: - return $filters[0]; + $filter = $filters[0]; + break; 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'])) { - $paths = $this->parseDirectories($config['directories']); + $paths = $this->expandBundlePaths($config['directories']); $filters[] = new DirectoryFilter($paths, $notFound); } if (!empty($config['namespaces'])) { - $namespaces = $this->parseNamespaces($config['namespaces']); + $namespaces = $this->expandBundleNamespaces($config['namespaces']); $filters[] = new NamespaceFilter($namespaces, $notFound); } - return $filters; + if (!empty($config['classes'])) { + $classes = $this->expandBundleNamespaces($config['classes']); + $filters[] = new ClassFilter($classes, $notFound); + } } /** * @param array $paths * @return array */ - protected function parseDirectories(array $paths) + protected function expandBundlePaths(array $paths) { $actualPaths = []; foreach($paths as $path) { @@ -97,16 +103,16 @@ class FilterFactory * @param array $paths * @return array */ - protected function parseNamespaces(array $namespaces) + protected function expandBundleNamespaces(array $names) { - $actualNamespaces = []; - foreach($namespaces as $namespace) { - if (preg_match('/^@(\w+)(.*)$/', $namespace, $groups)) { + $actualNames = []; + foreach($names as $name) { + if (preg_match('/^@(\w+)(.*)$/', $name, $groups)) { $bundle = $this->kernel->getBundle($groups[1]); - $namespace = $bundle->getNamespace() . $groups[2]; + $name = $bundle->getNamespace() . $groups[2]; } - $actualNamespaces[] = $namespace; + $actualNames[] = $name; } - return $actualNamespaces; + return $actualNames; } } diff --git a/Model/Filter/ClassFilter.php b/Model/Filter/ClassFilter.php new file mode 100644 index 0000000..9bbf692 --- /dev/null +++ b/Model/Filter/ClassFilter.php @@ -0,0 +1,59 @@ +<?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 ClassFilter implements ClassFilterInterface +{ + /** + * @var string[] + */ + private $classes = []; + + /** + * @var boolena + */ + private $notFound; + + public function __construct(array $classes, $notFound = false) + { + $this->classes = array_map( + function ($class) { + return trim($class, '\\'); + }, + $classes + ); + $this->notFound = $notFound; + } + + public function accept(ReflectionClass $class) + { + $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) + ); + } +} diff --git a/Model/Filter/Whitelist.php b/Model/Filter/Whitelist.php deleted file mode 100644 index 582960a..0000000 --- a/Model/Filter/Whitelist.php +++ /dev/null @@ -1,35 +0,0 @@ -<?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 Whiltelist - * - * @author Guillaume Perréal <guillaume.perreal@irstea.fr> - */ -class Whitelist implements ClassFilterInterface -{ - /** - * @var string[] - */ - private $accepted; - - public function __construct(array $classNames) - { - $this->accepted = array_fill_keys($classNames, true); - } - - public function accept(ReflectionClass $class) - { - return isset($this->accepted[$class->getName()]); - } -} -- GitLab