Skip to content

Commit

Permalink
Add configuration and add new features
Browse files Browse the repository at this point in the history
Before this commit, the bundle hasn't any configuration. With this
commit you can enable/disable the expression language, faker and so on.

And additional handler registries can also be registered, too.
  • Loading branch information
Baachi committed Jan 6, 2020
1 parent 40e7413 commit f105dde
Show file tree
Hide file tree
Showing 21 changed files with 482 additions and 108 deletions.
File renamed without changes.
27 changes: 27 additions & 0 deletions AnonymizerBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace PHPSharkTank\AnonymizerBundle;

use PHPSharkTank\AnonymizerBundle\DependencyInjection\AnonymizerExtension;
use PHPSharkTank\AnonymizerBundle\DependencyInjection\CompilerPass\ExclusionStrategyCompilerPass;
use PHPSharkTank\AnonymizerBundle\DependencyInjection\CompilerPass\HandlerCompilerPass;
use PHPSharkTank\AnonymizerBundle\DependencyInjection\CompilerPass\HandlerRegistryCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class AnonymizerBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new HandlerCompilerPass());
$container->addCompilerPass(new HandlerRegistryCompilerPass());
$container->addCompilerPass(new ExclusionStrategyCompilerPass());
}

public function getContainerExtension()
{
return new AnonymizerExtension();
}
}
104 changes: 104 additions & 0 deletions DependencyInjection/AnonymizerExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

namespace PHPSharkTank\AnonymizerBundle\DependencyInjection;

use PHPSharkTank\Anonymizer\AnonymizerInterface;
use PHPSharkTank\Anonymizer\ExclusionStrategy\DefaultExclusionStrategy;
use PHPSharkTank\Anonymizer\ExclusionStrategy\ExpressionExclusionStrategy;
use PHPSharkTank\Anonymizer\Handler\HandlerInterface;
use PHPSharkTank\Anonymizer\Loader\CachingLoader;
use PHPSharkTank\Anonymizer\Registry\HandlerRegistryInterface;
use PHPSharkTank\AnonymizerBundle\Exception\PackageMissingException;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

class AnonymizerExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yaml');

$this->loadMappingDrivers($config, $loader, $container);
$this->loadExclusionStrategies($config['exclusion_strategy'], $container);

if ($config['enable_alias']) {
$container->setAlias('anonymizer', 'sharktank_anonymizer.anonymizer')->setPublic(true);
}

if ($config['faker']['enabled']) {
$loader->load('faker.yaml');
$container->setAlias('sharktank_anonymizer.faker', $config['faker']['id']);
}

if ($config['cache']['enabled']) {
$id = (string) $container->getAlias('sharktank_anonymizer.mapping_loader');
$cacheDefinition = new Definition(CachingLoader::class);

$cacheDefinition->setDecoratedService($id);
$cacheDefinition->setArguments([
new Reference('sharktank_anonymizer.mapping_loader.inner'),
new Reference(sprintf('cache.%s', $config['cache']['pool']))
]);

$container->setDefinition('sharktank_anonymizer.mapping_loader', $cacheDefinition);
} else {
$container->setAlias('sharktank_anonymizer.mapping_loader', 'sharktank_anonymizer.mapping.annotation_loader');
}

if (method_exists($container, 'registerForAutoconfiguration')) {
$container->registerForAutoconfiguration(HandlerInterface::class)
->addTag('sharktank_anonymizer.handler');
$container->registerForAutoconfiguration(HandlerRegistryInterface::class)
->addTag('sharktank_anonymizer.handler_registry');

$container->setAlias(AnonymizerInterface::class, 'sharktank_anonymizer.anonymizer');
}
}

private function loadExclusionStrategies(array $config, ContainerBuilder $container): void
{
if ($config['default']['enabled']) {
$definition = new Definition(DefaultExclusionStrategy::class);
$definition->addTag('sharktank_anonymizer.exclusion_strategy');

$container->setDefinition('sharktank_anonymizer.exclusion_strategy.default', $definition);
}
if ($config['expression']['enabled']) {
if (!class_exists(ExpressionLanguage::class)) {
throw new PackageMissingException('symfony/expression-language');
}

$definition = new Definition(ExpressionLanguage::class);
$container->setDefinition('sharktank_anonymizer.exclusion_strategy.expression_language', $definition);

$definition = new Definition(ExpressionExclusionStrategy::class, [
new Reference('sharktank_anonymizer.exclusion_strategy.expression_language'),
]);
$definition->addTag('sharktank_anonymizer.exclusion_strategy');

$container->setDefinition('sharktank_anonymizer.exclusion_strategy.expression', $definition);
}
}

private function loadMappingDrivers(array $config, LoaderInterface $loader, ContainerBuilder $container): void
{
$loader->load('annotation.yaml');
}

public function getAlias()
{
return 'sharktank_anonymizer';
}
}
29 changes: 29 additions & 0 deletions DependencyInjection/CompilerPass/ExclusionStrategyCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace PHPSharkTank\AnonymizerBundle\DependencyInjection\CompilerPass;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

final class ExclusionStrategyCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->has('sharktank_anonymizer.exclusion_strategy')) {
return;
}

