Julien Fastré a7ade9dac4 DX: use tags iterator to inject aggregators, filters and export during ExportManager's construct
Squashed commit of the following:

commit dc2bbc8f4da24549a1d42feb3b453af9f79ab2ff
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Tue Oct 18 10:40:14 2022 +0200

    Fixes: remove iterator_to_array on formatter, which causes a PHP crashes

commit 4ce56a01a77103661fb790d0988df6625f1fb11b
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Mon Oct 17 17:41:14 2022 +0200

    DX: use tags and dependency injection to build ExportManager
2022-10-18 16:27:49 +02:00

109 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\DependencyInjection\CompilerPass;
use Chill\MainBundle\Export\ExportManager;
use LogicException;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
/**
* Compiles the services tagged with :.
*
* - chill.export
* - chill.export_formatter
* - chill.export_aggregator
* - chill.export_filter
* - chill.export_elements_provider
*/
class ExportsCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->has(ExportManager::class)) {
throw new LogicException('service ' . ExportManager::class . ' '
. 'is not defined. It is required by ExportsCompilerPass');
}
$chillManagerDefinition = $container->findDefinition(
ExportManager::class
);
$this->compileFormatters($chillManagerDefinition, $container);
$this->compileExportElementsProvider($chillManagerDefinition, $container);
}
private function compileExportElementsProvider(
Definition $chillManagerDefinition,
ContainerBuilder $container
) {
$taggedServices = $container->findTaggedServiceIds(
'chill.export_elements_provider'
);
$knownAliases = [];
foreach ($taggedServices as $id => $tagAttributes) {
foreach ($tagAttributes as $attributes) {
if (!isset($attributes['prefix'])) {
throw new LogicException("the 'prefix' attribute is missing in your " .
"service '{$id}' definition");
}
if (array_search($attributes['prefix'], $knownAliases, true)) {
throw new LogicException('There is already a chill.export_elements_provider service with prefix '
. $attributes['prefix'] . '. Choose another prefix.');
}
$knownAliases[] = $attributes['prefix'];
$chillManagerDefinition->addMethodCall(
'addExportElementsProvider',
[new Reference($id), $attributes['prefix']]
);
}
}
}
private function compileFormatters(
Definition $chillManagerDefinition,
ContainerBuilder $container
) {
$taggedServices = $container->findTaggedServiceIds(
'chill.export_formatter'
);
$knownAliases = [];
foreach ($taggedServices as $id => $tagAttributes) {
foreach ($tagAttributes as $attributes) {
if (!isset($attributes['alias'])) {
throw new LogicException("the 'alias' attribute is missing in your " .
"service '{$id}' definition");
}
if (array_search($attributes['alias'], $knownAliases, true)) {
throw new LogicException('There is already a chill.export_formatter service with alias '
. $attributes['alias'] . '. Choose another alias.');
}
$knownAliases[] = $attributes['alias'];
$chillManagerDefinition->addMethodCall(
'addFormatter',
[new Reference($id), $attributes['alias']]
);
}
}
}
}