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 4ce56a01a7
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
This commit is contained in:
2022-10-18 10:42:37 +02:00
parent a5c7934b76
commit a7ade9dac4
4 changed files with 35 additions and 166 deletions

View File

@@ -14,10 +14,8 @@ namespace Chill\MainBundle\Export;
use Chill\MainBundle\Form\Type\Export\ExportType;
use Chill\MainBundle\Form\Type\Export\PickCenterType;
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder;
use Generator;
use InvalidArgumentException;
use LogicException;
use Psr\Log\LoggerInterface;
use RuntimeException;
@@ -50,8 +48,6 @@ class ExportManager
private AuthorizationHelperInterface $authorizationHelper;
private EntityManagerInterface $em;
/**
* Collected Exports, injected by DI.
*
@@ -82,16 +78,28 @@ class ExportManager
public function __construct(
LoggerInterface $logger,
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);
// NOTE: PHP crashes on the next line (exit error code 11). This is desactivated until further investigation
//$this->formatters = iterator_to_array($formatters);
//foreach ($exportElementProvider as $prefix => $provider) {
// $this->addExportElementsProvider($provider, $prefix);
//}
}
/**
@@ -141,52 +149,17 @@ class ExportManager
}
}
/**
* add an aggregator.
*
* @internal used by DI
*
* @param string $alias
*/
public function addAggregator(AggregatorInterface $aggregator, $alias)
{
$this->aggregators[$alias] = $aggregator;
}
/**
* add an export.
*
* @internal used by DI
*
* @param DirectExportInterface|ExportInterface $export
* @param type $alias
*/
public function addExport($export, $alias)
{
if ($export instanceof ExportInterface || $export instanceof DirectExportInterface) {
$this->exports[$alias] = $export;
} else {
throw new InvalidArgumentException(sprintf(
'The export with alias %s '
. 'does not implements %s or %s.',
$alias,
ExportInterface::class,
DirectExportInterface::class
));
}
}
public function addExportElementsProvider(ExportElementsProviderInterface $provider, $prefix)
{
foreach ($provider->getExportElements() as $suffix => $element) {
$alias = $prefix . '_' . $suffix;
if ($element instanceof ExportInterface) {
$this->addExport($element, $alias);
$this->exports[$alias] = $element;
} elseif ($element instanceof FilterInterface) {
$this->addFilter($element, $alias);
$this->filters[$alias] = $element;
} elseif ($element instanceof AggregatorInterface) {
$this->addAggregator($element, $alias);
$this->aggregators[$alias] = $element;
} elseif ($element instanceof FormatterInterface) {
$this->addFormatter($element, $alias);
} else {
@@ -196,24 +169,12 @@ class ExportManager
}
}
/**
* add a Filter.
*
* @internal Normally used by the dependency injection
*
* @param string $alias
*/
public function addFilter(FilterInterface $filter, $alias)
{
$this->filters[$alias] = $filter;
}
/**
* add a formatter.
*
* @internal used by DI
*
* @param type $alias
* @param string $alias
*/
public function addFormatter(FormatterInterface $formatter, $alias)
{
@@ -231,7 +192,6 @@ class ExportManager
public function generate($exportAlias, array $pickedCentersData, array $data, array $formatterData)
{
$export = $this->getExport($exportAlias);
//$qb = $this->em->createQueryBuilder();
$centers = $this->getPickedCenters($pickedCentersData);
if ($export instanceof DirectExportInterface) {