diff --git a/config/services.yaml b/config/services.yaml index 036f41d..7148d0e 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -3,5 +3,5 @@ services: autowire: true autoconfigure: true - Ardent\PipelineBundle\Pipeline\OptionsProcessor\: - resource: '../src/Pipeline/OptionsProcessor' + Ardent\PipelineBundle\Pipeline\Service\: + resource: '../src/Pipeline/Service' \ No newline at end of file diff --git a/src/Pipeline/OptionsProcessor/Item.php b/src/Pipeline/OptionsProcessor/Item.php index 464420c..c2dbb2a 100644 --- a/src/Pipeline/OptionsProcessor/Item.php +++ b/src/Pipeline/OptionsProcessor/Item.php @@ -17,8 +17,7 @@ class Item string|int|null $id = null ) { - $id ??= sha1(microtime()); - $this->id = $id; + $this->id = $id ?? sha1(microtime()); } public function getId(): string @@ -125,4 +124,9 @@ class Item $this->parent = $parent; return $this; } + + public function getKeys(): array + { + return array_keys($this->data); + } } diff --git a/src/Pipeline/OptionsProcessor/ItemsOptionsPipeline.php b/src/Pipeline/OptionsProcessor/ItemsOptionsPipeline.php index 272b2e1..6a10b88 100644 --- a/src/Pipeline/OptionsProcessor/ItemsOptionsPipeline.php +++ b/src/Pipeline/OptionsProcessor/ItemsOptionsPipeline.php @@ -2,6 +2,7 @@ namespace Ardent\PipelineBundle\Pipeline\OptionsProcessor; +use Ardent\PipelineBundle\Pipeline\Service\OptionsStageInterface; use Psr\Log\LoggerInterface; class ItemsOptionsPipeline @@ -13,7 +14,8 @@ class ItemsOptionsPipeline public function __construct( private readonly OptionsProcessor $processor, - OptionsStageInterface ...$stages) + OptionsStageInterface ...$stages + ) { $this->stages = $stages; } @@ -28,9 +30,11 @@ class ItemsOptionsPipeline /** * @param Item[]|ItemCollection $items */ - public function process(array|ItemCollection $items): ItemCollection + public function process(array|ItemCollection|Item $items): ItemCollection { - if (is_array($items)) { + if ($items instanceof Item) { + $itemBag = new ItemCollection()->add($items); + } elseif (is_array($items)) { $itemBag = new ItemCollection(); foreach ($items as $item) { $itemBag->add($item); @@ -40,4 +44,4 @@ class ItemsOptionsPipeline } return $this->processor->process($itemBag, ...$this->stages); } -} \ No newline at end of file +} diff --git a/src/Pipeline/OptionsProcessor/Options.php b/src/Pipeline/OptionsProcessor/Options.php index d10dd71..0106c1b 100644 --- a/src/Pipeline/OptionsProcessor/Options.php +++ b/src/Pipeline/OptionsProcessor/Options.php @@ -2,11 +2,9 @@ namespace Ardent\PipelineBundle\Pipeline\OptionsProcessor; -class Options +readonly class Options { - public function __construct(private readonly array $options) - { - } + public function __construct(private array $options) {} public function get(string $key, $default = null): mixed { diff --git a/src/Pipeline/OptionsProcessor/OptionsProcessor.php b/src/Pipeline/OptionsProcessor/OptionsProcessor.php index 714999e..16c860d 100644 --- a/src/Pipeline/OptionsProcessor/OptionsProcessor.php +++ b/src/Pipeline/OptionsProcessor/OptionsProcessor.php @@ -2,6 +2,8 @@ namespace Ardent\PipelineBundle\Pipeline\OptionsProcessor; +use Ardent\PipelineBundle\Pipeline\Service\OptionsStageInterface; + class OptionsProcessor { private Options $options; diff --git a/src/Pipeline/OptionsProcessor/OptionsStageBuilder.php b/src/Pipeline/OptionsProcessor/OptionsStageBuilder.php index bfb0ccf..229d401 100644 --- a/src/Pipeline/OptionsProcessor/OptionsStageBuilder.php +++ b/src/Pipeline/OptionsProcessor/OptionsStageBuilder.php @@ -2,6 +2,11 @@ namespace Ardent\PipelineBundle\Pipeline\OptionsProcessor; +use Ardent\PipelineBundle\Pipeline\Service\OptionsStageBag; +use Ardent\PipelineBundle\Pipeline\Service\OptionsStageInterface; +use Ardent\PipelineBundle\Pipeline\Service\SubItemStage; +use Psr\Log\LoggerInterface; + class OptionsStageBuilder { /** @@ -11,10 +16,7 @@ class OptionsStageBuilder public function __construct( private readonly OptionsStageBag $bag, - private readonly ?Options $options = null, - ) - { - } + ) {} /** * @param string|OptionsStageInterface $stageClass Can either be the classFQN of an OptionsStageInterface or one directly @@ -26,7 +28,7 @@ class OptionsStageBuilder $this->stages[] = $this->bag->get($stageClass); } else { throw new \InvalidArgumentException( - sprintf('Stage must be an instance of OptionsStageInterface, %s given', get_debug_type($stage)) + sprintf('Stage must be an instance of OptionsStageInterface, %s given', get_debug_type($stageClass)) ); } } else { @@ -43,8 +45,13 @@ class OptionsStageBuilder return $this; } - public function build(): ItemsOptionsPipeline + public function build(?Options $options = null): ItemsOptionsPipeline { - return new ItemsOptionsPipeline(new OptionsProcessor($this->options), ...$this->stages); + $pipeline = new ItemsOptionsPipeline(new OptionsProcessor($options), ...$this->stages); + $logger = $options?->get(LoggerInterface::class); + if ($logger !== null) { + $pipeline->setLogger($logger); + } + return $pipeline; } } diff --git a/src/Pipeline/OptionsProcessor/AbstractOptionsStage.php b/src/Pipeline/Service/AbstractOptionsStage.php similarity index 81% rename from src/Pipeline/OptionsProcessor/AbstractOptionsStage.php rename to src/Pipeline/Service/AbstractOptionsStage.php index 45a67fe..4a4509a 100644 --- a/src/Pipeline/OptionsProcessor/AbstractOptionsStage.php +++ b/src/Pipeline/Service/AbstractOptionsStage.php @@ -1,7 +1,9 @@ $id * * @return T * @throws ServiceNotFoundException */ - public function get(string $id): OptionsStageInterface + public function get(string $id): T { return $this->locator->get($id); } diff --git a/src/Pipeline/OptionsProcessor/OptionsStageInterface.php b/src/Pipeline/Service/OptionsStageInterface.php similarity index 67% rename from src/Pipeline/OptionsProcessor/OptionsStageInterface.php rename to src/Pipeline/Service/OptionsStageInterface.php index 9838bf2..4b7d13c 100644 --- a/src/Pipeline/OptionsProcessor/OptionsStageInterface.php +++ b/src/Pipeline/Service/OptionsStageInterface.php @@ -1,11 +1,13 @@