DX: use tags and dependency injection to build ExportManager

This commit is contained in:
Julien Fastré 2022-10-17 17:41:14 +02:00
parent 50e12375f8
commit 4ce56a01a7
4 changed files with 38 additions and 17 deletions

View File

@ -4,18 +4,15 @@ services:
autoconfigure: true
## Indicators
chill.activity.export.count_activity_linked_to_person:
class: Chill\ActivityBundle\Export\Export\LinkedToPerson\CountActivity
Chill\ActivityBundle\Export\Export\LinkedToPerson\CountActivity:
tags:
- { name: chill.export, alias: 'count_activity_linked_to_person' }
chill.activity.export.sum_activity_duration_linked_to_person:
class: Chill\ActivityBundle\Export\Export\LinkedToPerson\StatActivityDuration
Chill\ActivityBundle\Export\Export\LinkedToPerson\StatActivityDuration:
tags:
- { name: chill.export, alias: 'sum_activity_duration_linked_to_person' }
chill.activity.export.list_activity_linked_to_person:
class: Chill\ActivityBundle\Export\Export\LinkedToPerson\ListActivity
Chill\ActivityBundle\Export\Export\LinkedToPerson\ListActivity:
tags:
- { name: chill.export, alias: 'list_activity_linked_to_person' }
@ -116,8 +113,7 @@ services:
- { name: chill.export_filter, alias: 'activity_userscope_filter' }
## Aggregators
chill.activity.export.reason_aggregator:
class: Chill\ActivityBundle\Export\Aggregator\PersonAggregators\ActivityReasonAggregator
Chill\ActivityBundle\Export\Aggregator\PersonAggregators\ActivityReasonAggregator:
tags:
- { name: chill.export_aggregator, alias: activity_reason_aggregator }

View File

@ -18,7 +18,7 @@ use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
/**
* Compiles the services tagged with :.
* Compiles the services tagged with :
*
* - chill.export
* - chill.export_formatter
@ -39,11 +39,11 @@ class ExportsCompilerPass implements CompilerPassInterface
'Chill\MainBundle\Export\ExportManager'
);
$this->compileExports($chillManagerDefinition, $container);
$this->compileFilters($chillManagerDefinition, $container);
$this->compileAggregators($chillManagerDefinition, $container);
$this->compileFormatters($chillManagerDefinition, $container);
$this->compileExportElementsProvider($chillManagerDefinition, $container);
//$this->compileExports($chillManagerDefinition, $container);
//$this->compileFilters($chillManagerDefinition, $container);
//$this->compileAggregators($chillManagerDefinition, $container);
//$this->compileFormatters($chillManagerDefinition, $container);
//$this->compileExportElementsProvider($chillManagerDefinition, $container);
}
private function compileAggregators(
@ -119,6 +119,12 @@ class ExportsCompilerPass implements CompilerPassInterface
$knownAliases = [];
foreach ($taggedServices as $id => $tagAttributes) {
if (!$container->has($id)) {
dump('the service was removed');
continue;
}
foreach ($tagAttributes as $attributes) {
if (!isset($attributes['alias'])) {
throw new LogicException("the 'alias' attribute is missing in your " .

View File

@ -85,13 +85,26 @@ class ExportManager
EntityManagerInterface $em,
AuthorizationCheckerInterface $authorizationChecker,
AuthorizationHelperInterface $authorizationHelper,
TokenStorageInterface $tokenStorage
TokenStorageInterface $tokenStorage,
iterable $exports,
iterable $aggregators,
iterable $filters,
iterable $formatters,
iterable $exportElementProvider
) {
$this->logger = $logger;
$this->em = $em;
$this->authorizationChecker = $authorizationChecker;
$this->authorizationHelper = $authorizationHelper;
$this->user = $tokenStorage->getToken()->getUser();
$this->exports = iterator_to_array($exports);
$this->aggregators = iterator_to_array($aggregators);
$this->filters = iterator_to_array($filters);
$this->formatters = iterator_to_array($formatters);
foreach ($exportElementProvider as $prefix => $provider) {
$this->addExportElementsProvider($provider, $prefix);
}
}
/**
@ -159,7 +172,7 @@ class ExportManager
* @internal used by DI
*
* @param DirectExportInterface|ExportInterface $export
* @param type $alias
* @param string $alias
*/
public function addExport($export, $alias)
{
@ -213,7 +226,7 @@ class ExportManager
*
* @internal used by DI
*
* @param type $alias
* @param string $alias
*/
public function addFormatter(FormatterInterface $formatter, $alias)
{

View File

@ -90,6 +90,12 @@ services:
Chill\MainBundle\Export\ExportManager:
autoconfigure: true
autowire: true
arguments:
$exports: !tagged_iterator { tag: chill.export, index_by: alias }
$aggregators: !tagged_iterator { tag: chill.export_aggregator, index_by: alias }
$filters: !tagged_iterator { tag: chill.export_filter, index_by: alias }
$formatters: !tagged_iterator { tag: chill.export_formatter, index_by: alias }
$exportElementProvider: !tagged_iterator { tag: chill.export_elements_provider, index_by: prefix }
Chill\MainBundle\Security\Resolver\CenterResolverDispatcherInterface: '@Chill\MainBundle\Security\Resolver\CenterResolverDispatcher'