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:
# Ardent\PipelineBundle\Pipeline\:
# resource: '../src/Pipeline/'
# public: true
# autowire: true
_defaults:
autowire: true
autoconfigure: true
_instanceof:
Ardent\PipelineBundle\Pipeline\OptionsProcessor\OptionsStageInterface:
tags: [ 'ardent.pipeline.optionsProcessor' ]
Ardent\PipelineBundle\Pipeline\OptionsProcessor\OptionsStageBag:
arguments: [ !tagged { tag: 'ardent.pipeline.optionsProcessor' } ]
public: true
autowire: true
Ardent\PipelineBundle\Pipeline\OptionsProcessor\:
resource: '../src/Pipeline/OptionsProcessor'

View File

@ -22,12 +22,12 @@ abstract class AbstractOptionsStage implements OptionsStageInterface
$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()));
}
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()));
}

View File

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

View File

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

View File

@ -4,7 +4,7 @@ namespace Ardent\PipelineBundle\Pipeline\OptionsProcessor;
use Psr\Log\LoggerInterface;
class ItemBagPipeline
class ItemsOptionsPipeline
{
/**
* @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)) {
$itemBag = new ItemBag();
$itemBag = new ItemCollection();
foreach ($items as $item) {
$itemBag->add($item);
}

View File

@ -13,7 +13,7 @@ class OptionsProcessor
$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) {
$stage->setOptions($this->options);

View File

@ -2,42 +2,37 @@
namespace Ardent\PipelineBundle\Pipeline\OptionsProcessor;
use Exception;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
use Symfony\Component\DependencyInjection\ServiceLocator;
class OptionsStageBag implements ContainerInterface
{
/**
* @var OptionsStageInterface[]
*/
private array $stages = [];
private readonly ServiceLocator $locator;
public function __construct(
iterable $optionProcessors
#[TaggedLocator(OptionsStageInterface::class, indexAttribute: 'name')]
ServiceLocator $locator
)
{
foreach ($optionProcessors as $optionProcessor) {
/** @var OptionsStageInterface $optionProcessor */
$this->stages[get_class($optionProcessor)] = $optionProcessor;
}
$this->locator = $locator;
}
/**
* @template T of OptionsStageInterface
*
* @param class-string<T> $id
*
* @return T
* @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
*/
public function get(string $id): OptionsStageInterface
{
if (!$this->has($id)) {
throw new Exception(sprintf('Class "%s" in not an OptionsStageInterface, it is not in the OptionsStageBag', $id));
}
return $this->stages[$id];
return $this->locator->get($id);
}
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;
}
public function each(ItemBagPipeline $pipeline): self
public function each(ItemsOptionsPipeline $pipeline): self
{
$this->stages[] = $this->bag->get(SubItemStage::class)->setSubPipeline($pipeline);
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;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
//#[AutoconfigureTag('ardent.pipeline.optionsProcessor')]
#[AutoconfigureTag]
interface OptionsStageInterface
{
/**
@ -13,9 +16,9 @@ interface OptionsStageInterface
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;
}

View File

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