Skip to content

Commit

Permalink
main
Browse files Browse the repository at this point in the history
  • Loading branch information
gzhegow1991 committed Nov 26, 2024
1 parent 49ad059 commit 3022e96
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
21 changes: 19 additions & 2 deletions src/PipelineFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -21,4 +21,21 @@ public function newPipelineProcessor() : PipelineProcessorInterface

return $pipelineProcessor;
}


/**
* @template-covariant T of object
*
* @param class-string<T>|T $class
*
* @return T
*/
public function newHandlerObject(string $class, array $parameters = []) : object
{
[ $list ] = Lib::array_kwargs($parameters);

$handler = new $class(...$list);

return $handler;
}
}
8 changes: 5 additions & 3 deletions src/PipelineFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
42 changes: 39 additions & 3 deletions src/PipelineInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
25 changes: 4 additions & 21 deletions src/PipelineProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,6 @@ public function __construct(PipelineFactoryInterface $factory)
}


/**
* @template-covariant T of object
*
* @param class-string<T>|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 }
*/
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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)
);
}

Expand Down
10 changes: 0 additions & 10 deletions src/PipelineProcessorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@

interface PipelineProcessorInterface
{
/**
* @template-covariant T of object
*
* @param class-string<T>|T $class
*
* @return T
*/
public function newHandlerObject(string $class, array $parameters = []) : object;


/**
* @return array{ 0?: mixed }
*/
Expand Down

0 comments on commit 3022e96

Please sign in to comment.