$arguments = [];
$definition = $container->findDefinition('sharktank_anonymizer.exclusion_strategy');

foreach ($container->findTaggedServiceIds('sharktank_anonymizer.exclusion_strategy') as $id => $tags) {
$arguments[] = new Reference($id);
}

$definition->replaceArgument(0, $arguments);
}

}
28 changes: 28 additions & 0 deletions DependencyInjection/CompilerPass/HandlerCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace PHPSharkTank\AnonymizerBundle\DependencyInjection\CompilerPass;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

final class HandlerCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('sharktank_anonymizer.handler.simple_registry')) {
return;
}

$definition = $container->findDefinition('sharktank_anonymizer.handler.simple_registry');
$arguments = [];

foreach ($container->findTaggedServiceIds('sharktank_anonymizer.handler') as $id => $tags) {
$arguments[] = new Reference($id);
}

$definition->replaceArgument(0, $arguments);
}
}
28 changes: 28 additions & 0 deletions DependencyInjection/CompilerPass/HandlerRegistryCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace PHPSharkTank\AnonymizerBundle\DependencyInjection\CompilerPass;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

final class HandlerRegistryCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('sharktank_anonymizer.handler_registry')) {
return;
}

$definition = $container->findDefinition('sharktank_anonymizer.handler_registry');
$arguments = [];

foreach ($container->findTaggedServiceIds('sharktank_anonymizer.handler_registry') as $id => $tags) {
$arguments[] = new Reference($id);
}

$definition->replaceArgument(0, $arguments);
}
}
52 changes: 52 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace PHPSharkTank\AnonymizerBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('sharktank_anonymizer');
$root = $treeBuilder->getRootNode();

$root
->children()
->booleanNode('enable_alias')->defaultTrue()->end()
->arrayNode('exclusion_strategy')
->addDefaultsIfNotSet()
->children()
->arrayNode('default')
->canBeDisabled()
->end()
->arrayNode('expression')
->canBeEnabled()
->end()
->end()
->end()
->arrayNode('cache')
->canBeEnabled()
->children()
->scalarNode('pool')
->defaultValue('system')
->end()
->end()
->end()
->arrayNode('faker')
->canBeEnabled()
->children()
->scalarNode('id')
->defaultValue('faker')
->end()
->end()
->end()
->end();

return $treeBuilder;
}

}
18 changes: 18 additions & 0 deletions Exception/PackageMissingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace PHPSharkTank\AnonymizerBundle\Exception;

use Throwable;

class PackageMissingException extends \Exception
{
public function __construct(string $package)
{
parent::__construct(sprintf(
'The package "%1$s" is missing. Please run "composer require %1$s".',
$package
));
}
}
6 changes: 6 additions & 0 deletions Resources/config/annotation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
sharktank_anonymizer.mapping.annotation_loader:
class: PHPSharkTank\Anonymizer\Loader\AnnotationLoader
arguments: ['@annotations.reader']

sharktank_anonymizer.mapping_loader: '@sharktank_anonymizer.mapping.annotation_loader'
12 changes: 12 additions & 0 deletions Resources/config/faker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
services:
sharktank_anonymizer.handler.faker:
class: PHPSharkTank\Anonymizer\Handler\FakerHandler
arguments: ['@sharktank_anonymizer.faker', 'faker']
tags:
- { name: 'sharktank.anonymizer.handler' }

sharktank_anonymizer.handler_registry.faker:
class: PHPSharkTank\Anonymizer\Registry\FakerHandlerRegistry
arguments: ['@sharktank_anonymizer.faker']
tags:
- { name: 'sharktank.anonymizer.handler_registry' }
35 changes: 35 additions & 0 deletions Resources/config/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
services:
sharktank_anonymizer.handler.callback:
class: PHPSharkTank\Anonymizer\Handler\CallbackHandler
tags:
- { name: 'sharktank.anonymizer.handler' }

sharktank_anonymizer.handler.null:
class: PHPSharkTank\Anonymizer\Handler\NullHandler
tags:
- { name: 'sharktank.anonymizer.handler' }

sharktank_anonymizer.handler.simple_registry:
class: PHPSharkTank\Anonymizer\Registry\HandlerRegistry
arguments: [[]]

sharktank_anonymizer.handler_registry:
class: PHPSharkTank\Anonymizer\Registry\ChainHandlerRegistry
arguments: [[]]

sharktank_anonymizer.graph_navigator:
class: PHPSharkTank\Anonymizer\Visitor\GraphNavigator
arguments:
- '@sharktank_anonymizer.mapping_loader'
- '@sharktank_anonymizer.handler_registry'
- '@sharktank_anonymizer.exclusion_strategy'
- '@event_dispatcher'

sharktank_anonymizer.exclusion_strategy:
class: PHPSharkTank\Anonymizer\ExclusionStrategy\ChainExclusionStrategy
arguments: [[]]

sharktank_anonymizer.anonymizer:
class: PHPSharkTank\Anonymizer\Anonymizer
public: true
arguments: ['@sharktank_anonymizer.graph_navigator']
Loading

0 comments on commit f105dde

Please sign in to comment.