diff --git a/src/Generator.php b/src/Generator.php index 55d3e1de..10460511 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -354,16 +354,7 @@ public function addProcessor($processor, ?string $before = null): Generator if (!$before) { $processors->add($processor); } else { - $matcher = function (array $pipes) use ($before) { - foreach ($pipes as $ii => $current) { - if ($current instanceof $before) { - return $ii; - } - } - - return null; - }; - $processors->insert($processor, $matcher); + $processors->insert($processor, $before); } $this->processorPipeline = $processors; diff --git a/src/Pipeline.php b/src/Pipeline.php index a6fe60aa..754f2afa 100644 --- a/src/Pipeline.php +++ b/src/Pipeline.php @@ -73,10 +73,25 @@ public function remove($pipe = null, ?callable $matcher = null): Pipeline } /** - * @param callable $matcher Callable to determine the position to insert (returned as `int`) + * @param callable|class-string $matcher used to determine the position to insert + * either an `int` from a callable or, in the case of `$matcher` being + * a `class-string`, the position before the first pipe of that class */ - public function insert(callable $pipe, callable $matcher): Pipeline + public function insert(callable $pipe, $matcher): Pipeline { + if (is_string($matcher)) { + $before = $matcher; + $matcher = function (array $pipes) use ($before) { + foreach ($pipes as $ii => $current) { + if ($current instanceof $before) { + return $ii; + } + } + + return null; + }; + } + $index = $matcher($this->pipes); if (null === $index || $index < 0 || $index > count($this->pipes)) { throw new OpenApiException('Matcher result out of range'); diff --git a/tests/PipelineTest.php b/tests/PipelineTest.php index 0e14cbc1..76ca929d 100644 --- a/tests/PipelineTest.php +++ b/tests/PipelineTest.php @@ -89,7 +89,7 @@ public function testRemoveClassString() $this->assertEquals('c', $pipeline->process('')); } - public function testInsert() + public function testInsertMatcher() { $pipeline = new Pipeline(); @@ -100,4 +100,16 @@ public function testInsert() $pipeline->insert($this->pipe('y'), function ($pipes) { return 1; }); $this->assertEquals('xyz', $pipeline->process('')); } + + public function testInsertClassString() + { + $pipeline = new Pipeline(); + + $pipeline->add($this); + $pipeline->add($this->pipe('y')); + $this->assertEquals('xy', $pipeline->process('')); + + $pipeline->insert($this->pipe('a'), __CLASS__); + $this->assertEquals('axy', $pipeline->process('')); + } }