Update services to latest symfony version

Use Attributes for tagging and injection
Rename ItemBag to ItemCollection
Lazyload the stages in the OptionsStageBag
This commit is contained in:
Tim 2023-03-19 16:38:32 +01:00
parent f2a18d2928
commit 851bf13e65
10 changed files with 39 additions and 48 deletions

View File

@ -1,14 +1,7 @@
services: services:
# Ardent\PipelineBundle\Pipeline\: _defaults:
# resource: '../src/Pipeline/' autowire: true
# public: true autoconfigure: true
# autowire: true
_instanceof: Ardent\PipelineBundle\Pipeline\OptionsProcessor\:
Ardent\PipelineBundle\Pipeline\OptionsProcessor\OptionsStageInterface: resource: '../src/Pipeline/OptionsProcessor'
tags: [ 'ardent.pipeline.optionsProcessor' ]
Ardent\PipelineBundle\Pipeline\OptionsProcessor\OptionsStageBag:
arguments: [ !tagged { tag: 'ardent.pipeline.optionsProcessor' } ]
public: true
autowire: true

View File

@ -22,12 +22,12 @@ abstract class AbstractOptionsStage implements OptionsStageInterface
$this->logger = $logger; $this->logger = $logger;
} }
public function before(ItemBag $items): void public function before(ItemCollection $items): void
{ {
$this->logger->notice(sprintf('Begin %s with %d item(s)', static::class, $items->count())); $this->logger->notice(sprintf('Begin %s with %d item(s)', static::class, $items->count()));
} }
public function after(ItemBag $items): void public function after(ItemCollection $items): void
{ {
$this->logger->notice(sprintf('End %s with %d item(s)', static::class, $items->count())); $this->logger->notice(sprintf('End %s with %d item(s)', static::class, $items->count()));
} }

View File

