Skip to content

Commit

Permalink
main
Browse files Browse the repository at this point in the history
  • Loading branch information
gzhegow1991 committed Dec 5, 2024
1 parent 33c19cd commit 45a4853
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 34 deletions.
26 changes: 20 additions & 6 deletions src/Handler/GenericHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ public static function tryFrom($from, \Throwable &$last = null) : ?self
protected static function tryFromInstance($instance) : ?self
{
if (! is_a($instance, static::class)) {
return Lib::php_error([ 'The `from` should be instance of: ' . static::class, $instance ]);
return Lib::php_error(
[ 'The `from` should be instance of: ' . static::class, $instance ]
);
}

return $instance;
Expand All @@ -117,7 +119,9 @@ protected static function tryFromInstance($instance) : ?self
protected static function tryFromClosure($closure) : ?self
{
if (! is_a($closure, \Closure::class)) {
return Lib::php_error([ 'The `from` should be instance of: ' . \Closure::class, $closure ]);
return Lib::php_error(
[ 'The `from` should be instance of: ' . \Closure::class, $closure ]
);
}

$instance = new static();
Expand All @@ -132,7 +136,9 @@ protected static function tryFromClosure($closure) : ?self
protected static function tryFromMethod($method) : ?self
{
if (! Lib::php_method_exists($method, null, $methodArray)) {
return Lib::php_error([ 'The `from` should be existing method', $method ]);
return Lib::php_error(
[ 'The `from` should be existing method', $method ]
);
}

$instance = new static();
Expand Down Expand Up @@ -183,7 +189,9 @@ protected static function tryFromInvokable($invokable) : ?self
}

if (null === $instance) {
return Lib::php_error([ 'The `from` should be existing invokable class or object', $invokable ]);
return Lib::php_error(
[ 'The `from` should be existing invokable class or object', $invokable ]
);
}

return $instance;
Expand All @@ -194,10 +202,16 @@ protected static function tryFromInvokable($invokable) : ?self
*/
protected static function tryFromFunction($function) : ?self
{
$_function = Lib::parse_astring($function);
if (null === ($_function = Lib::parse_astring($function))) {
return Lib::php_error(
[ 'The `from` should be existing function name', $function ]
);
}

if (! function_exists($_function)) {
return Lib::php_error([ 'The `from` should be existing function name', $function ]);
return Lib::php_error(
[ 'The `from` should be existing function name', $_function ]
);
}

$instance = new static();
Expand Down
81 changes: 53 additions & 28 deletions src/Pipe/Pipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Gzhegow\Pipeline\Pipe;

use Gzhegow\Pipeline\Lib;
use Gzhegow\Pipeline\Chain\PipelineChain;
use Gzhegow\Pipeline\Chain\ChainInterface;
use Gzhegow\Pipeline\Chain\MiddlewareChain;
use Gzhegow\Pipeline\Handler\GenericHandler;
use Gzhegow\Pipeline\Chain\ChainInterface;
use Gzhegow\Pipeline\Exception\LogicException;
use Gzhegow\Pipeline\Handler\Action\GenericHandlerAction;
use Gzhegow\Pipeline\Handler\Fallback\GenericHandlerFallback;
use Gzhegow\Pipeline\Handler\Middleware\GenericHandlerMiddleware;
Expand Down Expand Up @@ -49,86 +51,109 @@ public static function from($from) : self
return $instance;
}

public static function tryFrom($from) : ?self
public static function tryFrom($from, \Throwable &$last = null) : ?self
{
$last = null;

Lib::php_errors_start($b);

$instance = null
?? static::tryFromInstance($from)
?? static::tryFromMiddleware($from)
?? static::tryFromHandlerAction($from)
?? static::tryFromHandlerFallback($from)
?? static::tryFromHandlerMiddleware($from)
?? static::tryFromPipeline($from)
;
?? static::tryFromPipeline($from);

$errors = Lib::php_errors_end($b);

if (null === $instance) {
foreach ( $errors as $error ) {
$last = new LogicException($error, null, $last);
}
}

return $instance;
}


public static function tryFromInstance($from) : ?self
public static function tryFromInstance($instance) : ?self
{
if (! ($from instanceof static)) {
return null;
if (! ($instance instanceof static)) {
return Lib::php_error(
[ 'The `from` should be instance of: ' . static::class, $instance ]
);
}

return $from;
return $instance;
}

public static function tryFromMiddleware($from) : ?self
public static function tryFromMiddleware($middleware) : ?self
{
if (! ($from instanceof MiddlewareChain)) {
return null;
if (! ($middleware instanceof MiddlewareChain)) {
return Lib::php_error(
[ 'The `from` should be instance of: ' . MiddlewareChain::class, $middleware ]
);
}

$instance = new static();
$instance->middleware = $from;
$instance->middleware = $middleware;

return $instance;
}

public static function tryFromHandlerAction($from) : ?self
public static function tryFromHandlerAction($handlerAction) : ?self
{
if (! ($from instanceof GenericHandlerAction)) {
return null;
if (! ($handlerAction instanceof GenericHandlerAction)) {
return Lib::php_error(
[ 'The `from` should be instance of: ' . GenericHandlerAction::class, $handlerAction ]
);
}

$instance = new static();
$instance->handlerAction = $from;
$instance->handlerAction = $handlerAction;

return $instance;
}

public static function tryFromHandlerFallback($from) : ?self
public static function tryFromHandlerFallback($handlerFallback) : ?self
{
if (! ($from instanceof GenericHandlerFallback)) {
return null;
if (! ($handlerFallback instanceof GenericHandlerFallback)) {
return Lib::php_error(
[ 'The `from` should be instance of: ' . GenericHandlerFallback::class, $handlerFallback ]
);
}

$instance = new static();
$instance->handlerFallback = $from;
$instance->handlerFallback = $handlerFallback;

return $instance;
}

public static function tryFromHandlerMiddleware($from) : ?self
public static function tryFromHandlerMiddleware($handlerMiddleware) : ?self
{
if (! ($from instanceof GenericHandlerMiddleware)) {
return null;
if (! ($handlerMiddleware instanceof GenericHandlerMiddleware)) {
return Lib::php_error(
[ 'The `from` should be instance of: ' . GenericHandlerMiddleware::class, $handlerMiddleware ]
);
}

$instance = new static();
$instance->handlerMiddleware = $from;
$instance->handlerMiddleware = $handlerMiddleware;

return $instance;
}

public static function tryFromPipeline($from) : ?self
public static function tryFromPipeline($pipeline) : ?self
{
if (! ($from instanceof PipelineChain)) {
return null;
if (! ($pipeline instanceof PipelineChain)) {
return Lib::php_error(
[ 'The `from` should be instance of: ' . PipelineChain::class, $pipeline ]
);
}

$instance = new static();
$instance->pipeline = $from;
$instance->pipeline = $pipeline;

return $instance;
}
Expand Down

0 comments on commit 45a4853

Please sign in to comment.