diff --git a/src/Pipeline.php b/src/Pipeline.php index 9cae43b..a1f0747 100644 --- a/src/Pipeline.php +++ b/src/Pipeline.php @@ -93,7 +93,7 @@ public function pipeline($pipeline) // : static return $this; } - public function addPipeline(self $pipeline) : int + public function addPipeline(PipelineInterface $pipeline) : int { $id = ++$this->lastPipeId; diff --git a/src/PipelineFactory.php b/src/PipelineFactory.php index 65568ec..a897799 100644 --- a/src/PipelineFactory.php +++ b/src/PipelineFactory.php @@ -5,9 +5,9 @@ class PipelineFactory implements PipelineFactoryInterface { - public function newPipeline() : PipelineInterface + public function newPipeline(PipelineProcessorInterface $processor = null) : PipelineInterface { - $processor = $this->newPipelineProcessor(); + $processor = $processor ?? $this->newPipelineProcessor(); $pipeline = new Pipeline($processor); @@ -21,4 +21,21 @@ public function newPipelineProcessor() : PipelineProcessorInterface return $pipelineProcessor; } + + + /** + * @template-covariant T of object + * + * @param class-string|T $class + * + * @return T + */ + public function newHandlerObject(string $class, array $parameters = []) : object + { + [ $list ] = Lib::array_kwargs($parameters); + + $handler = new $class(...$list); + + return $handler; + } } diff --git a/src/PipelineFactoryInterface.php b/src/PipelineFactoryInterface.php index c567ce5..eff9938 100644 --- a/src/PipelineFactoryInterface.php +++ b/src/PipelineFactoryInterface.php @@ -2,9 +2,11 @@ namespace Gzhegow\Pipeline; -/** - * @mixin PipelineFactory - */ interface PipelineFactoryInterface { + public function newPipeline(PipelineProcessorInterface $processor = null) : PipelineInterface; + + public function newPipelineProcessor() : PipelineProcessorInterface; + + public function newHandlerObject(string $class, array $parameters = []) : object; } diff --git a/src/PipelineInterface.php b/src/PipelineInterface.php index 103d2bd..731b784 100644 --- a/src/PipelineInterface.php +++ b/src/PipelineInterface.php @@ -2,10 +2,46 @@ namespace Gzhegow\Pipeline; +use Gzhegow\Pipeline\Handler\Action\GenericAction; +use Gzhegow\Pipeline\Handler\Fallback\GenericFallback; +use Gzhegow\Pipeline\Handler\Middleware\GenericMiddleware; +use Gzhegow\Pipeline\Exception\Exception\PipelineException; + -/** - * @mixin Pipeline - */ interface PipelineInterface { + public function pipelines(array $pipelines); + + public function pipeline($pipeline); + + public function addPipeline(PipelineInterface $pipeline) : int; + + + public function middlewares(array $middlewares); + + public function middleware($middleware); + + public function addMiddleware(GenericMiddleware $middleware) : int; + + + public function actions(array $actions); + + public function action($action); + + public function addAction(GenericAction $action) : int; + + + public function fallbacks(array $fallbacks); + + public function fallback($fallback); + + public function addFallback(GenericFallback $fallback) : int; + + + /** + * @throws PipelineException + */ + public function run($input = null, $context = null); + + public function next($input = null, $context = null); } diff --git a/src/PipelineProcessor.php b/src/PipelineProcessor.php index 10a641a..083d35b 100644 --- a/src/PipelineProcessor.php +++ b/src/PipelineProcessor.php @@ -23,23 +23,6 @@ public function __construct(PipelineFactoryInterface $factory) } - /** - * @template-covariant T of object - * - * @param class-string|T $class - * - * @return T - */ - public function newHandlerObject(string $class, array $parameters = []) : object - { - [ $list ] = Lib::array_kwargs($parameters); - - $handler = new $class(...$list); - - return $handler; - } - - /** * @return array{ 0?: mixed } */ @@ -175,7 +158,7 @@ protected function extractHandlerCallable(GenericHandler $handler) } elseif ($handler->method) { $object = null ?? $handler->methodObject - ?? $this->newHandlerObject($handler->methodClass); + ?? $this->factory->newHandlerObject($handler->methodClass); $method = $handler->methodName; @@ -184,7 +167,7 @@ protected function extractHandlerCallable(GenericHandler $handler) } elseif ($handler->invokable) { $object = null ?? $handler->invokableObject - ?? $this->newHandlerObject($handler->invokableClass); + ?? $this->factory->newHandlerObject($handler->invokableClass); $fn = $object; @@ -194,8 +177,8 @@ protected function extractHandlerCallable(GenericHandler $handler) if (! is_callable($fn)) { throw new RuntimeException( - 'Unable to extract callable from handler. ' - . 'Handler: ' . Lib::php_var_dump($handler) + 'Unable to extract callable from handler.' + . ' Handler: ' . Lib::php_var_dump($handler) ); } diff --git a/src/PipelineProcessorInterface.php b/src/PipelineProcessorInterface.php index 2cc5398..6a8d1c2 100644 --- a/src/PipelineProcessorInterface.php +++ b/src/PipelineProcessorInterface.php @@ -9,16 +9,6 @@ interface PipelineProcessorInterface { - /** - * @template-covariant T of object - * - * @param class-string|T $class - * - * @return T - */ - public function newHandlerObject(string $class, array $parameters = []) : object; - - /** * @return array{ 0?: mixed } */