@ -8,7 +8,7 @@ class Item
private bool $delete = false; private bool $delete = false;
private ?ItemBag $children = null; private ?ItemCollection $children = null;
private ?Item $parent = null; private ?Item $parent = null;
private array $data = []; private array $data = [];
@ -54,12 +54,12 @@ class Item
return array_key_exists($key, $this->data); return array_key_exists($key, $this->data);
} }
public function getChildren(): ?ItemBag public function getChildren(): ?ItemCollection
{ {
return $this->children; return $this->children;
} }
public function setChildren(ItemBag $children): void public function setChildren(ItemCollection $children): void
{ {
$this->children = $children; $this->children = $children;
} }
@ -67,7 +67,7 @@ class Item
public function addChild(Item $item): self public function addChild(Item $item): self
{ {
if (!$this->children) { if (!$this->children) {
$this->children = new ItemBag(); $this->children = new ItemCollection();
} }
$item->setParent($this); $item->setParent($this);
$this->children->add($item); $this->children->add($item);

View File

@ -2,7 +2,7 @@
namespace Ardent\PipelineBundle\Pipeline\OptionsProcessor; namespace Ardent\PipelineBundle\Pipeline\OptionsProcessor;
class ItemBag class ItemCollection
{ {
/** /**
* @var Item[] * @var Item[]

View File

@ -4,7 +4,7 @@ namespace Ardent\PipelineBundle\Pipeline\OptionsProcessor;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
class ItemBagPipeline class ItemsOptionsPipeline
{ {
/** /**
* @var OptionsStageInterface[] * @var OptionsStageInterface[]
@ -26,12 +26,12 @@ class ItemBagPipeline
} }
/** /**
* @param Item[]|ItemBag $items * @param Item[]|ItemCollection $items
*/ */
public function process(array|ItemBag $items): ItemBag public function process(array|ItemCollection $items): ItemCollection
{ {
if (is_array($items)) { if (is_array($items)) {
$itemBag = new ItemBag(); $itemBag = new ItemCollection();
foreach ($items as $item) { foreach ($items as $item) {
$itemBag->add($item); $itemBag->add($item);
} }

View File

@ -13,7 +13,7 @@ class OptionsProcessor
$this->options = $options ?? new Options([]); $this->options = $options ?? new Options([]);
} }
public function process(ItemBag $items, OptionsStageInterface ...$stages): ItemBag public function process(ItemCollection $items, OptionsStageInterface ...$stages): ItemCollection
{ {
foreach ($stages as $stage) { foreach ($stages as $stage) {
$stage->setOptions($this->options); $stage->setOptions($this->options);

View File

@ -2,42 +2,37 @@
namespace Ardent\PipelineBundle\Pipeline\OptionsProcessor; namespace Ardent\PipelineBundle\Pipeline\OptionsProcessor;
use Exception;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
use Symfony\Component\DependencyInjection\ServiceLocator;
class OptionsStageBag implements ContainerInterface class OptionsStageBag implements ContainerInterface
{ {
/** private readonly ServiceLocator $locator;
* @var OptionsStageInterface[]
*/
private array $stages = [];
public function __construct( public function __construct(
iterable $optionProcessors #[TaggedLocator(OptionsStageInterface::class, indexAttribute: 'name')]
ServiceLocator $locator
) )
{ {
foreach ($optionProcessors as $optionProcessor) { $this->locator = $locator;
/** @var OptionsStageInterface $optionProcessor */
$this->stages[get_class($optionProcessor)] = $optionProcessor;
}
} }
/** /**
* @template T of OptionsStageInterface * @template T of OptionsStageInterface
*
* @param class-string<T> $id * @param class-string<T> $id
*
* @return T * @return T
* @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
*/ */
public function get(string $id): OptionsStageInterface public function get(string $id): OptionsStageInterface
{ {
if (!$this->has($id)) { return $this->locator->get($id);
throw new Exception(sprintf('Class "%s" in not an OptionsStageInterface, it is not in the OptionsStageBag', $id));
}
return $this->stages[$id];
} }
public function has(string $id): bool public function has(string $id): bool
{ {
return array_key_exists($id, $this->stages); return $this->locator->has($id);
} }
} }

View File

@ -30,15 +30,15 @@ class OptionsStageBuilder
return $this; return $this;
} }
public function each(ItemBagPipeline $pipeline): self public function each(ItemsOptionsPipeline $pipeline): self
{ {
$this->stages[] = $this->bag->get(SubItemStage::class)->setSubPipeline($pipeline); $this->stages[] = $this->bag->get(SubItemStage::class)->setSubPipeline($pipeline);
return $this; return $this;
} }
public function build(): ItemBagPipeline public function build(): ItemsOptionsPipeline
{ {
return new ItemBagPipeline(new OptionsProcessor($this->options), ...$this->stages); return new ItemsOptionsPipeline(new OptionsProcessor($this->options), ...$this->stages);
} }
} }

View File

@ -3,7 +3,10 @@
namespace Ardent\PipelineBundle\Pipeline\OptionsProcessor; namespace Ardent\PipelineBundle\Pipeline\OptionsProcessor;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
//#[AutoconfigureTag('ardent.pipeline.optionsProcessor')]
#[AutoconfigureTag]
interface OptionsStageInterface interface OptionsStageInterface
{ {
/** /**
@ -13,9 +16,9 @@ interface OptionsStageInterface
public function setLogger(LoggerInterface $logger): void; public function setLogger(LoggerInterface $logger): void;
public function before(ItemBag $items): void; public function before(ItemCollection $items): void;
public function after(ItemBag $items): void; public function after(ItemCollection $items): void;
public function setOptions(Options $options): void; public function setOptions(Options $options): void;
} }

View File

@ -6,9 +6,9 @@ use Exception;
class SubItemStage extends AbstractOptionsStage class SubItemStage extends AbstractOptionsStage
{ {
private ?ItemBagPipeline $subPipeline = null; private ?ItemsOptionsPipeline $subPipeline = null;
public function getSubPipeline(): ItemBagPipeline public function getSubPipeline(): ItemsOptionsPipeline
{ {
if (!$this->subPipeline) { if (!$this->subPipeline) {
throw new Exception('subPipeline not set'); throw new Exception('subPipeline not set');
@ -16,7 +16,7 @@ class SubItemStage extends AbstractOptionsStage
return $this->subPipeline; return $this->subPipeline;
} }
public function setSubPipeline(ItemBagPipeline $subPipeline): self public function setSubPipeline(ItemsOptionsPipeline $subPipeline): self
{ {
$clone = clone $this; $clone = clone $this;
$clone->subPipeline = $subPipeline; $clone->subPipeline = $subPipeline;